Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions apptrust/app/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ package app

import (
"github.com/jfrog/jfrog-cli-application/apptrust/service/applications"
"github.com/jfrog/jfrog-cli-application/apptrust/service/packages"
"github.com/jfrog/jfrog-cli-application/apptrust/service/systems"
"github.com/jfrog/jfrog-cli-application/apptrust/service/versions"
)

type Context interface {
GetApplicationService() applications.ApplicationService
GetVersionService() versions.VersionService
GetPackageService() packages.PackageService
GetSystemService() systems.SystemService
GetConfig() interface{}
}

type context struct {
applicationService applications.ApplicationService
versionService versions.VersionService
packageService packages.PackageService
systemService systems.SystemService
}

func NewAppContext() Context {
return &context{
applicationService: applications.NewApplicationService(),
versionService: versions.NewVersionService(),
packageService: packages.NewPackageService(),
systemService: systems.NewSystemService(),
}
}
Expand All @@ -35,6 +39,10 @@ func (c *context) GetVersionService() versions.VersionService {
return c.versionService
}

func (c *context) GetPackageService() packages.PackageService {
return c.packageService
}

func (c *context) GetSystemService() systems.SystemService {
return c.systemService
}
Expand Down
23 changes: 23 additions & 0 deletions apptrust/commands/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const (
CreateAppVersion = "version-create"
PromoteAppVersion = "version-promote"
DeleteAppVersion = "version-delete"
PackageBind = "package-bind"
PackageUnbind = "package-unbind"
CreateApp = "app-create"
UpdateApp = "app-update"
DeleteApp = "app-delete"
Expand Down Expand Up @@ -98,6 +100,20 @@ var commandFlags = map[string][]string{
serverId,
},

PackageBind: {
url,
user,
accessToken,
serverId,
},

PackageUnbind: {
url,
user,
accessToken,
serverId,
},

Ping: {
url,
user,
Expand Down Expand Up @@ -139,6 +155,13 @@ var commandFlags = map[string][]string{
SpecFlag,
SpecVarsFlag,
},

DeleteApp: {
url,
user,
accessToken,
serverId,
},
}

func GetCommandFlags(cmdKey string) []components.Flag {
Expand Down
85 changes: 85 additions & 0 deletions apptrust/commands/package/bind_package_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package packagecmds

import (
"github.com/jfrog/jfrog-cli-application/apptrust/app"
"github.com/jfrog/jfrog-cli-application/apptrust/commands"
"github.com/jfrog/jfrog-cli-application/apptrust/commands/utils"
"github.com/jfrog/jfrog-cli-application/apptrust/common"
"github.com/jfrog/jfrog-cli-application/apptrust/model"
"github.com/jfrog/jfrog-cli-application/apptrust/service"
"github.com/jfrog/jfrog-cli-application/apptrust/service/packages"
commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands"
pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common"
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
)

type bindPackageCommand struct {
packageService packages.PackageService
serverDetails *coreConfig.ServerDetails
requestPayload *model.BindPackageRequest
}

func (bp *bindPackageCommand) Run() error {
ctx, err := service.NewContext(*bp.serverDetails)
if err != nil {
return err
}
return bp.packageService.BindPackage(ctx, bp.requestPayload)
}

func (bp *bindPackageCommand) ServerDetails() (*coreConfig.ServerDetails, error) {
return bp.serverDetails, nil
}

func (bp *bindPackageCommand) CommandName() string {
return commands.PackageBind
}

func (bp *bindPackageCommand) prepareAndRunCommand(ctx *components.Context) error {
if len(ctx.Arguments) != 4 {
return pluginsCommon.WrongNumberOfArgumentsHandler(ctx)
}

var err error
bp.serverDetails, err = utils.ServerDetailsByFlags(ctx)
if err != nil {
return err
}
bp.requestPayload, err = BuildPackageRequestPayload(ctx)
if err != nil {
return err
}

return commonCLiCommands.Exec(bp)
}

func GetBindPackageCommand(appContext app.Context) components.Command {
cmd := &bindPackageCommand{packageService: appContext.GetPackageService()}
return components.Command{
Name: commands.PackageBind,
Description: "Bind packages to an application",
Category: common.CategoryPackage,
Aliases: []string{"pb"},
Arguments: []components.Argument{
{
Name: "application-key",
Description: "The key of the application to bind the package to.",
},
{
Name: "package-type",
Description: "Package type (e.g., npm, docker, maven, generic).",
},
{
Name: "package-name",
Description: "Package name.",
},
{
Name: "package-versions",
Description: "Comma-separated versions of the package to bind (e.g., '1.0.0,1.1.0,1.2.0').",
},
},
Flags: commands.GetCommandFlags(commands.PackageBind),
Action: cmd.prepareAndRunCommand,
}
}
65 changes: 65 additions & 0 deletions apptrust/commands/package/bind_package_cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package packagecmds

import (
"errors"
"testing"

"github.com/jfrog/jfrog-cli-application/apptrust/model"
mockpackages "github.com/jfrog/jfrog-cli-application/apptrust/service/packages/mocks"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

func TestBindPackageCommand_Run(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

serverDetails := &config.ServerDetails{Url: "https://example.com"}
requestPayload := &model.BindPackageRequest{
ApplicationKey: "app-key",
Type: "npm",
Name: "test-package",
Versions: []string{"1.0.0"},
}

mockPackageService := mockpackages.NewMockPackageService(ctrl)
mockPackageService.EXPECT().BindPackage(gomock.Any(), requestPayload).
Return(nil).Times(1)

cmd := &bindPackageCommand{
packageService: mockPackageService,
serverDetails: serverDetails,
requestPayload: requestPayload,
}

err := cmd.Run()
assert.NoError(t, err)
}

func TestBindPackageCommand_Run_Error(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

serverDetails := &config.ServerDetails{Url: "https://example.com"}
requestPayload := &model.BindPackageRequest{
ApplicationKey: "app-key",
Type: "npm",
Name: "test-package",
Versions: []string{"1.0.0"},
}

mockPackageService := mockpackages.NewMockPackageService(ctrl)
mockPackageService.EXPECT().BindPackage(gomock.Any(), requestPayload).
Return(errors.New("bind error")).Times(1)

cmd := &bindPackageCommand{
packageService: mockPackageService,
serverDetails: serverDetails,
requestPayload: requestPayload,
}

err := cmd.Run()
assert.Error(t, err)
assert.Equal(t, "bind error", err.Error())
}
85 changes: 85 additions & 0 deletions apptrust/commands/package/unbind_package_cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package packagecmds

import (
"github.com/jfrog/jfrog-cli-application/apptrust/app"
"github.com/jfrog/jfrog-cli-application/apptrust/commands"
"github.com/jfrog/jfrog-cli-application/apptrust/commands/utils"
"github.com/jfrog/jfrog-cli-application/apptrust/common"
"github.com/jfrog/jfrog-cli-application/apptrust/model"
"github.com/jfrog/jfrog-cli-application/apptrust/service"
"github.com/jfrog/jfrog-cli-application/apptrust/service/packages"
commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands"
pluginsCommon "github.com/jfrog/jfrog-cli-core/v2/plugins/common"
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
coreConfig "github.com/jfrog/jfrog-cli-core/v2/utils/config"
)

type unbindPackageCommand struct {
packageService packages.PackageService
serverDetails *coreConfig.ServerDetails
requestPayload *model.BindPackageRequest
}

func (up *unbindPackageCommand) Run() error {
ctx, err := service.NewContext(*up.serverDetails)
if err != nil {
return err
}
return up.packageService.UnbindPackage(ctx, up.requestPayload)
}

func (up *unbindPackageCommand) ServerDetails() (*coreConfig.ServerDetails, error) {
return up.serverDetails, nil
}

func (up *unbindPackageCommand) CommandName() string {
return commands.PackageUnbind
}

func (up *unbindPackageCommand) prepareAndRunCommand(ctx *components.Context) error {
if len(ctx.Arguments) < 3 || len(ctx.Arguments) > 4 {
return pluginsCommon.WrongNumberOfArgumentsHandler(ctx)
}

var err error
up.serverDetails, err = utils.ServerDetailsByFlags(ctx)
if err != nil {
return err
}
up.requestPayload, err = BuildPackageRequestPayload(ctx)
if err != nil {
return err
}

return commonCLiCommands.Exec(up)
}

func GetUnbindPackageCommand(appContext app.Context) components.Command {
cmd := &unbindPackageCommand{packageService: appContext.GetPackageService()}
return components.Command{
Name: commands.PackageUnbind,
Description: "Unbind packages from an application",
Category: common.CategoryPackage,
Aliases: []string{"pu"},
Arguments: []components.Argument{
{
Name: "application-key",
Description: "The key of the application to unbind the package from.",
},
{
Name: "package-type",
Description: "Package type (e.g., npm, docker, maven, generic).",
},
{
Name: "package-name",
Description: "Package name.",
},
{
Name: "package-versions",
Description: "Comma-separated versions of the package to unbind (e.g., '1.0.0,1.1.0,1.2.0'). If omitted, all versions will be unbound.",
},
},
Flags: commands.GetCommandFlags(commands.PackageUnbind),
Action: cmd.prepareAndRunCommand,
}
}
65 changes: 65 additions & 0 deletions apptrust/commands/package/unbind_package_cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package packagecmds

import (
"errors"
"testing"

"github.com/jfrog/jfrog-cli-application/apptrust/model"
mockpackages "github.com/jfrog/jfrog-cli-application/apptrust/service/packages/mocks"
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
"github.com/stretchr/testify/assert"
"go.uber.org/mock/gomock"
)

func TestUnbindPackageCommand_Run(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

serverDetails := &config.ServerDetails{Url: "https://example.com"}
requestPayload := &model.BindPackageRequest{
ApplicationKey: "app-key",
Type: "npm",
Name: "test-package",
Versions: []string{"1.0.0"},
}

mockPackageService := mockpackages.NewMockPackageService(ctrl)
mockPackageService.EXPECT().UnbindPackage(gomock.Any(), requestPayload).
Return(nil).Times(1)

cmd := &unbindPackageCommand{
packageService: mockPackageService,
serverDetails: serverDetails,
requestPayload: requestPayload,
}

err := cmd.Run()
assert.NoError(t, err)
}

func TestUnbindPackageCommand_Run_Error(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

serverDetails := &config.ServerDetails{Url: "https://example.com"}
requestPayload := &model.BindPackageRequest{
ApplicationKey: "app-key",
Type: "npm",
Name: "test-package",
Versions: []string{"1.0.0"},
}

mockPackageService := mockpackages.NewMockPackageService(ctrl)
mockPackageService.EXPECT().UnbindPackage(gomock.Any(), requestPayload).
Return(errors.New("unbind error")).Times(1)

cmd := &unbindPackageCommand{
packageService: mockPackageService,
serverDetails: serverDetails,
requestPayload: requestPayload,
}

err := cmd.Run()
assert.Error(t, err)
assert.Equal(t, "unbind error", err.Error())
}
Loading
Loading