/
command.go
83 lines (76 loc) · 2.3 KB
/
command.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
// Package cp implements "aws s3 cp" commands.
package cp
import (
"fmt"
"os"
"time"
pkg_aws "github.com/aws/aws-k8s-tester/pkg/aws"
pkg_s3 "github.com/aws/aws-k8s-tester/pkg/aws/s3"
"github.com/aws/aws-k8s-tester/pkg/logutil"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
var (
logLevel string
partition string
region string
s3Bucket string
s3Key string
localPath string
timeout time.Duration
)
func init() {
cobra.EnablePrefixMatching = true
}
// NewCommand implements "s3-utils cp" command.
func NewCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "cp",
Short: "AWS s3 cp commands",
Run: cpFunc,
}
cmd.PersistentFlags().StringVar(&logLevel, "log-level", "info", "Log level (debug, info, warn, error, dpanic, panic, fatal)")
cmd.PersistentFlags().StringVar(&partition, "partition", "aws", "AWS partition")
cmd.PersistentFlags().StringVar(®ion, "region", "us-west-2", "AWS region")
cmd.PersistentFlags().StringVar(&s3Bucket, "s3-bucket", "", "s3 bucket")
cmd.PersistentFlags().StringVar(&s3Key, "s3-key", "", "s3 key")
cmd.PersistentFlags().StringVar(&localPath, "local-path", "", "local download path")
cmd.PersistentFlags().DurationVar(&timeout, "timeout", 5*time.Minute, "request timeout")
return cmd
}
func cpFunc(cmd *cobra.Command, args []string) {
lcfg := logutil.GetDefaultZapLoggerConfig()
lcfg.Level = zap.NewAtomicLevelAt(logutil.ConvertToZapLevel(logLevel))
lg, err := lcfg.Build()
if err != nil {
panic(err)
}
ss, _, _, err := pkg_aws.New(&pkg_aws.Config{
Logger: lg,
DebugAPICalls: logLevel == "debug",
Partition: partition,
Region: region,
})
if ss == nil {
lg.Fatal("failed to create AWS session", zap.Error(err))
}
if err != nil {
lg.Warn("failed to create AWS session or get sts caller identity", zap.Error(err))
}
reqOpts := []pkg_s3.OpOption{
pkg_s3.WithOverwrite(true),
}
if timeout > 0 {
reqOpts = append(reqOpts, pkg_s3.WithTimeout(timeout))
}
if err = pkg_s3.Download(lg, s3.New(ss), s3Bucket, s3Key, localPath, reqOpts...); err != nil {
lg.Fatal("failed to download S3 file",
zap.String("s3-bucket", s3Bucket),
zap.String("s3-key", s3Key),
zap.Error(err),
)
} else {
fmt.Fprintf(os.Stderr, "SUCCESSFULLY DOWNLOADED %q %q to %q\n", s3Bucket, s3Key, localPath)
}
}