-
Notifications
You must be signed in to change notification settings - Fork 239
/
app.go
232 lines (211 loc) · 6.05 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package command
import (
"context"
"fmt"
"os"
"strings"
"github.com/urfave/cli/v2"
"github.com/peak/s5cmd/v2/log"
"github.com/peak/s5cmd/v2/log/stat"
"github.com/peak/s5cmd/v2/parallel"
"github.com/peak/s5cmd/v2/storage"
)
const (
defaultWorkerCount = 256
defaultRetryCount = 10
appName = "s5cmd"
)
var app = &cli.App{
Name: appName,
Usage: "Blazing fast S3 and local filesystem execution tool",
EnableBashCompletion: true,
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",
EnvVars: []string{"S3_ENDPOINT_URL"},
},
&cli.BoolFlag{
Name: "no-verify-ssl",
Usage: "disable SSL certificate verification",
},
&cli.GenericFlag{
Name: "log",
Value: &EnumValue{
Enum: []string{"trace", "debug", "info", "error"},
Default: "info",
},
Usage: "log level: (trace, debug, info, error)",
},
&cli.BoolFlag{
Name: "install-completion",
Usage: "get completion installation instructions for your shell (only available for bash, pwsh, and zsh)",
},
&cli.BoolFlag{
Name: "dry-run",
Usage: "fake run; show what commands will be executed without actually executing them",
},
&cli.BoolFlag{
Name: "stat",
Usage: "collect statistics of program execution and display it at the end",
},
&cli.BoolFlag{
Name: "no-sign-request",
Usage: "do not sign requests: credentials will not be loaded if --no-sign-request is provided",
},
&cli.BoolFlag{
Name: "use-list-objects-v1",
Usage: "use ListObjectsV1 API for services that don't support ListObjectsV2",
},
&cli.StringFlag{
Name: "request-payer",
Usage: "who pays for request (access requester pays buckets)",
},
&cli.StringFlag{
Name: "profile",
Usage: "use the specified profile from the credentials file",
},
&cli.StringFlag{
Name: "credentials-file",
Usage: "use the specified credentials file instead of the default credentials file",
},
},
Before: func(c *cli.Context) error {
retryCount := c.Int("retry-count")
workerCount := c.Int("numworkers")
printJSON := c.Bool("json")
logLevel := c.String("log")
isStat := c.Bool("stat")
endpointURL := c.String("endpoint-url")
log.Init(logLevel, printJSON)
parallel.Init(workerCount)
if retryCount < 0 {
err := fmt.Errorf("retry count cannot be a negative value")
printError(commandFromContext(c), c.Command.Name, err)
return err
}
if c.Bool("no-sign-request") && c.String("profile") != "" {
err := fmt.Errorf(`"no-sign-request" and "profile" flags cannot be used together`)
printError(commandFromContext(c), c.Command.Name, err)
return err
}
if c.Bool("no-sign-request") && c.String("credentials-file") != "" {
err := fmt.Errorf(`"no-sign-request" and "credentials-file" flags cannot be used together`)
printError(commandFromContext(c), c.Command.Name, err)
return err
}
if isStat {
stat.InitStat()
}
if endpointURL != "" {
if !strings.HasPrefix(endpointURL, "http") {
err := fmt.Errorf(`bad value for --endpoint-url %v: scheme is missing. Must be of the form http://<hostname>/ or https://<hostname>/`, endpointURL)
printError(commandFromContext(c), c.Command.Name, err)
return err
}
}
return nil
},
CommandNotFound: func(c *cli.Context, command string) {
msg := log.ErrorMessage{
Command: command,
Err: "command not found",
}
log.Error(msg)
// After callback is not called if app exists with cli.Exit.
parallel.Close()
log.Close()
},
OnUsageError: func(c *cli.Context, err error, isSubcommand bool) error {
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s %s\n", "Incorrect Usage:", err.Error())
_, _ = fmt.Fprintf(os.Stderr, "See 's5cmd --help' for usage\n")
return err
}
return nil
},
Action: func(c *cli.Context) error {
if c.Bool("install-completion") {
printAutocompletionInstructions(os.Getenv("SHELL"))
return nil
}
args := c.Args()
if args.Present() {
cli.ShowCommandHelp(c, args.First())
return cli.Exit("", 1)
}
return cli.ShowAppHelp(c)
},
After: func(c *cli.Context) error {
if c.Bool("stat") && len(stat.Statistics()) > 0 {
log.Stat(stat.Statistics())
}
parallel.Close()
log.Close()
return nil
},
}
// NewStorageOpts creates storage.Options object from the given context.
func NewStorageOpts(c *cli.Context) storage.Options {
return storage.Options{
DryRun: c.Bool("dry-run"),
Endpoint: c.String("endpoint-url"),
MaxRetries: c.Int("retry-count"),
NoSignRequest: c.Bool("no-sign-request"),
NoVerifySSL: c.Bool("no-verify-ssl"),
RequestPayer: c.String("request-payer"),
UseListObjectsV1: c.Bool("use-list-objects-v1"),
Profile: c.String("profile"),
CredentialFile: c.String("credentials-file"),
LogLevel: log.LevelFromString(c.String("log")),
NoSuchUploadRetryCount: c.Int("no-such-upload-retry-count"),
}
}
func Commands() []*cli.Command {
return []*cli.Command{
NewListCommand(),
NewCopyCommand(),
NewDeleteCommand(),
NewMoveCommand(),
NewMakeBucketCommand(),
NewRemoveBucketCommand(),
NewSelectCommand(),
NewSizeCommand(),
NewCatCommand(),
NewPipeCommand(),
NewRunCommand(),
NewSyncCommand(),
NewVersionCommand(),
NewBucketVersionCommand(),
NewPresignCommand(),
NewHeadCommand(),
}
}
func AppCommand(name string) *cli.Command {
for _, c := range Commands() {
if c.HasName(name) {
return c
}
}
return nil
}
// Main is the entrypoint function to run given commands.
func Main(ctx context.Context, args []string) error {
app.Commands = Commands()
return app.RunContext(ctx, args)
}