forked from jfrog/artifactory-cli-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
326 lines (306 loc) · 10.4 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package main
import (
"os"
"strconv"
"strings"
"github.com/eyalbe4/artifactory-cli-go/utils"
"github.com/eyalbe4/artifactory-cli-go/commands"
"github.com/eyalbe4/artifactory-cli-go/Godeps/_workspace/src/github.com/codegangsta/cli"
)
var flags = new(utils.Flags)
func main() {
utils.CreateTempDirPath()
defer utils.RemoveTempDir()
app := cli.NewApp()
app.Name = "art"
app.Usage = "See https://github.com/JFrogDev/artifactory-cli-go for usage instructions."
app.Version = utils.GetVersion()
app.Commands = []cli.Command{
{
Name: "config",
Flags: getConfigFlags(),
Aliases: []string{"c"},
Usage: "config",
Action: func(c *cli.Context) {
config(c)
},
},
{
Name: "upload",
Flags: getUploadFlags(),
Aliases: []string{"u"},
Usage: "upload <local path> <repo path>",
Action: func(c *cli.Context) {
upload(c)
},
},
{
Name: "download",
Flags: getDownloadFlags(),
Aliases: []string{"d"},
Usage: "download <repo path>",
Action: func(c *cli.Context) {
download(c)
},
},
}
app.Run(os.Args)
}
func getFlags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "url",
Usage: "[Mandatory] Artifactory URL",
},
cli.StringFlag{
Name: "user",
Usage: "[Optional] Artifactory username",
},
cli.StringFlag{
Name: "password",
Usage: "[Optional] Artifactory password",
},
cli.StringFlag{
Name: "ssh-key-path",
Usage: "[Optional] SSH key file path",
},
}
}
func getUploadFlags() []cli.Flag {
flags := []cli.Flag{
nil, nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,
}
copy(flags[0:4], getFlags())
flags[4] = cli.StringFlag{
Name: "props",
Usage: "[Optional] List of properties in the form of \"key1=value1;key2=value2,...\" to be attached to the uploaded artifacts.",
}
flags[5] = cli.StringFlag{
Name: "deb",
Usage: "[Optional] Used for Debian packages in the form of distribution/component/architecture.",
}
flags[6] = cli.StringFlag{
Name: "recursive",
Value: "",
Usage: "[Default: true] Set to false if you do not wish to collect artifacts in sub-folders to be uploaded to Artifactory.",
}
flags[7] = cli.StringFlag{
Name: "flat",
Value: "",
Usage: "[Default: true] If not set to true, and the upload path ends with a slash, files are uploaded according to their file system hierarchy.",
}
flags[8] = cli.BoolFlag{
Name: "regexp",
Usage: "[Default: false] Set to true to use a regular expression instead of wildcards expression to collect files to upload.",
}
flags[9] = cli.StringFlag{
Name: "threads",
Value: "",
Usage: "[Default: 3] Number of artifacts to upload in parallel.",
}
flags[10] = cli.BoolFlag{
Name: "dry-run",
Usage: "[Default: false] Set to true to disable communication with Artifactory.",
}
return flags
}
func getDownloadFlags() []cli.Flag {
flags := []cli.Flag{
nil,nil,nil,nil,nil,nil,nil,nil,nil,nil,
}
copy(flags[0:4], getFlags())
flags[4] = cli.StringFlag{
Name: "props",
Usage: "[Optional] List of properties in the form of \"key1=value1;key2=value2,...\" Only artifacts with these properties will be downloaded.",
}
flags[5] = cli.StringFlag{
Name: "recursive",
Value: "",
Usage: "[Default: true] Set to false if you do not wish to include the download of artifacts inside sub-folders in Artifactory.",
}
flags[6] = cli.StringFlag{
Name: "flat",
Value: "",
Usage: "[Default: false] Set to true if you do not wish to have the Artifactory repository path structure created locally for your downloaded files.",
}
flags[7] = cli.StringFlag{
Name: "min-split",
Value: "",
Usage: "[Default: 5120] Minimum file size in KB to split into ranges when downloading. Set to -1 for no splits.",
}
flags[8] = cli.StringFlag{
Name: "split-count",
Value: "",
Usage: "[Default: 3] Number of parts to split a file when downloading. Set to 0 for no splits.",
}
flags[9] = cli.StringFlag{
Name: "threads",
Value: "",
Usage: "[Default: 3] Number of artifacts to download in parallel.",
}
return flags
}
func getConfigFlags() []cli.Flag {
flags := []cli.Flag{
nil,nil,nil,nil,nil,nil,
}
flags[0] = cli.StringFlag{
Name: "interactive",
Usage: "[Default: true] Set to false if you do not want the config command to be interactive. If true, the --url option becomes optional.",
}
flags[1] = cli.StringFlag{
Name: "enc-password",
Usage: "[Default: true] If set to false then the configured password will not be encrypted using Artifatory's encryption API.",
}
copy(flags[2:6], getFlags())
return flags
}
func initFlags(c *cli.Context, cmd string) {
if c.String("recursive") == "" {
flags.Recursive = true
} else {
flags.Recursive = c.Bool("recursive")
}
if c.String("interactive") == "" {
flags.Interactive = true
} else {
flags.Interactive = c.Bool("interactive")
}
if c.String("enc-password") == "" {
flags.EncPassword = true
} else {
flags.EncPassword = c.Bool("enc-password")
}
if cmd == "config" {
flags.ArtDetails = getArtifactoryDetails(c, false)
if !flags.Interactive && flags.ArtDetails.Url == "" {
utils.Exit(utils.ExitCodeError, "The --url option is mandatory when the --interactive option is set to false")
}
} else {
flags.ArtDetails = getArtifactoryDetails(c, true)
if flags.ArtDetails.Url == "" {
utils.Exit(utils.ExitCodeError, "The --url option is mandatory")
}
}
strFlat := c.String("flat")
if cmd == "upload" {
if strFlat == "" {
flags.Flat = true
} else {
flags.Flat, _ = strconv.ParseBool(strFlat)
}
} else {
if strFlat == "" {
flags.Flat = false
} else {
flags.Flat, _ = strconv.ParseBool(strFlat)
}
}
flags.Deb = c.String("deb")
if flags.Deb != "" && len(strings.Split(flags.Deb, "/")) != 3 {
utils.Exit(utils.ExitCodeError, "The --deb option should be in the form of distribution/component/architecture")
}
flags.Props = c.String("props")
flags.DryRun = c.Bool("dry-run")
flags.UseRegExp = c.Bool("regexp")
var err error
if c.String("threads") == "" {
flags.Threads = 3
} else {
flags.Threads, err = strconv.Atoi(c.String("threads"))
if err != nil || flags.Threads < 1 {
utils.Exit(utils.ExitCodeError, "The '--threads' option should have a numeric positive value.")
}
}
if c.String("min-split") == "" {
flags.MinSplitSize = 5120
} else {
flags.MinSplitSize, err = strconv.ParseInt(c.String("min-split"), 10, 64)
if err != nil {
utils.Exit(utils.ExitCodeError, "The '--min-split' option should have a numeric value. Try 'art download --help'.")
}
}
if c.String("split-count") == "" {
flags.SplitCount = 3
} else {
flags.SplitCount, err = strconv.Atoi(c.String("split-count"))
if err != nil {
utils.Exit(utils.ExitCodeError, "The '--split-count' option should have a numeric value. Try 'art download --help'.")
}
if flags.SplitCount > 15 {
utils.Exit(utils.ExitCodeError, "The '--split-count' option value is limitted to a maximum of 15.")
}
if flags.SplitCount < 0 {
utils.Exit(utils.ExitCodeError, "The '--split-count' option cannot have a negative value.")
}
}
}
func config(c *cli.Context) {
if len(c.Args()) > 1 {
utils.Exit(utils.ExitCodeError, "Wrong number of arguments. Try 'art config --help'.")
} else
if len(c.Args()) == 1 {
if c.Args()[0] == "show" {
commands.ShowConfig()
} else
if c.Args()[0] == "clear" {
commands.ClearConfig()
} else {
utils.Exit(utils.ExitCodeError, "Unknown argument '" + c.Args()[0] + "'. Available arguments are 'show' and 'clear'.")
}
} else {
initFlags(c, "config")
commands.Config(flags.ArtDetails, flags.Interactive, flags.EncPassword)
}
}
func download(c *cli.Context) {
initFlags(c, "download")
if len(c.Args()) != 1 {
utils.Exit(utils.ExitCodeError, "Wrong number of arguments. Try 'art download --help'.")
}
pattern := c.Args()[0]
commands.Download(pattern, flags)
}
func upload(c *cli.Context) {
initFlags(c, "upload")
size := len(c.Args())
if size != 2 {
utils.Exit(utils.ExitCodeError, "Wrong number of arguments. Try 'art upload --help'.")
}
localPath := c.Args()[0]
targetPath := c.Args()[1]
uploaded , failed := commands.Upload(localPath, targetPath, flags)
if failed > 0 {
if uploaded > 0 {
utils.Exit(utils.ExitCodeWarning, "")
}
utils.Exit(utils.ExitCodeError, "")
}
}
func getArtifactoryDetails(c *cli.Context, includeConfig bool) *utils.ArtifactoryDetails {
details := new(utils.ArtifactoryDetails)
details.Url = c.String("url")
details.User = c.String("user")
details.Password = c.String("password")
details.SshKeyPath = c.String("ssh-key-path")
if includeConfig {
if details.Url == "" ||
((details.User == "" || details.Password == "") && details.SshKeyPath == "") {
confDetails := commands.GetConfig()
if details.Url == "" {
details.Url = confDetails.Url
}
if details.User == "" {
details.User = confDetails.User
}
if details.Password == "" {
details.Password = confDetails.Password
}
if details.SshKeyPath == "" {
details.SshKeyPath = confDetails.SshKeyPath
}
}
}
details.Url = utils.AddTrailingSlashIfNeeded(details.Url)
return details
}