-
Notifications
You must be signed in to change notification settings - Fork 5
Update application command #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 12 commits
402ea93
81bada5
bb83487
9a463db
9f1939d
9edf5a4
3ef91d9
f2106be
d6552a1
362f730
730d870
2a2d746
85abb33
a38c311
9f706be
f031ec9
064c2ac
e7f7c58
2cef81c
b9ec8c9
0b7e0c3
9a54ab8
9741a02
1d39479
89ff137
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -49,7 +49,7 @@ prereq:: | |
| GOBIN=${TOOLS_DIR} $(GOCMD) install go.uber.org/mock/[email protected] | ||
| ${TOOLS_DIR}/mockgen --version | ||
|
|
||
| build:: | ||
| build:: clean generate-mock | ||
| $(GOCMD) env GOOS GOARCH | ||
| $(GOCMD) build -ldflags="${LINKERFLAGS}" -gcflags ${COMPILERFLAGS} -o ${BINARY_CLI}/application-cli-plugin main.go | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package application | ||
|
|
||
| import ( | ||
| pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common" | ||
|
Check failure on line 4 in application/commands/application/create_app_cmd.go
|
||
| "slices" | ||
|
|
||
|
Check failure on line 6 in application/commands/application/create_app_cmd.go
|
||
| "github.com/jfrog/jfrog-cli-application/application/commands/utils" | ||
| "github.com/jfrog/jfrog-cli-application/application/model" | ||
| "github.com/jfrog/jfrog-cli-application/application/service" | ||
| commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands" | ||
| "github.com/jfrog/jfrog-cli-core/v2/plugins/components" | ||
| coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
| "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" | ||
| "github.com/jfrog/jfrog-client-go/utils/errorutils" | ||
|
|
||
| "github.com/jfrog/jfrog-cli-application/application/app" | ||
| "github.com/jfrog/jfrog-cli-application/application/commands" | ||
| "github.com/jfrog/jfrog-cli-application/application/common" | ||
| "github.com/jfrog/jfrog-cli-application/application/service/applications" | ||
| ) | ||
|
|
||
| type createAppCommand struct { | ||
| serverDetails *coreConfig.ServerDetails | ||
| applicationService applications.ApplicationService | ||
| requestBody *model.AppDescriptor | ||
| } | ||
|
|
||
| func (cac *createAppCommand) Run() error { | ||
| ctx, err := service.NewContext(*cac.serverDetails) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return cac.applicationService.CreateApplication(ctx, cac.requestBody) | ||
| } | ||
|
|
||
| func (cac *createAppCommand) ServerDetails() (*coreConfig.ServerDetails, error) { | ||
| return cac.serverDetails, nil | ||
| } | ||
|
|
||
| func (cac *createAppCommand) CommandName() string { | ||
| return commands.CreateApp | ||
| } | ||
|
|
||
| func (cac *createAppCommand) buildRequestPayload(ctx *components.Context) (*model.AppDescriptor, error) { | ||
| appKey := ctx.Arguments[0] | ||
| displayName := ctx.GetStringFlagValue(commands.DisplayNameFlag) | ||
| if displayName == "" { | ||
| // Default to the application key if display name is not provided | ||
| displayName = appKey | ||
| } | ||
|
|
||
| project := ctx.GetStringFlagValue(commands.ProjectFlag) | ||
| if project == "" { | ||
| return nil, errorutils.CheckErrorf("--%s is mandatory", commands.ProjectFlag) | ||
| } | ||
|
|
||
| businessCriticality := ctx.GetStringFlagValue(commands.BusinessCriticalityFlag) | ||
| if businessCriticality == "" { | ||
| // Default to "unspecified" if not provided | ||
| businessCriticality = model.BusinessCriticalityValues[0] | ||
| } else if !slices.Contains(model.BusinessCriticalityValues, businessCriticality) { | ||
| return nil, errorutils.CheckErrorf("invalid value for --%s: '%s'. Allowed values: %s", commands.BusinessCriticalityFlag, businessCriticality, coreutils.ListToText(model.BusinessCriticalityValues)) | ||
| } | ||
|
|
||
| maturityLevel := ctx.GetStringFlagValue(commands.MaturityLevelFlag) | ||
| if maturityLevel == "" { | ||
| // Default to "unspecified" if not provided | ||
| maturityLevel = model.MaturityLevelValues[0] | ||
| } else if !slices.Contains(model.MaturityLevelValues, maturityLevel) { | ||
| return nil, errorutils.CheckErrorf("invalid value for --%s: '%s'. Allowed values: %s", commands.MaturityLevelFlag, maturityLevel, coreutils.ListToText(model.MaturityLevelValues)) | ||
| } | ||
|
|
||
| description := ctx.GetStringFlagValue(commands.DescriptionFlag) | ||
| userOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.UserOwnersFlag)) | ||
| groupOwners := utils.ParseSliceFlag(ctx.GetStringFlagValue(commands.GroupOwnersFlag)) | ||
| labelsMap, err := utils.ParseMapFlag(ctx.GetStringFlagValue(commands.LabelsFlag)) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &model.AppDescriptor{ | ||
| ApplicationName: displayName, | ||
| ApplicationKey: appKey, | ||
| Description: description, | ||
| ProjectKey: project, | ||
| MaturityLevel: maturityLevel, | ||
| BusinessCriticality: businessCriticality, | ||
| Labels: labelsMap, | ||
| UserOwners: userOwners, | ||
| GroupOwners: groupOwners, | ||
| }, nil | ||
| } | ||
|
|
||
| func (cac *createAppCommand) prepareAndRunCommand(ctx *components.Context) error { | ||
| if len(ctx.Arguments) != 1 { | ||
| return pluginsCommon.WrongNumberOfArgumentsHandler(ctx) | ||
| } | ||
|
|
||
| var err error | ||
| cac.requestBody, err = cac.buildRequestPayload(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| cac.serverDetails, err = utils.ServerDetailsByFlags(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return commonCLiCommands.Exec(cac) | ||
| } | ||
|
|
||
| func GetCreateAppCommand(appContext app.Context) components.Command { | ||
| cmd := &createAppCommand{ | ||
| applicationService: appContext.GetApplicationService(), | ||
| } | ||
| return components.Command{ | ||
| Name: "create", | ||
| Description: "Create a new application", | ||
| Category: common.CategoryApplication, | ||
| Arguments: []components.Argument{ | ||
| { | ||
| Name: "application-key", | ||
| Description: "The key of the application to create", | ||
| Optional: false, | ||
| }, | ||
| }, | ||
| Flags: commands.GetCommandFlags(commands.CreateApp), | ||
| Action: cmd.prepareAndRunCommand, | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| package application | ||
|
|
||
| import ( | ||
| "errors" | ||
| "flag" | ||
| "github.com/urfave/cli" | ||
|
Check failure on line 6 in application/commands/application/create_app_cmd_test.go
|
||
| "testing" | ||
|
|
||
|
Check failure on line 8 in application/commands/application/create_app_cmd_test.go
|
||
| "github.com/jfrog/jfrog-cli-application/application/model" | ||
| mockapps "github.com/jfrog/jfrog-cli-application/application/service/applications/mocks" | ||
| "github.com/jfrog/jfrog-cli-core/v2/plugins/components" | ||
| "github.com/jfrog/jfrog-cli-core/v2/utils/config" | ||
| "github.com/stretchr/testify/assert" | ||
| "go.uber.org/mock/gomock" | ||
| ) | ||
|
|
||
| func TestCreateAppCommand_Run(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
|
|
||
| serverDetails := &config.ServerDetails{Url: "https://example.com"} | ||
| requestPayload := &model.AppDescriptor{ | ||
| ApplicationKey: "app-key", | ||
| ApplicationName: "app-name", | ||
| ProjectKey: "proj-key", | ||
| } | ||
|
|
||
| mockAppService := mockapps.NewMockApplicationService(ctrl) | ||
| mockAppService.EXPECT().CreateApplication(gomock.Any(), requestPayload).Return(nil).Times(1) | ||
|
|
||
| cmd := &createAppCommand{ | ||
| applicationService: mockAppService, | ||
| serverDetails: serverDetails, | ||
| requestBody: requestPayload, | ||
| } | ||
|
|
||
| err := cmd.Run() | ||
| assert.NoError(t, err) | ||
| } | ||
|
|
||
| func TestCreateAppCommand_Error(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
|
|
||
| serverDetails := &config.ServerDetails{Url: "https://example.com"} | ||
| requestPayload := &model.AppDescriptor{ | ||
| ApplicationKey: "app-key", | ||
| ApplicationName: "app-name", | ||
| ProjectKey: "proj-key", | ||
| } | ||
|
|
||
| mockAppService := mockapps.NewMockApplicationService(ctrl) | ||
| mockAppService.EXPECT().CreateApplication(gomock.Any(), requestPayload).Return(errors.New("failed to create an application. Status code: 500")).Times(1) | ||
|
|
||
| cmd := &createAppCommand{ | ||
| applicationService: mockAppService, | ||
| serverDetails: serverDetails, | ||
| requestBody: requestPayload, | ||
| } | ||
|
|
||
| err := cmd.Run() | ||
| assert.Error(t, err) | ||
| assert.Equal(t, "failed to create an application. Status code: 500", err.Error()) | ||
| } | ||
|
|
||
| func TestCreateAppCommand_WrongNumberOfArguments(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
| app := cli.NewApp() | ||
| set := flag.NewFlagSet("test", 0) | ||
| ctx := cli.NewContext(app, set, nil) | ||
|
|
||
| mockAppService := mockapps.NewMockApplicationService(ctrl) | ||
| cmd := &createAppCommand{ | ||
| applicationService: mockAppService, | ||
| } | ||
|
|
||
| // Test with no arguments | ||
| context, err := components.ConvertContext(ctx) | ||
| assert.NoError(t, err) | ||
|
|
||
| err = cmd.prepareAndRunCommand(context) | ||
| assert.Error(t, err) | ||
| assert.Contains(t, err.Error(), "Wrong number of arguments") | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.