|
| 1 | +package application |
| 2 | + |
| 3 | +import ( |
| 4 | + pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common" |
| 5 | + |
| 6 | + "github.com/jfrog/jfrog-cli-application/application/commands/utils" |
| 7 | + "github.com/jfrog/jfrog-cli-application/application/model" |
| 8 | + "github.com/jfrog/jfrog-cli-application/application/service" |
| 9 | + commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands" |
| 10 | + "github.com/jfrog/jfrog-cli-core/v2/plugins/components" |
| 11 | + coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" |
| 12 | + "github.com/jfrog/jfrog-client-go/utils/errorutils" |
| 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 | + applicationKey := ctx.Arguments[0] |
| 45 | + applicationName := ctx.GetStringFlagValue(commands.ApplicationNameFlag) |
| 46 | + if applicationName == "" { |
| 47 | + // Default to the application key if application name is not provided |
| 48 | + applicationName = applicationKey |
| 49 | + } |
| 50 | + |
| 51 | + project := ctx.GetStringFlagValue(commands.ProjectFlag) |
| 52 | + if project == "" { |
| 53 | + return nil, errorutils.CheckErrorf("--%s is mandatory", commands.ProjectFlag) |
| 54 | + } |
| 55 | + |
| 56 | + businessCriticalityStr := ctx.GetStringFlagValue(commands.BusinessCriticalityFlag) |
| 57 | + businessCriticality, err := utils.ValidateEnumFlag( |
| 58 | + commands.BusinessCriticalityFlag, |
| 59 | + businessCriticalityStr, |
| 60 | + model.BusinessCriticalityUnspecified, |
| 61 | + model.BusinessCriticalityValues) |
| 62 | + if err != nil { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + maturityLevelStr := ctx.GetStringFlagValue(commands.MaturityLevelFlag) |
| 67 | + maturityLevel, err := utils.ValidateEnumFlag( |
| 68 | + commands.MaturityLevelFlag, |
| 69 | + maturityLevelStr, |
| 70 | + model.MaturityLevelUnspecified, |
| 71 | + model.MaturityLevelValues) |
| 72 | + if err != nil { |
| 73 | + return nil, err |
| 74 | + } |
| 75 | + |
| 76 | + description := ctx.GetStringFlagValue(commands.DescriptionFlag) |
| 77 | + userOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.UserOwnersFlag)) |
| 78 | + groupOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.GroupOwnersFlag)) |
| 79 | + labelsMap, err := utils.ParseMapFlag(ctx.GetStringFlagValue(commands.LabelsFlag)) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + return &model.CreateAppRequest{ |
| 85 | + ApplicationName: applicationName, |
| 86 | + ApplicationKey: applicationKey, |
| 87 | + Description: description, |
| 88 | + ProjectKey: project, |
| 89 | + MaturityLevel: maturityLevel, |
| 90 | + BusinessCriticality: businessCriticality, |
| 91 | + Labels: labelsMap, |
| 92 | + UserOwners: userOwners, |
| 93 | + GroupOwners: groupOwners, |
| 94 | + }, nil |
| 95 | +} |
| 96 | + |
| 97 | +func (cac *createAppCommand) prepareAndRunCommand(ctx *components.Context) error { |
| 98 | + if len(ctx.Arguments) != 1 { |
| 99 | + return pluginsCommon.WrongNumberOfArgumentsHandler(ctx) |
| 100 | + } |
| 101 | + |
| 102 | + var err error |
| 103 | + cac.requestBody, err = cac.buildRequestPayload(ctx) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + cac.serverDetails, err = utils.ServerDetailsByFlags(ctx) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + return commonCLiCommands.Exec(cac) |
| 114 | +} |
| 115 | + |
| 116 | +func GetCreateAppCommand(appContext app.Context) components.Command { |
| 117 | + cmd := &createAppCommand{ |
| 118 | + applicationService: appContext.GetApplicationService(), |
| 119 | + } |
| 120 | + return components.Command{ |
| 121 | + Name: "create", |
| 122 | + Description: "Create a new application", |
| 123 | + Category: common.CategoryApplication, |
| 124 | + Arguments: []components.Argument{ |
| 125 | + { |
| 126 | + Name: "application-key", |
| 127 | + Description: "The key of the application to create", |
| 128 | + Optional: false, |
| 129 | + }, |
| 130 | + }, |
| 131 | + Flags: commands.GetCommandFlags(commands.CreateApp), |
| 132 | + Action: cmd.prepareAndRunCommand, |
| 133 | + } |
| 134 | +} |
0 commit comments