-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
114 lines (97 loc) · 2.6 KB
/
main.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
package main
import (
"fmt"
"flag"
"os"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws"
"strings"
)
var (
awsAccessKey string
awsSecretKey string
awsRegion string
sourcePath string
destPath string
kmsId string
)
func validateArgs() {
flag.StringVar(&awsAccessKey, "access-key", "", "AWS Access Key")
flag.StringVar(&awsSecretKey, "secret-key", "", "AWS Secret Key")
flag.StringVar(&kmsId, "kms-id", "", "KMS Key Id")
flag.StringVar(&awsRegion, "region", "us-east-1", "AWS Region")
flag.Parse()
// try to use environment variable if there is
if val, ok := os.LookupEnv("KMS_ID"); kmsId == "" && ok {
kmsId = val
}
if flag.NArg() < 3 {
fmt.Print("Usage: s3csecmd [-a <access_key>] [-s <secret_key>] -kms-id <kms_id> [CMD] ARG1 ARG2 ... ARGN\n")
fmt.Print(" Possible commands are:\n")
fmt.Print(" s3csecmd cp SOURCE DEST\n")
fmt.Print("\n")
os.Exit(1)
}
cmd := flag.Arg(0)
if cmd == "cp" {
sourcePath = flag.Arg(1)
destPath = flag.Arg(2)
} else {
fmt.Printf("Unknown command %v\n", cmd)
os.Exit(1)
}
}
func main() {
validateArgs()
var (
creds *credentials.Credentials
)
if awsAccessKey != "" {
creds = credentials.NewStaticCredentials(
awsAccessKey, awsSecretKey, "",
)
}
sess, err := session.NewSession(&aws.Config{
Credentials: creds,
Region: &awsRegion,
})
if err != nil {
panic(err)
}
cli := NewClient(sess, kmsId)
if strings.Contains(sourcePath, "s3://") {
s3loc, err := NewS3Location(sourcePath)
if err == nil {
if cli.IsFile(s3loc) {
fmt.Printf("Downloading file from %v ...\n", sourcePath)
err = cli.DownloadFile(s3loc, destPath)
} else {
fmt.Printf("Downloading folder from %v ...\n", sourcePath)
err = cli.DownloadFolder(s3loc, destPath)
}
}
if err != nil {
fmt.Fprintf(os.Stderr, "Unexpected error: %v\n", err)
os.Exit(1)
}
} else if strings.Contains(destPath, "s3://") {
s3loc, err := NewS3Location(destPath)
if err == nil {
if ok, _ := IsFile(sourcePath); ok {
fmt.Printf("Uploading file from %v ...\n", sourcePath)
err = cli.UploadFile(sourcePath, s3loc)
} else {
fmt.Printf("Uploading folder from %v ...\n", sourcePath)
err = cli.UploadFolder(sourcePath, s3loc)
}
}
if err != nil {
fmt.Fprintf(os.Stderr, "Unexpected error: %v\n", err)
os.Exit(1)
}
} else {
fmt.Print("You must specify at least one s3 location.\n")
os.Exit(1)
}
}