|
| 1 | +package application |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/jfrog/jfrog-cli-application/application/commands/utils" |
| 5 | + "github.com/jfrog/jfrog-cli-application/application/model" |
| 6 | + "github.com/jfrog/jfrog-cli-application/application/service" |
| 7 | + commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands" |
| 8 | + "github.com/jfrog/jfrog-cli-core/v2/plugins/components" |
| 9 | + coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" |
| 10 | + "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" |
| 11 | + "github.com/jfrog/jfrog-client-go/utils/errorutils" |
| 12 | + "slices" |
| 13 | + |
| 14 | + "github.com/jfrog/jfrog-cli-application/application/app" |
| 15 | + "github.com/jfrog/jfrog-cli-application/application/commands" |
| 16 | + "github.com/jfrog/jfrog-cli-application/application/common" |
| 17 | + "github.com/jfrog/jfrog-cli-application/application/service/applications" |
| 18 | +) |
| 19 | + |
| 20 | +type createAppCommand struct { |
| 21 | + serverDetails *coreConfig.ServerDetails |
| 22 | + applicationService applications.ApplicationService |
| 23 | + requestBody *model.CreateAppRequest |
| 24 | +} |
| 25 | + |
| 26 | +func (cac *createAppCommand) Run() error { |
| 27 | + ctx, err := service.NewContext(*cac.serverDetails) |
| 28 | + if err != nil { |
| 29 | + return err |
| 30 | + } |
| 31 | + |
| 32 | + return cac.applicationService.CreateApplication(ctx, cac.requestBody) |
| 33 | +} |
| 34 | + |
| 35 | +func (cac *createAppCommand) ServerDetails() (*coreConfig.ServerDetails, error) { |
| 36 | + return cac.serverDetails, nil |
| 37 | +} |
| 38 | + |
| 39 | +func (cac *createAppCommand) CommandName() string { |
| 40 | + return commands.CreateApp |
| 41 | +} |
| 42 | + |
| 43 | +func (cac *createAppCommand) buildRequestPayload(ctx *components.Context) (*model.CreateAppRequest, error) { |
| 44 | + appKey := ctx.Arguments[0] |
| 45 | + displayName := ctx.GetStringFlagValue(commands.DisplayNameFlag) |
| 46 | + if displayName == "" { |
| 47 | + // Default to the application key if display name is not provided |
| 48 | + displayName = appKey |
| 49 | + } |
| 50 | + |
| 51 | + project := ctx.GetStringFlagValue(commands.ProjectFlag) |
| 52 | + if project == "" { |
| 53 | + return nil, errorutils.CheckErrorf("--%s is mandatory", commands.ProjectFlag) |
| 54 | + } |
| 55 | + |
| 56 | + businessCriticality := ctx.GetStringFlagValue(commands.BusinessCriticalityFlag) |
| 57 | + if businessCriticality == "" { |
| 58 | + // Default to "unspecified" if not provided |
| 59 | + businessCriticality = model.BusinessCriticalityValues[0] |
| 60 | + } else if !slices.Contains(model.BusinessCriticalityValues, businessCriticality) { |
| 61 | + return nil, errorutils.CheckErrorf("invalid value for --%s: '%s'. Allowed values: %s", commands.BusinessCriticalityFlag, businessCriticality, coreutils.ListToText(model.BusinessCriticalityValues)) |
| 62 | + } |
| 63 | + |
| 64 | + maturityLevel := ctx.GetStringFlagValue(commands.MaturityLevelFlag) |
| 65 | + if maturityLevel == "" { |
| 66 | + // Default to "unspecified" if not provided |
| 67 | + maturityLevel = model.MaturityLevelValues[0] |
| 68 | + } else if !slices.Contains(model.MaturityLevelValues, maturityLevel) { |
| 69 | + return nil, errorutils.CheckErrorf("invalid value for --%s: '%s'. Allowed values: %s", commands.MaturityLevelFlag, maturityLevel, coreutils.ListToText(model.MaturityLevelValues)) |
| 70 | + } |
| 71 | + |
| 72 | + description := ctx.GetStringFlagValue(commands.DescriptionFlag) |
| 73 | + userOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.UserOwnersFlag)) |
| 74 | + groupOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.GroupOwnersFlag)) |
| 75 | + labelsMap, err := utils.ParseMapFlag(ctx.GetStringFlagValue(commands.LabelsFlag)) |
| 76 | + if err != nil { |
| 77 | + return nil, err |
| 78 | + } |
| 79 | + |
| 80 | + return &model.CreateAppRequest{ |
| 81 | + ApplicationName: displayName, |
| 82 | + ApplicationKey: appKey, |
| 83 | + Description: description, |
| 84 | + ProjectKey: project, |
| 85 | + MaturityLevel: maturityLevel, |
| 86 | + BusinessCriticality: businessCriticality, |
| 87 | + Labels: labelsMap, |
| 88 | + UserOwners: userOwners, |
| 89 | + GroupOwners: groupOwners, |
| 90 | + }, nil |
| 91 | +} |
| 92 | + |
| 93 | +func (cac *createAppCommand) prepareAndRunCommand(ctx *components.Context) error { |
| 94 | + if len(ctx.Arguments) < 1 { |
| 95 | + return errorutils.CheckErrorf("application key is required") |
| 96 | + } |
| 97 | + |
| 98 | + var err error |
| 99 | + cac.requestBody, err = cac.buildRequestPayload(ctx) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + cac.serverDetails, err = utils.ServerDetailsByFlags(ctx) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + |
| 109 | + return commonCLiCommands.Exec(cac) |
| 110 | +} |
| 111 | + |
| 112 | +func GetCreateAppCommand(appContext app.Context) components.Command { |
| 113 | + cmd := &createAppCommand{ |
| 114 | + applicationService: appContext.GetApplicationService(), |
| 115 | + } |
| 116 | + return components.Command{ |
| 117 | + Name: "create", |
| 118 | + Description: "Create a new application", |
| 119 | + Category: common.CategoryApplication, |
| 120 | + Aliases: []string{"ca"}, |
| 121 | + Arguments: []components.Argument{ |
| 122 | + { |
| 123 | + Name: "application-key", |
| 124 | + Description: "The key of the application to create", |
| 125 | + Optional: false, |
| 126 | + }, |
| 127 | + }, |
| 128 | + Flags: commands.GetCommandFlags(commands.CreateApp), |
| 129 | + Action: cmd.prepareAndRunCommand, |
| 130 | + } |
| 131 | +} |
0 commit comments