|
| 1 | +package version |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/jfrog/jfrog-cli-application/apptrust/app" |
| 5 | + "github.com/jfrog/jfrog-cli-application/apptrust/commands" |
| 6 | + "github.com/jfrog/jfrog-cli-application/apptrust/commands/utils" |
| 7 | + "github.com/jfrog/jfrog-cli-application/apptrust/common" |
| 8 | + "github.com/jfrog/jfrog-cli-application/apptrust/model" |
| 9 | + "github.com/jfrog/jfrog-cli-application/apptrust/service" |
| 10 | + "github.com/jfrog/jfrog-cli-application/apptrust/service/versions" |
| 11 | + commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands" |
| 12 | + pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common" |
| 13 | + "github.com/jfrog/jfrog-cli-core/v2/plugins/components" |
| 14 | + coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" |
| 15 | + "github.com/jfrog/jfrog-client-go/utils/errorutils" |
| 16 | +) |
| 17 | + |
| 18 | +type distributeAppVersionCommand struct { |
| 19 | + versionService versions.VersionService |
| 20 | + serverDetails *coreConfig.ServerDetails |
| 21 | + applicationKey string |
| 22 | + version string |
| 23 | + requestPayload *model.DistributeAppVersionRequest |
| 24 | +} |
| 25 | + |
| 26 | +func (dv *distributeAppVersionCommand) Run() error { |
| 27 | + ctx, err := service.NewContext(*dv.serverDetails) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + |
| 32 | + return dv.versionService.DistributeAppVersion(ctx, dv.applicationKey, dv.version, dv.requestPayload) |
| 33 | +} |
| 34 | + |
| 35 | +func (dv *distributeAppVersionCommand) ServerDetails() (*coreConfig.ServerDetails, error) { |
| 36 | + return dv.serverDetails, nil |
| 37 | +} |
| 38 | + |
| 39 | +func (dv *distributeAppVersionCommand) CommandName() string { |
| 40 | + return commands.VersionDistribute |
| 41 | +} |
| 42 | + |
| 43 | +func (dv *distributeAppVersionCommand) prepareAndRunCommand(ctx *components.Context) error { |
| 44 | + if len(ctx.Arguments) != 2 { |
| 45 | + return pluginsCommon.WrongNumberOfArgumentsHandler(ctx) |
| 46 | + } |
| 47 | + |
| 48 | + dv.applicationKey = ctx.Arguments[0] |
| 49 | + dv.version = ctx.Arguments[1] |
| 50 | + |
| 51 | + if err := ValidateDistributionFlags(ctx); err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + |
| 55 | + serverDetails, err := utils.ServerDetailsByFlags(ctx) |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + dv.serverDetails = serverDetails |
| 60 | + |
| 61 | + dv.requestPayload, err = dv.buildRequestPayload(ctx) |
| 62 | + if errorutils.CheckError(err) != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + return commonCLiCommands.Exec(dv) |
| 67 | +} |
| 68 | + |
| 69 | +func (dv *distributeAppVersionCommand) buildRequestPayload(ctx *components.Context) (*model.DistributeAppVersionRequest, error) { |
| 70 | + distributionRules, err := ParseDistributionRules(ctx) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + modifications, err := ParseDistributionModifications(ctx) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + return &model.DistributeAppVersionRequest{ |
| 81 | + DistributionRules: distributionRules, |
| 82 | + AutoCreateRepo: ctx.GetBoolFlagValue(commands.CreateRepoFlag), |
| 83 | + Modifications: modifications, |
| 84 | + }, nil |
| 85 | +} |
| 86 | + |
| 87 | +func GetDistributeAppVersionCommand(appContext app.Context) components.Command { |
| 88 | + cmd := &distributeAppVersionCommand{ |
| 89 | + versionService: appContext.GetVersionService(), |
| 90 | + } |
| 91 | + return components.Command{ |
| 92 | + Name: commands.VersionDistribute, |
| 93 | + Description: "Distribute application version to distribution targets.", |
| 94 | + AIDescription: `Distribute an application version's artifacts to one or more distribution targets according to distribution rules. |
| 95 | +
|
| 96 | +When to use: |
| 97 | +- Make an application version's artifacts available on distribution targets close to consumers. |
| 98 | +- Replicate a released version to remote sites for faster, local access. |
| 99 | +
|
| 100 | +Prerequisites: |
| 101 | +- The application version must already exist. |
| 102 | +- Configured server and distribution permission on the application's project. |
| 103 | +- Reachable distribution target(s) matching the provided distribution rules. |
| 104 | +
|
| 105 | +Common patterns: |
| 106 | + $ jf apptrust version-distribute my-app 1.0.0 |
| 107 | + $ jf apptrust version-distribute my-app 1.0.0 --site="us-*" --country-codes="US;CA" |
| 108 | + $ jf apptrust version-distribute my-app 1.0.0 --dist-rules=/path/to/dist-rules.json |
| 109 | + $ jf apptrust version-distribute my-app 1.0.0 --mapping-pattern="repo/(*)" --mapping-target="target/{1}" |
| 110 | + $ jf apptrust version-distribute my-app 1.0.0 --create-repo |
| 111 | +
|
| 112 | +Gotchas: |
| 113 | +- Distribution is asynchronous: a successful result means it was triggered, not that it has completed on the distribution targets. |
| 114 | +- --dist-rules can't be combined with --site, --city or --country-codes. |
| 115 | +- --mapping-pattern and --mapping-target must be provided together. |
| 116 | +
|
| 117 | +Related: jf apptrust version-delete-remote, jf apptrust version-release`, |
| 118 | + Category: common.CategoryVersion, |
| 119 | + Arguments: []components.Argument{ |
| 120 | + { |
| 121 | + Name: "application-key", |
| 122 | + Description: "The application key.", |
| 123 | + Optional: false, |
| 124 | + }, |
| 125 | + { |
| 126 | + Name: "version", |
| 127 | + Description: "The version to distribute.", |
| 128 | + Optional: false, |
| 129 | + }, |
| 130 | + }, |
| 131 | + Flags: commands.GetCommandFlags(commands.VersionDistribute), |
| 132 | + Action: cmd.prepareAndRunCommand, |
| 133 | + } |
| 134 | +} |
0 commit comments