forked from jfrog/jfrog-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.go
456 lines (402 loc) · 14.9 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
package lifecycle
import (
"errors"
"fmt"
commonCliUtils "github.com/jfrog/jfrog-cli-core/v2/common/cliutils"
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
"github.com/jfrog/jfrog-cli-core/v2/common/spec"
speccore "github.com/jfrog/jfrog-cli-core/v2/common/spec"
coreCommon "github.com/jfrog/jfrog-cli-core/v2/docs/common"
"github.com/jfrog/jfrog-cli-core/v2/lifecycle"
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
"github.com/jfrog/jfrog-cli/docs/common"
rbCreate "github.com/jfrog/jfrog-cli/docs/lifecycle/create"
rbDeleteLocal "github.com/jfrog/jfrog-cli/docs/lifecycle/deletelocal"
rbDeleteRemote "github.com/jfrog/jfrog-cli/docs/lifecycle/deleteremote"
rbDistribute "github.com/jfrog/jfrog-cli/docs/lifecycle/distribute"
rbExport "github.com/jfrog/jfrog-cli/docs/lifecycle/export"
rbImport "github.com/jfrog/jfrog-cli/docs/lifecycle/importbundle"
rbPromote "github.com/jfrog/jfrog-cli/docs/lifecycle/promote"
"github.com/jfrog/jfrog-cli/utils/cliutils"
"github.com/jfrog/jfrog-cli/utils/distribution"
artClientUtils "github.com/jfrog/jfrog-client-go/artifactory/services/utils"
"github.com/jfrog/jfrog-client-go/lifecycle/services"
"github.com/jfrog/jfrog-client-go/utils"
"github.com/jfrog/jfrog-client-go/utils/errorutils"
"github.com/urfave/cli"
"os"
"strings"
)
const lcCategory = "Lifecycle"
func GetCommands() []cli.Command {
return cliutils.GetSortedCommands(cli.CommandsByName{
{
Name: "release-bundle-create",
Aliases: []string{"rbc"},
Flags: cliutils.GetCommandFlags(cliutils.ReleaseBundleCreate),
Usage: rbCreate.GetDescription(),
HelpName: coreCommon.CreateUsage("release-bundle-create", rbCreate.GetDescription(), rbCreate.Usage),
UsageText: rbCreate.GetArguments(),
ArgsUsage: common.CreateEnvVars(),
BashComplete: coreCommon.CreateBashCompletionFunc(),
Category: lcCategory,
Action: create,
},
{
Name: "release-bundle-promote",
Aliases: []string{"rbp"},
Flags: cliutils.GetCommandFlags(cliutils.ReleaseBundlePromote),
Usage: rbPromote.GetDescription(),
HelpName: coreCommon.CreateUsage("release-bundle-promote", rbPromote.GetDescription(), rbPromote.Usage),
UsageText: rbPromote.GetArguments(),
ArgsUsage: common.CreateEnvVars(),
BashComplete: coreCommon.CreateBashCompletionFunc(),
Category: lcCategory,
Action: promote,
},
{
Name: "release-bundle-distribute",
Aliases: []string{"rbd"},
Flags: cliutils.GetCommandFlags(cliutils.ReleaseBundleDistribute),
Usage: rbDistribute.GetDescription(),
HelpName: coreCommon.CreateUsage("rbd", rbDistribute.GetDescription(), rbDistribute.Usage),
UsageText: rbDistribute.GetArguments(),
ArgsUsage: common.CreateEnvVars(),
BashComplete: coreCommon.CreateBashCompletionFunc(),
Category: lcCategory,
Action: distribute,
},
{
Name: "release-bundle-export",
Aliases: []string{"rbe"},
Flags: cliutils.GetCommandFlags(cliutils.ReleaseBundleExport),
Usage: rbExport.GetDescription(),
HelpName: coreCommon.CreateUsage("rbe", rbExport.GetDescription(), rbExport.Usage),
UsageText: rbExport.GetArguments(),
ArgsUsage: common.CreateEnvVars(),
BashComplete: coreCommon.CreateBashCompletionFunc(),
Category: lcCategory,
Action: export,
},
{
Name: "release-bundle-delete-local",
Aliases: []string{"rbdell"},
Flags: cliutils.GetCommandFlags(cliutils.ReleaseBundleDeleteLocal),
Usage: rbDeleteLocal.GetDescription(),
HelpName: coreCommon.CreateUsage("rbdell", rbDeleteLocal.GetDescription(), rbDeleteLocal.Usage),
UsageText: rbDeleteLocal.GetArguments(),
ArgsUsage: common.CreateEnvVars(),
BashComplete: coreCommon.CreateBashCompletionFunc(),
Category: lcCategory,
Action: deleteLocal,
},
{
Name: "release-bundle-delete-remote",
Aliases: []string{"rbdelr"},
Flags: cliutils.GetCommandFlags(cliutils.ReleaseBundleDeleteRemote),
Usage: rbDeleteRemote.GetDescription(),
HelpName: coreCommon.CreateUsage("rbdelr", rbDeleteRemote.GetDescription(), rbDeleteRemote.Usage),
UsageText: rbDeleteRemote.GetArguments(),
ArgsUsage: common.CreateEnvVars(),
BashComplete: coreCommon.CreateBashCompletionFunc(),
Category: lcCategory,
Action: deleteRemote,
},
{
Name: "release-bundle-import",
Aliases: []string{"rbi"},
Flags: cliutils.GetCommandFlags(cliutils.ReleaseBundleImport),
Usage: rbImport.GetDescription(),
HelpName: coreCommon.CreateUsage("rbi", rbImport.GetDescription(), rbImport.Usage),
UsageText: rbImport.GetArguments(),
ArgsUsage: common.CreateEnvVars(),
BashComplete: coreCommon.CreateBashCompletionFunc(),
Category: lcCategory,
Action: releaseBundleImport,
},
})
}
func validateCreateReleaseBundleContext(c *cli.Context) error {
if show, err := cliutils.ShowCmdHelpIfNeeded(c, c.Args()); show || err != nil {
return err
}
if c.NArg() != 2 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
return assertValidCreationMethod(c)
}
func assertValidCreationMethod(c *cli.Context) error {
// Determine the methods provided
methods := []bool{
c.IsSet("spec"),
c.IsSet(cliutils.Builds),
c.IsSet(cliutils.ReleaseBundles),
}
methodCount := coreutils.SumTrueValues(methods)
// Validate that only one creation method is provided
if err := validateSingleCreationMethod(methodCount); err != nil {
return err
}
if err := validateCreationValuesPresence(c, methodCount); err != nil {
return err
}
return nil
}
func validateSingleCreationMethod(methodCount int) error {
if methodCount > 1 {
return errorutils.CheckErrorf(
"exactly one creation source must be supplied: --%s, --%s, or --%s.\n"+
"Opt to use the --%s option as the --%s and --%s are deprecated",
"spec", cliutils.Builds, cliutils.ReleaseBundles,
"spec", cliutils.Builds, cliutils.ReleaseBundles,
)
}
return nil
}
func validateCreationValuesPresence(c *cli.Context, methodCount int) error {
if methodCount == 0 {
if !areBuildFlagsSet(c) && !areBuildEnvVarsSet() {
return errorutils.CheckErrorf("Either --build-name or JFROG_CLI_BUILD_NAME, and --build-number or JFROG_CLI_BUILD_NUMBER must be defined")
}
}
return nil
}
// areBuildFlagsSet checks if build-name or build-number flags are set.
func areBuildFlagsSet(c *cli.Context) bool {
return c.IsSet(cliutils.BuildName) || c.IsSet(cliutils.BuildNumber)
}
// areBuildEnvVarsSet checks if build environment variables are set.
func areBuildEnvVarsSet() bool {
return os.Getenv("JFROG_CLI_BUILD_NUMBER") != "" && os.Getenv("JFROG_CLI_BUILD_NAME") != ""
}
func create(c *cli.Context) (err error) {
if err = validateCreateReleaseBundleContext(c); err != nil {
return err
}
creationSpec, err := getReleaseBundleCreationSpec(c)
if err != nil {
return
}
lcDetails, err := createLifecycleDetailsByFlags(c)
if err != nil {
return
}
createCmd := lifecycle.NewReleaseBundleCreateCommand().SetServerDetails(lcDetails).SetReleaseBundleName(c.Args().Get(0)).
SetReleaseBundleVersion(c.Args().Get(1)).SetSigningKeyName(c.String(cliutils.SigningKey)).SetSync(c.Bool(cliutils.Sync)).
SetReleaseBundleProject(cliutils.GetProject(c)).SetSpec(creationSpec).
SetBuildsSpecPath(c.String(cliutils.Builds)).SetReleaseBundlesSpecPath(c.String(cliutils.ReleaseBundles))
return commands.Exec(createCmd)
}
func getReleaseBundleCreationSpec(c *cli.Context) (*spec.SpecFiles, error) {
// Checking if the "builds" or "release-bundles" flags are set - if so, the spec flag should be ignored
if c.IsSet(cliutils.Builds) || c.IsSet(cliutils.ReleaseBundles) {
return nil, nil
}
// Check if the "spec" flag is set - if so, return the spec
if c.IsSet("spec") {
return cliutils.GetSpec(c, true, false)
}
// Else - create a spec from the buildName and buildnumber flags or env vars
buildName := getStringFlagOrEnv(c, cliutils.BuildName, coreutils.BuildName)
buildNumber := getStringFlagOrEnv(c, cliutils.BuildNumber, coreutils.BuildNumber)
if buildName != "" && buildNumber != "" {
return speccore.CreateSpecFromBuildNameAndNumber(buildName, buildNumber)
}
return nil, fmt.Errorf("either the --spec flag must be provided, " +
"or both --build-name and --build-number flags (or their corresponding environment variables " +
"JFROG_CLI_BUILD_NAME and JFROG_CLI_BUILD_NUMBER) must be set")
}
func getStringFlagOrEnv(c *cli.Context, flag string, envVar string) string {
if c.IsSet(flag) {
return c.String(flag)
}
return os.Getenv(envVar)
}
func promote(c *cli.Context) error {
if show, err := cliutils.ShowCmdHelpIfNeeded(c, c.Args()); show || err != nil {
return err
}
if c.NArg() != 3 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
lcDetails, err := createLifecycleDetailsByFlags(c)
if err != nil {
return err
}
promoteCmd := lifecycle.NewReleaseBundlePromoteCommand().SetServerDetails(lcDetails).SetReleaseBundleName(c.Args().Get(0)).
SetReleaseBundleVersion(c.Args().Get(1)).SetEnvironment(c.Args().Get(2)).SetSigningKeyName(c.String(cliutils.SigningKey)).
SetSync(c.Bool(cliutils.Sync)).SetReleaseBundleProject(cliutils.GetProject(c)).
SetIncludeReposPatterns(splitRepos(c, cliutils.IncludeRepos)).SetExcludeReposPatterns(splitRepos(c, cliutils.ExcludeRepos))
return commands.Exec(promoteCmd)
}
func distribute(c *cli.Context) error {
if err := validateDistributeCommand(c); err != nil {
return err
}
lcDetails, err := createLifecycleDetailsByFlags(c)
if err != nil {
return err
}
distributionRules, maxWaitMinutes, _, err := distribution.InitReleaseBundleDistributeCmd(c)
if err != nil {
return err
}
distributeCmd := lifecycle.NewReleaseBundleDistributeCommand()
distributeCmd.SetServerDetails(lcDetails).
SetReleaseBundleName(c.Args().Get(0)).
SetReleaseBundleVersion(c.Args().Get(1)).
SetReleaseBundleProject(cliutils.GetProject(c)).
SetDistributionRules(distributionRules).
SetDryRun(c.Bool("dry-run")).
SetAutoCreateRepo(c.Bool(cliutils.CreateRepo)).
SetPathMappingPattern(c.String(cliutils.PathMappingPattern)).
SetPathMappingTarget(c.String(cliutils.PathMappingTarget)).
SetSync(c.Bool(cliutils.Sync)).
SetMaxWaitMinutes(maxWaitMinutes)
return commands.Exec(distributeCmd)
}
func deleteLocal(c *cli.Context) error {
if show, err := cliutils.ShowCmdHelpIfNeeded(c, c.Args()); show || err != nil {
return err
}
if c.NArg() != 2 && c.NArg() != 3 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
lcDetails, err := createLifecycleDetailsByFlags(c)
if err != nil {
return err
}
environment := ""
if c.NArg() == 3 {
environment = c.Args().Get(2)
}
deleteCmd := lifecycle.NewReleaseBundleDeleteCommand().
SetServerDetails(lcDetails).
SetReleaseBundleName(c.Args().Get(0)).
SetReleaseBundleVersion(c.Args().Get(1)).
SetEnvironment(environment).
SetQuiet(cliutils.GetQuietValue(c)).
SetReleaseBundleProject(cliutils.GetProject(c)).
SetSync(c.Bool(cliutils.Sync))
return commands.Exec(deleteCmd)
}
func deleteRemote(c *cli.Context) error {
if show, err := cliutils.ShowCmdHelpIfNeeded(c, c.Args()); show || err != nil {
return err
}
if c.NArg() != 2 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
lcDetails, err := createLifecycleDetailsByFlags(c)
if err != nil {
return err
}
distributionRules, maxWaitMinutes, _, err := distribution.InitReleaseBundleDistributeCmd(c)
if err != nil {
return err
}
deleteCmd := lifecycle.NewReleaseBundleRemoteDeleteCommand().
SetServerDetails(lcDetails).
SetReleaseBundleName(c.Args().Get(0)).
SetReleaseBundleVersion(c.Args().Get(1)).
SetDistributionRules(distributionRules).
SetDryRun(c.Bool("dry-run")).
SetMaxWaitMinutes(maxWaitMinutes).
SetQuiet(cliutils.GetQuietValue(c)).
SetReleaseBundleProject(cliutils.GetProject(c)).
SetSync(c.Bool(cliutils.Sync))
return commands.Exec(deleteCmd)
}
func export(c *cli.Context) error {
if show, err := cliutils.ShowCmdHelpIfNeeded(c, c.Args()); show || err != nil {
return err
}
if c.NArg() < 2 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
lcDetails, err := createLifecycleDetailsByFlags(c)
if err != nil {
return err
}
exportCmd, modifications := initReleaseBundleExportCmd(c)
downloadConfig, err := cliutils.CreateDownloadConfiguration(c)
if err != nil {
return err
}
exportCmd.
SetServerDetails(lcDetails).
SetReleaseBundleExportModifications(modifications).
SetDownloadConfiguration(*downloadConfig)
return commands.Exec(exportCmd)
}
func releaseBundleImport(c *cli.Context) error {
if show, err := cliutils.ShowCmdHelpIfNeeded(c, c.Args()); show || err != nil {
return err
}
if c.NArg() != 1 {
return cliutils.WrongNumberOfArgumentsHandler(c)
}
rtDetails, err := createLifecycleDetailsByFlags(c)
if err != nil {
return err
}
importCmd := lifecycle.NewReleaseBundleImportCommand()
if err != nil {
return err
}
importCmd.
SetServerDetails(rtDetails).
SetFilepath(c.Args().Get(0))
return commands.Exec(importCmd)
}
func validateDistributeCommand(c *cli.Context) error {
if err := distribution.ValidateReleaseBundleDistributeCmd(c); err != nil {
return err
}
mappingPatternProvided := c.IsSet(cliutils.PathMappingPattern)
mappingTargetProvided := c.IsSet(cliutils.PathMappingTarget)
if (mappingPatternProvided && !mappingTargetProvided) ||
(!mappingPatternProvided && mappingTargetProvided) {
return errorutils.CheckErrorf("the options --%s and --%s must be provided together", cliutils.PathMappingPattern, cliutils.PathMappingTarget)
}
return nil
}
func createLifecycleDetailsByFlags(c *cli.Context) (*coreConfig.ServerDetails, error) {
lcDetails, err := cliutils.CreateServerDetailsWithConfigOffer(c, true, commonCliUtils.Platform)
if err != nil {
return nil, err
}
if lcDetails.Url == "" {
return nil, errors.New("platform URL is mandatory for lifecycle commands")
}
PlatformToLifecycleUrls(lcDetails)
return lcDetails, nil
}
func PlatformToLifecycleUrls(lcDetails *coreConfig.ServerDetails) {
lcDetails.ArtifactoryUrl = utils.AddTrailingSlashIfNeeded(lcDetails.Url) + "artifactory/"
lcDetails.LifecycleUrl = utils.AddTrailingSlashIfNeeded(lcDetails.Url) + "lifecycle/"
lcDetails.Url = ""
}
func splitRepos(c *cli.Context, reposOptionKey string) []string {
if c.IsSet(reposOptionKey) {
return strings.Split(c.String(reposOptionKey), ";")
}
return nil
}
func initReleaseBundleExportCmd(c *cli.Context) (command *lifecycle.ReleaseBundleExportCommand, modifications services.Modifications) {
command = lifecycle.NewReleaseBundleExportCommand().
SetReleaseBundleName(c.Args().Get(0)).
SetReleaseBundleVersion(c.Args().Get(1)).
SetTargetPath(c.Args().Get(2)).
SetProject(c.String(cliutils.Project))
modifications = services.Modifications{
PathMappings: []artClientUtils.PathMapping{
{
Input: c.String(cliutils.PathMappingPattern),
Output: c.String(cliutils.PathMappingTarget),
},
},
}
return
}