forked from larrabee/s3sync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
150 lines (133 loc) · 3.89 KB
/
cli.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
package main
import (
"fmt"
"github.com/alexflint/go-arg"
"net/url"
"strings"
"time"
)
var (
version = "dev"
commit = "none"
date = "unknown"
)
type ConnType int
type onFailAction int
const (
s3Conn ConnType = 1
fsConn ConnType = 2
s3StConn ConnType = 3
onFailFatal onFailAction = 1
onFailLog onFailAction = 2
)
type argsParsed struct {
args
Source connect
Target connect
RetryInterval time.Duration
OnFail onFailAction
}
type connect struct {
Type ConnType
Bucket string
Path string
}
type args struct {
// Source config
Source string `arg:"positional"`
SourceKey string `arg:"--sk" help:"Source AWS key"`
SourceSecret string `arg:"--ss" help:"Source AWS secret"`
SourceRegion string `arg:"--sr" help:"Source AWS Region"`
SourceEndpoint string `arg:"--se" help:"Source AWS Endpoint"`
// Target config
Target string `arg:"positional"`
TargetKey string `arg:"--tk" help:"Target AWS key"`
TargetSecret string `arg:"--ts" help:"Target AWS secret"`
TargetRegion string `arg:"--tr" help:"Target AWS Region"`
TargetEndpoint string `arg:"--te" help:"Target AWS Endpoint"`
// Sync config
Workers uint `arg:"-w" help:"Workers count"`
Retry uint `arg:"-r" help:"Max numbers of retries to sync file"`
RetryInterval uint `arg:"--rs" help:"Sleep interval (sec) between sync retries on error"`
FilterExtension []string `arg:"--fe,separate" help:"Sync only files with given extensions"`
FilterTimestamp int64 `arg:"--ft" help:"Sync only files modified after given unix timestamp"`
Acl string `arg:"--acl" help:"S3 ACL for uploaded files. Possible values: private, public-read, public-read-write, aws-exec-read, authenticated-read, bucket-owner-read, bucket-owner-full-control"`
Debug bool `arg:"-d" help:"Show debug logging"`
OnFail string `arg:"--on-fail,-f" help:"Action on failed. Possible values: fatal, log"`
DisableHTTP2 bool `arg:"--disable-http2" help:"Disable HTTP2 for http client"`
}
//Version return program version string on human format
func (args) Version() string {
return fmt.Sprintf("Version: %v, commit: %v, built at: %v", version, commit, date)
}
//Description return program description string
func (args) Description() string {
return "Really fast sync tool for S3"
}
//GetCliArgs return cli args structure and error
func GetCliArgs() (cli argsParsed, err error) {
rawCli := args{}
rawCli.SourceRegion = "us-east-1"
rawCli.TargetRegion = "us-east-1"
rawCli.Workers = 16
rawCli.Retry = 1
rawCli.RetryInterval = 1
rawCli.Acl = "private"
rawCli.OnFail = "fatal"
p := arg.MustParse(&rawCli)
cli.args = rawCli
switch cli.args.Acl {
case "private":
break
case "public-read":
break
case "public-read-write":
break
case "aws-exec-read":
break
case "authenticated-read":
break
case "bucket-owner-read":
break
case "bucket-owner-full-control":
break
default:
p.Fail("--acl must be one of \"private, public-read, public-read-write, aws-exec-read, authenticated-read, bucket-owner-read, bucket-owner-full-control\"")
}
switch cli.args.OnFail {
case "fatal":
cli.OnFail = onFailFatal
case "log":
cli.OnFail = onFailLog
default:
p.Fail("--on-fail must be one of \"fatal, log\"")
}
cli.RetryInterval = time.Duration(cli.args.RetryInterval) * time.Second
if cli.Source, err = parseConn(cli.args.Source); err != nil {
return cli, err
}
if cli.Target, err = parseConn(cli.args.Target); err != nil {
return cli, err
}
return
}
func parseConn(cStr string) (conn connect, err error) {
u, err := url.Parse(cStr)
if err != nil {
return
}
switch u.Scheme {
case "s3":
conn.Type = s3Conn
conn.Bucket = u.Host
conn.Path = strings.TrimPrefix(u.Path, "/")
case "s3st":
conn.Type = s3StConn
conn.Bucket = u.Host
conn.Path = strings.TrimPrefix(u.Path, "/")
default:
conn.Type = fsConn
conn.Path = cStr
}
return
}