forked from peak/s5cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
124 lines (109 loc) · 2.55 KB
/
app.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package command
import (
"context"
"fmt"
cmpinstall "github.com/posener/complete/cmd/install"
"github.com/urfave/cli/v2"
"github.com/peak/s5cmd/log"
"github.com/peak/s5cmd/parallel"
"github.com/peak/s5cmd/storage"
)
const (
defaultWorkerCount = 256
defaultRetryCount = 10
appName = "s5cmd"
)
var app = &cli.App{
Name: appName,
Usage: "Blazing fast S3 and local filesystem execution tool",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "json",
Usage: "enable JSON formatted output",
},
&cli.IntFlag{
Name: "numworkers",
Value: defaultWorkerCount,
Usage: "number of workers execute operation on each object",
},
&cli.IntFlag{
Name: "retry-count",
Aliases: []string{"r"},
Value: defaultRetryCount,
Usage: "number of times that a request will be retried for failures",
},
&cli.StringFlag{
Name: "endpoint-url",
Usage: "override default S3 host for custom services",
},
&cli.BoolFlag{
Name: "no-verify-ssl",
Usage: "disable SSL certificate verification",
},
&cli.StringFlag{
Name: "log",
Value: "info",
Usage: "log level: (debug, info, error)",
},
&cli.BoolFlag{
Name: "install-completion",
Usage: "install completion for your shell",
},
},
Before: func(c *cli.Context) error {
noVerifySSL := c.Bool("no-verify-ssl")
retryCount := c.Int("retry-count")
endpointURL := c.String("endpoint-url")
workerCount := c.Int("numworkers")
printJSON := c.Bool("json")
logLevel := c.String("log")
log.Init(logLevel, printJSON)
parallel.Init(workerCount)
if retryCount < 0 {
return fmt.Errorf("retry count cannot be a negative value")
}
s3opts := storage.S3Options{
MaxRetries: retryCount,
Endpoint: endpointURL,
NoVerifySSL: noVerifySSL,
}
return storage.Init(s3opts)
},
Action: func(c *cli.Context) error {
if c.Bool("install-completion") {
if cmpinstall.IsInstalled(appName) {
return nil
}
return cmpinstall.Install(appName)
}
return cli.ShowAppHelp(c)
},
After: func(c *cli.Context) error {
parallel.Close()
log.Close()
return nil
},
ExitErrHandler: func(c *cli.Context, err error) {
if err != nil {
printError(givenCommand(c), c.Command.Name, err)
}
},
}
// Main is the entrypoint function to run given commands.
func Main(ctx context.Context, args []string) error {
app.Commands = []*cli.Command{
listCommand,
copyCommand,
deleteCommand,
moveCommand,
makeBucketCommand,
sizeCommand,
catCommand,
runCommand,
versionCommand,
}
if maybeAutoComplete() {
return nil
}
return app.RunContext(ctx, args)
}