|
| 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 | + "github.com/jfrog/jfrog-client-go/utils/log" |
| 17 | +) |
| 18 | + |
| 19 | +type updateAppVersionSourcesCommand struct { |
| 20 | + versionService versions.VersionService |
| 21 | + serverDetails *coreConfig.ServerDetails |
| 22 | + applicationKey string |
| 23 | + version string |
| 24 | + requestPayload *model.UpdateVersionSourcesRequest |
| 25 | + sync bool |
| 26 | + dryRun bool |
| 27 | + failFast bool |
| 28 | +} |
| 29 | + |
| 30 | +func (cmd *updateAppVersionSourcesCommand) Run() error { |
| 31 | + ctx, err := service.NewContext(*cmd.serverDetails) |
| 32 | + if err != nil { |
| 33 | + log.Error("Failed to create service context:", err) |
| 34 | + return err |
| 35 | + } |
| 36 | + |
| 37 | + err = cmd.versionService.UpdateAppVersionSources(ctx, cmd.applicationKey, cmd.version, cmd.requestPayload, cmd.sync, cmd.dryRun, cmd.failFast) |
| 38 | + if err != nil { |
| 39 | + log.Error("Failed to update application version sources:", err) |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +func (cmd *updateAppVersionSourcesCommand) ServerDetails() (*coreConfig.ServerDetails, error) { |
| 47 | + return cmd.serverDetails, nil |
| 48 | +} |
| 49 | + |
| 50 | +func (cmd *updateAppVersionSourcesCommand) CommandName() string { |
| 51 | + return commands.VersionUpdateSources |
| 52 | +} |
| 53 | + |
| 54 | +func (cmd *updateAppVersionSourcesCommand) prepareAndRunCommand(ctx *components.Context) error { |
| 55 | + if err := validateUpdateSourcesContext(ctx); err != nil { |
| 56 | + return err |
| 57 | + } |
| 58 | + |
| 59 | + if err := cmd.parseFlagsAndSetFields(ctx); err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + var err error |
| 64 | + cmd.requestPayload, err = cmd.buildRequestPayload(ctx) |
| 65 | + if errorutils.CheckError(err) != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + |
| 69 | + return commonCLiCommands.Exec(cmd) |
| 70 | +} |
| 71 | + |
| 72 | +func validateUpdateSourcesContext(ctx *components.Context) error { |
| 73 | + if len(ctx.Arguments) != 2 { |
| 74 | + return pluginsCommon.WrongNumberOfArgumentsHandler(ctx) |
| 75 | + } |
| 76 | + |
| 77 | + if err := validateNoSpecAndFlagsTogether(ctx); err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + if !hasSourceFlags(ctx) { |
| 82 | + return errorutils.CheckErrorf( |
| 83 | + "At least one source flag is required. Please provide --%s or at least one of the following: --%s, --%s, --%s, --%s, --%s.", |
| 84 | + commands.SpecFlag, commands.SourceTypeBuildsFlag, commands.SourceTypeReleaseBundlesFlag, commands.SourceTypeApplicationVersionsFlag, commands.SourceTypePackagesFlag, commands.SourceTypeArtifactsFlag) |
| 85 | + } |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +// parseFlagsAndSetFields parses CLI flags and sets struct fields accordingly. |
| 91 | +func (cmd *updateAppVersionSourcesCommand) parseFlagsAndSetFields(ctx *components.Context) error { |
| 92 | + cmd.applicationKey = ctx.Arguments[0] |
| 93 | + cmd.version = ctx.Arguments[1] |
| 94 | + |
| 95 | + serverDetails, err := utils.ServerDetailsByFlags(ctx) |
| 96 | + if err != nil { |
| 97 | + return err |
| 98 | + } |
| 99 | + cmd.serverDetails = serverDetails |
| 100 | + |
| 101 | + cmd.sync = ctx.GetBoolTFlagValue(commands.SyncFlag) |
| 102 | + cmd.dryRun = ctx.GetBoolFlagValue(commands.DryRunFlag) |
| 103 | + cmd.failFast = ctx.GetBoolTFlagValue(commands.FailFastFlag) |
| 104 | + |
| 105 | + return nil |
| 106 | +} |
| 107 | + |
| 108 | +func (cmd *updateAppVersionSourcesCommand) buildRequestPayload(ctx *components.Context) (*model.UpdateVersionSourcesRequest, error) { |
| 109 | + sources, filters, err := buildSourcesAndFiltersFromContext(ctx) |
| 110 | + if err != nil { |
| 111 | + return nil, err |
| 112 | + } |
| 113 | + |
| 114 | + return &model.UpdateVersionSourcesRequest{ |
| 115 | + Sources: sources, |
| 116 | + Filters: filters, |
| 117 | + }, nil |
| 118 | +} |
| 119 | + |
| 120 | +func GetUpdateAppVersionSourcesCommand(appContext app.Context) components.Command { |
| 121 | + cmd := &updateAppVersionSourcesCommand{versionService: appContext.GetVersionService()} |
| 122 | + return components.Command{ |
| 123 | + Name: commands.VersionUpdateSources, |
| 124 | + Description: "Updates the sources for a draft application version.", |
| 125 | + Category: common.CategoryVersion, |
| 126 | + Aliases: []string{"vus"}, |
| 127 | + Arguments: []components.Argument{ |
| 128 | + { |
| 129 | + Name: "app-key", |
| 130 | + Description: "The application key of the application for which the version sources are being updated.", |
| 131 | + Optional: false, |
| 132 | + }, |
| 133 | + { |
| 134 | + Name: "version", |
| 135 | + Description: "The version number (in SemVer format) for the application version to update sources.", |
| 136 | + Optional: false, |
| 137 | + }, |
| 138 | + }, |
| 139 | + Flags: commands.GetCommandFlags(commands.VersionUpdateSources), |
| 140 | + Action: cmd.prepareAndRunCommand, |
| 141 | + } |
| 142 | +} |
0 commit comments