forked from peak/s5cmd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcat.go
85 lines (68 loc) · 1.82 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
package command
import (
"context"
"fmt"
"io"
"os"
"github.com/urfave/cli/v2"
"github.com/peak/s5cmd/storage"
"github.com/peak/s5cmd/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
`
var catCommand = &cli.Command{
Name: "cat",
HelpName: "cat",
Usage: "print remote object's contents to stdout",
CustomHelpTemplate: catHelpTemplate,
Before: func(c *cli.Context) error {
if c.Args().Len() != 1 {
return fmt.Errorf("expected only one argument")
}
src, err := url.New(c.Args().Get(0))
if err != nil {
return err
}
if !src.IsRemote() {
return fmt.Errorf("source must be a remote object")
}
if src.IsBucket() || src.IsPrefix() {
return fmt.Errorf("remote source must be an object")
}
if src.HasGlob() {
return fmt.Errorf("remote source %q can not contain glob characters", src)
}
return nil
},
Action: func(c *cli.Context) error {
src, err := url.New(c.Args().Get(0))
if err != nil {
return err
}
return Cat(c.Context, src)
},
}
// Cat prints content of given source to standard output.
func Cat(ctx context.Context, src *url.URL) error {
client := storage.NewClient(src)
// set concurrency to 1 for sequential write to 'stdout' and give a dummy 'partSize' since
// `storage.S3.Get()` ignores 'partSize' if concurrency is set to 1.
_, err := client.Get(ctx, src, sequentialWriterAt{w: os.Stdout}, 1, -1)
return err
}
type sequentialWriterAt struct {
w io.Writer
}
func (sw sequentialWriterAt) WriteAt(p []byte, offset int64) (int, error) {
// ignore 'offset' because we forced sequential downloads
return sw.w.Write(p)
}