-
Notifications
You must be signed in to change notification settings - Fork 13
/
cat.go
49 lines (45 loc) · 1.44 KB
/
cat.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package main
import (
"github.com/qingstor/noah/task"
"github.com/spf13/cobra"
"math"
"github.com/qingstor/qsctl/v2/cmd/qsctl/taskutils"
"github.com/qingstor/qsctl/v2/pkg/i18n"
"github.com/qingstor/qsctl/v2/utils"
)
// CatCommand will handle cat command.
var CatCommand = &cobra.Command{
Use: "cat qs://<bucket_name>/<object_key>",
Short: i18n.Sprintf("cat a remote object to stdout"),
Long: i18n.Sprintf("qsctl cat can cat a remote object to stdout"),
Example: utils.AlignPrintWithColon(
i18n.Sprintf("Cat object: qsctl cat qs://prefix/a"),
),
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceErrors = true // handle runtime errors with i18n, do not show error
if err := catRun(cmd, args); err != nil {
i18n.Fprintf(cmd.OutOrStderr(), "Execute %s command error: %s\n", "cat", err.Error())
return err
}
return nil
},
}
func catRun(c *cobra.Command, args []string) (err error) {
silenceUsage(c) // silence usage when handled error returns
rootTask := taskutils.NewBetweenStorageTask(10)
_, _, err = utils.ParseBetweenStorageInput(rootTask, args[0], "-")
if err != nil {
return
}
t := task.NewCopyFile(rootTask)
t.SetCheckMD5(false)
t.SetCheckTasks(nil)
// cat copy file into local fs, always call CopySmallFile, just set threshold any value to pass validate check
t.SetPartThreshold(math.MaxInt64)
t.Run(c.Context())
if t.GetFault().HasError() {
return t.GetFault()
}
return nil
}