-
Notifications
You must be signed in to change notification settings - Fork 255
/
Copy pathcat.go
183 lines (155 loc) · 4.38 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
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
package command
import (
"context"
"fmt"
"os"
"github.com/urfave/cli/v2"
"github.com/peak/s5cmd/v2/log/stat"
"github.com/peak/s5cmd/v2/orderedwriter"
"github.com/peak/s5cmd/v2/storage"
"github.com/peak/s5cmd/v2/storage/url"
)
var catHelpTemplate = `Name:
{{.HelpName}} - {{.Usage}}
Usage:
{{.HelpName}} [options] source
Options:
{{range .VisibleFlags}}{{.}}
{{end}}
Examples:
1. Print a remote object's content to stdout
> s5cmd {{.HelpName}} s3://bucket/prefix/object
2. Print specific version of a remote object's content to stdout
> s5cmd {{.HelpName}} --version-id VERSION_ID s3://bucket/prefix/object
3. Concatenate multiple objects matching a prefix or wildcard and print to stdout
> s5cmd {{.HelpName}} "s3://bucket/prefix/*"
`
func NewCatCommand() *cli.Command {
cmd := &cli.Command{
Name: "cat",
HelpName: "cat",
Usage: "print remote object content",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "raw",
Usage: "disable the wildcard operations, useful with filenames that contains glob characters",
},
&cli.StringFlag{
Name: "version-id",
Usage: "use the specified version of an object",
},
&cli.IntFlag{
Name: "concurrency",
Aliases: []string{"c"},
Value: defaultCopyConcurrency,
Usage: "number of concurrent parts transferred between host and remote server",
},
&cli.IntFlag{
Name: "part-size",
Aliases: []string{"p"},
Value: defaultPartSize,
Usage: "size of each part transferred between host and remote server, in MiB",
},
},
CustomHelpTemplate: catHelpTemplate,
Before: func(c *cli.Context) error {
err := validateCatCommand(c)
if err != nil {
printError(commandFromContext(c), c.Command.Name, err)
}
return err
},
Action: func(c *cli.Context) (err error) {
defer stat.Collect(c.Command.FullName(), &err)()
op := c.Command.Name
fullCommand := commandFromContext(c)
src, err := url.New(c.Args().Get(0), url.WithVersion(c.String("version-id")),
url.WithRaw(c.Bool("raw")))
if err != nil {
printError(fullCommand, op, err)
return err
}
return Cat{
src: src,
op: op,
fullCommand: fullCommand,
storageOpts: NewStorageOpts(c),
concurrency: c.Int("concurrency"),
partSize: c.Int64("part-size") * megabytes,
}.Run(c.Context)
},
}
cmd.BashComplete = getBashCompleteFn(cmd, true, false)
return cmd
}
// Cat holds cat operation flags and states.
type Cat struct {
src *url.URL
op string
fullCommand string
storageOpts storage.Options
concurrency int
partSize int64
}
// Run prints content of given source to standard output.
func (c Cat) Run(ctx context.Context) error {
client, err := storage.NewRemoteClient(ctx, c.src, c.storageOpts)
if err != nil {
printError(c.fullCommand, c.op, err)
return err
}
if c.src.IsWildcard() || c.src.IsPrefix() || c.src.IsBucket() {
objectChan := client.List(ctx, c.src, false)
return c.processObjects(ctx, client, objectChan)
}
_, err = client.Stat(ctx, c.src)
if err != nil {
printError(c.fullCommand, c.op, err)
return err
}
return c.processSingleObject(ctx, client, c.src)
}
func (c Cat) processObjects(ctx context.Context, client *storage.S3, objectChan <-chan *storage.Object) error {
for obj := range objectChan {
if obj.Err != nil {
printError(c.fullCommand, c.op, obj.Err)
return obj.Err
}
if obj.Type.IsDir() {
continue
}
err := c.processSingleObject(ctx, client, obj.URL)
if err != nil {
printError(c.fullCommand, c.op, err)
return err
}
}
return nil
}
func (c Cat) processSingleObject(ctx context.Context, client *storage.S3, url *url.URL) error {
buf := orderedwriter.New(os.Stdout)
_, err := client.Get(ctx, url, buf, c.concurrency, c.partSize)
return err
}
func validateCatCommand(c *cli.Context) error {
if c.Args().Len() != 1 {
return fmt.Errorf("expected only one argument")
}
src, err := url.New(c.Args().Get(0), url.WithVersion(c.String("version-id")),
url.WithRaw(c.Bool("raw")))
if err != nil {
return err
}
if !src.IsRemote() {
return fmt.Errorf("source must be a remote object")
}
if err := checkVersioningWithGoogleEndpoint(c); err != nil {
return err
}
if src.IsWildcard() || src.IsPrefix() || src.IsBucket() {
if c.String("version-id") != "" {
return fmt.Errorf("wildcard/prefix operations are disabled with --version-id flag")
}
}
return nil
}