|
| 1 | +package packagecmds |
| 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/model" |
| 8 | + "github.com/jfrog/jfrog-cli-application/apptrust/service" |
| 9 | + "github.com/jfrog/jfrog-cli-application/apptrust/service/packages" |
| 10 | + commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands" |
| 11 | + pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common" |
| 12 | + "github.com/jfrog/jfrog-cli-core/v2/plugins/components" |
| 13 | + coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config" |
| 14 | +) |
| 15 | + |
| 16 | +type bindPackageCommand struct { |
| 17 | + packageService packages.PackageService |
| 18 | + serverDetails *coreConfig.ServerDetails |
| 19 | + requestPayload *model.BindPackageRequest |
| 20 | +} |
| 21 | + |
| 22 | +func (bp *bindPackageCommand) Run() error { |
| 23 | + ctx, err := service.NewContext(*bp.serverDetails) |
| 24 | + if err != nil { |
| 25 | + return err |
| 26 | + } |
| 27 | + return bp.packageService.BindPackage(ctx, bp.requestPayload) |
| 28 | +} |
| 29 | + |
| 30 | +func (bp *bindPackageCommand) ServerDetails() (*coreConfig.ServerDetails, error) { |
| 31 | + return bp.serverDetails, nil |
| 32 | +} |
| 33 | + |
| 34 | +func (bp *bindPackageCommand) CommandName() string { |
| 35 | + return commands.PackageBind |
| 36 | +} |
| 37 | + |
| 38 | +func (bp *bindPackageCommand) prepareAndRunCommand(ctx *components.Context) error { |
| 39 | + if len(ctx.Arguments) != 4 { |
| 40 | + return pluginsCommon.WrongNumberOfArgumentsHandler(ctx) |
| 41 | + } |
| 42 | + |
| 43 | + var err error |
| 44 | + bp.serverDetails, err = utils.ServerDetailsByFlags(ctx) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + bp.requestPayload, err = bp.buildRequestPayload(ctx) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + |
| 53 | + return commonCLiCommands.Exec(bp) |
| 54 | +} |
| 55 | + |
| 56 | +func (bp *bindPackageCommand) buildRequestPayload(ctx *components.Context) (*model.BindPackageRequest, error) { |
| 57 | + applicationKey := ctx.Arguments[0] |
| 58 | + packageType := ctx.Arguments[1] |
| 59 | + packageName := ctx.Arguments[2] |
| 60 | + packageVersion := ctx.Arguments[3] |
| 61 | + |
| 62 | + return &model.BindPackageRequest{ |
| 63 | + ApplicationKey: applicationKey, |
| 64 | + Type: packageType, |
| 65 | + Name: packageName, |
| 66 | + Version: packageVersion, |
| 67 | + }, nil |
| 68 | +} |
| 69 | + |
| 70 | +func GetBindPackageCommand(appContext app.Context) components.Command { |
| 71 | + cmd := &bindPackageCommand{packageService: appContext.GetPackageService()} |
| 72 | + return components.Command{ |
| 73 | + Name: commands.PackageBind, |
| 74 | + Description: "Bind packages to an application version.", |
| 75 | + Aliases: []string{"pb"}, |
| 76 | + Arguments: []components.Argument{ |
| 77 | + { |
| 78 | + Name: "application-key", |
| 79 | + Description: "The key of the application to bind the package to.", |
| 80 | + }, |
| 81 | + { |
| 82 | + Name: "package-type", |
| 83 | + Description: "Package type (e.g., npm, docker, maven, generic).", |
| 84 | + }, |
| 85 | + { |
| 86 | + Name: "package-name", |
| 87 | + Description: "Package name.", |
| 88 | + }, |
| 89 | + { |
| 90 | + Name: "package-version", |
| 91 | + Description: "Package version.", |
| 92 | + }, |
| 93 | + }, |
| 94 | + Flags: commands.GetCommandFlags(commands.PackageBind), |
| 95 | + Action: cmd.prepareAndRunCommand, |
| 96 | + } |
| 97 | +} |
0 commit comments