Skip to content

Commit e2e4ccb

Browse files
committed
Align the package-bind and package-unbind commands with the new design spec
1 parent b15a0a0 commit e2e4ccb

File tree

12 files changed

+119
-211
lines changed

12 files changed

+119
-211
lines changed

apptrust/commands/package/bind_package_cmd.go

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
type bindPackageCommand struct {
1818
packageService packages.PackageService
1919
serverDetails *coreConfig.ServerDetails
20+
applicationKey string
2021
requestPayload *model.BindPackageRequest
2122
}
2223

@@ -25,7 +26,7 @@ func (bp *bindPackageCommand) Run() error {
2526
if err != nil {
2627
return err
2728
}
28-
return bp.packageService.BindPackage(ctx, bp.requestPayload)
29+
return bp.packageService.BindPackage(ctx, bp.applicationKey, bp.requestPayload)
2930
}
3031

3132
func (bp *bindPackageCommand) ServerDetails() (*coreConfig.ServerDetails, error) {
@@ -46,14 +47,24 @@ func (bp *bindPackageCommand) prepareAndRunCommand(ctx *components.Context) erro
4647
if err != nil {
4748
return err
4849
}
49-
bp.requestPayload, err = BuildPackageRequestPayload(ctx)
50-
if err != nil {
51-
return err
52-
}
50+
bp.extractFromArgs(ctx)
5351

5452
return commonCLiCommands.Exec(bp)
5553
}
5654

55+
func (bp *bindPackageCommand) extractFromArgs(ctx *components.Context) {
56+
bp.applicationKey = ctx.Arguments[0]
57+
packageType := ctx.Arguments[1]
58+
packageName := ctx.Arguments[2]
59+
version := ctx.Arguments[3]
60+
61+
bp.requestPayload = &model.BindPackageRequest{
62+
Type: packageType,
63+
Name: packageName,
64+
Version: version,
65+
}
66+
}
67+
5768
func GetBindPackageCommand(appContext app.Context) components.Command {
5869
cmd := &bindPackageCommand{packageService: appContext.GetPackageService()}
5970
return components.Command{
@@ -75,8 +86,8 @@ func GetBindPackageCommand(appContext app.Context) components.Command {
7586
Description: "Package name.",
7687
},
7788
{
78-
Name: "package-versions",
79-
Description: "Comma-separated versions of the package to bind (e.g., '1.0.0,1.1.0,1.2.0').",
89+
Name: "package-version",
90+
Description: "Package version.",
8091
},
8192
},
8293
Flags: commands.GetCommandFlags(commands.PackageBind),

apptrust/commands/package/bind_package_cmd_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,21 @@ func TestBindPackageCommand_Run(t *testing.T) {
1616
defer ctrl.Finish()
1717

1818
serverDetails := &config.ServerDetails{Url: "https://example.com"}
19+
applicationKey := "app-key"
1920
requestPayload := &model.BindPackageRequest{
20-
ApplicationKey: "app-key",
21-
Type: "npm",
22-
Name: "test-package",
23-
Versions: []string{"1.0.0"},
21+
Type: "npm",
22+
Name: "test-package",
23+
Version: "1.0.0",
2424
}
2525

2626
mockPackageService := mockpackages.NewMockPackageService(ctrl)
27-
mockPackageService.EXPECT().BindPackage(gomock.Any(), requestPayload).
27+
mockPackageService.EXPECT().BindPackage(gomock.Any(), applicationKey, requestPayload).
2828
Return(nil).Times(1)
2929

3030
cmd := &bindPackageCommand{
3131
packageService: mockPackageService,
3232
serverDetails: serverDetails,
33+
applicationKey: applicationKey,
3334
requestPayload: requestPayload,
3435
}
3536

@@ -42,20 +43,21 @@ func TestBindPackageCommand_Run_Error(t *testing.T) {
4243
defer ctrl.Finish()
4344

4445
serverDetails := &config.ServerDetails{Url: "https://example.com"}
46+
applicationKey := "app-key"
4547
requestPayload := &model.BindPackageRequest{
46-
ApplicationKey: "app-key",
47-
Type: "npm",
48-
Name: "test-package",
49-
Versions: []string{"1.0.0"},
48+
Type: "npm",
49+
Name: "test-package",
50+
Version: "1.0.0",
5051
}
5152

5253
mockPackageService := mockpackages.NewMockPackageService(ctrl)
53-
mockPackageService.EXPECT().BindPackage(gomock.Any(), requestPayload).
54+
mockPackageService.EXPECT().BindPackage(gomock.Any(), applicationKey, requestPayload).
5455
Return(errors.New("bind error")).Times(1)
5556

5657
cmd := &bindPackageCommand{
5758
packageService: mockPackageService,
5859
serverDetails: serverDetails,
60+
applicationKey: applicationKey,
5961
requestPayload: requestPayload,
6062
}
6163

apptrust/commands/package/unbind_package_cmd.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"github.com/jfrog/jfrog-cli-application/apptrust/commands"
66
"github.com/jfrog/jfrog-cli-application/apptrust/commands/utils"
77
"github.com/jfrog/jfrog-cli-application/apptrust/common"
8-
"github.com/jfrog/jfrog-cli-application/apptrust/model"
98
"github.com/jfrog/jfrog-cli-application/apptrust/service"
109
"github.com/jfrog/jfrog-cli-application/apptrust/service/packages"
1110
commonCLiCommands "github.com/jfrog/jfrog-cli-core/v2/common/commands"
@@ -17,15 +16,18 @@ import (
1716
type unbindPackageCommand struct {
1817
packageService packages.PackageService
1918
serverDetails *coreConfig.ServerDetails
20-
requestPayload *model.BindPackageRequest
19+
applicationKey string
20+
packageType string
21+
packageName string
22+
packageVersion string
2123
}
2224

2325
func (up *unbindPackageCommand) Run() error {
2426
ctx, err := service.NewContext(*up.serverDetails)
2527
if err != nil {
2628
return err
2729
}
28-
return up.packageService.UnbindPackage(ctx, up.requestPayload)
30+
return up.packageService.UnbindPackage(ctx, up.applicationKey, up.packageType, up.packageName, up.packageVersion)
2931
}
3032

3133
func (up *unbindPackageCommand) ServerDetails() (*coreConfig.ServerDetails, error) {
@@ -37,7 +39,7 @@ func (up *unbindPackageCommand) CommandName() string {
3739
}
3840

3941
func (up *unbindPackageCommand) prepareAndRunCommand(ctx *components.Context) error {
40-
if len(ctx.Arguments) < 3 || len(ctx.Arguments) > 4 {
42+
if len(ctx.Arguments) != 4 {
4143
return pluginsCommon.WrongNumberOfArgumentsHandler(ctx)
4244
}
4345

@@ -46,10 +48,12 @@ func (up *unbindPackageCommand) prepareAndRunCommand(ctx *components.Context) er
4648
if err != nil {
4749
return err
4850
}
49-
up.requestPayload, err = BuildPackageRequestPayload(ctx)
50-
if err != nil {
51-
return err
52-
}
51+
52+
// Extract from arguments
53+
up.applicationKey = ctx.Arguments[0]
54+
up.packageType = ctx.Arguments[1]
55+
up.packageName = ctx.Arguments[2]
56+
up.packageVersion = ctx.Arguments[3]
5357

5458
return commonCLiCommands.Exec(up)
5559
}
@@ -75,8 +79,8 @@ func GetUnbindPackageCommand(appContext app.Context) components.Command {
7579
Description: "Package name.",
7680
},
7781
{
78-
Name: "package-versions",
79-
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.",
82+
Name: "package-version",
83+
Description: "Package version.",
8084
},
8185
},
8286
Flags: commands.GetCommandFlags(commands.PackageUnbind),

apptrust/commands/package/unbind_package_cmd_test.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"errors"
55
"testing"
66

7-
"github.com/jfrog/jfrog-cli-application/apptrust/model"
87
mockpackages "github.com/jfrog/jfrog-cli-application/apptrust/service/packages/mocks"
98
"github.com/jfrog/jfrog-cli-core/v2/utils/config"
109
"github.com/stretchr/testify/assert"
@@ -16,21 +15,22 @@ func TestUnbindPackageCommand_Run(t *testing.T) {
1615
defer ctrl.Finish()
1716

1817
serverDetails := &config.ServerDetails{Url: "https://example.com"}
19-
requestPayload := &model.BindPackageRequest{
20-
ApplicationKey: "app-key",
21-
Type: "npm",
22-
Name: "test-package",
23-
Versions: []string{"1.0.0"},
24-
}
18+
applicationKey := "app-key"
19+
pkgType := "npm"
20+
pkgName := "test-package"
21+
pkgVersion := "1.0.0"
2522

2623
mockPackageService := mockpackages.NewMockPackageService(ctrl)
27-
mockPackageService.EXPECT().UnbindPackage(gomock.Any(), requestPayload).
24+
mockPackageService.EXPECT().UnbindPackage(gomock.Any(), applicationKey, pkgType, pkgName, pkgVersion).
2825
Return(nil).Times(1)
2926

3027
cmd := &unbindPackageCommand{
3128
packageService: mockPackageService,
3229
serverDetails: serverDetails,
33-
requestPayload: requestPayload,
30+
applicationKey: applicationKey,
31+
packageType: pkgType,
32+
packageName: pkgName,
33+
packageVersion: pkgVersion,
3434
}
3535

3636
err := cmd.Run()
@@ -42,21 +42,22 @@ func TestUnbindPackageCommand_Run_Error(t *testing.T) {
4242
defer ctrl.Finish()
4343

4444
serverDetails := &config.ServerDetails{Url: "https://example.com"}
45-
requestPayload := &model.BindPackageRequest{
46-
ApplicationKey: "app-key",
47-
Type: "npm",
48-
Name: "test-package",
49-
Versions: []string{"1.0.0"},
50-
}
45+
applicationKey := "app-key"
46+
pkgType := "npm"
47+
pkgName := "test-package"
48+
pkgVersion := "1.0.0"
5149

5250
mockPackageService := mockpackages.NewMockPackageService(ctrl)
53-
mockPackageService.EXPECT().UnbindPackage(gomock.Any(), requestPayload).
51+
mockPackageService.EXPECT().UnbindPackage(gomock.Any(), applicationKey, pkgType, pkgName, pkgVersion).
5452
Return(errors.New("unbind error")).Times(1)
5553

5654
cmd := &unbindPackageCommand{
5755
packageService: mockPackageService,
5856
serverDetails: serverDetails,
59-
requestPayload: requestPayload,
57+
applicationKey: applicationKey,
58+
packageType: pkgType,
59+
packageName: pkgName,
60+
packageVersion: pkgVersion,
6061
}
6162

6263
err := cmd.Run()

apptrust/commands/package/utils.go

Lines changed: 0 additions & 44 deletions
This file was deleted.

apptrust/commands/package/utils_test.go

Lines changed: 0 additions & 61 deletions
This file was deleted.

apptrust/http/http_client.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type ApptrustHttpClient interface {
2626
Post(path string, requestBody interface{}, params map[string]string) (resp *http.Response, body []byte, err error)
2727
Get(path string) (resp *http.Response, body []byte, err error)
2828
Patch(path string, requestBody interface{}) (resp *http.Response, body []byte, err error)
29-
Delete(path string, requestBody interface{}) (resp *http.Response, body []byte, err error)
29+
Delete(path string) (resp *http.Response, body []byte, err error)
3030
}
3131

3232
type apptrustHttpClient struct {
@@ -139,22 +139,14 @@ func (c *apptrustHttpClient) toJsonBytes(payload interface{}) ([]byte, error) {
139139
return jsonBytes, nil
140140
}
141141

142-
func (c *apptrustHttpClient) Delete(path string, requestBody interface{}) (resp *http.Response, body []byte, err error) {
142+
func (c *apptrustHttpClient) Delete(path string) (resp *http.Response, body []byte, err error) {
143143
url, err := utils.BuildUrl(c.serverDetails.Url, apptrustApiPath+path, nil)
144144
if err != nil {
145145
return nil, nil, err
146146
}
147147

148-
var requestContent []byte
149-
if requestBody != nil {
150-
requestContent, err = c.toJsonBytes(requestBody)
151-
if err != nil {
152-
return nil, nil, err
153-
}
154-
}
155-
156148
log.Debug("Sending DELETE request to:", url)
157-
return c.client.SendDelete(url, requestContent, c.getJsonHttpClientDetails())
149+
return c.client.SendDelete(url, nil, c.getJsonHttpClientDetails())
158150
}
159151

160152
func (c *apptrustHttpClient) getJsonHttpClientDetails() *httputils.HttpClientDetails {
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package model
22

33
type BindPackageRequest struct {
4-
ApplicationKey string `json:"application_key"`
5-
Type string `json:"type"`
6-
Name string `json:"name"`
7-
Versions []string `json:"versions,omitempty"`
4+
Type string `json:"package_type"`
5+
Name string `json:"package_name"`
6+
Version string `json:"package_version"`
87
}

0 commit comments

Comments
 (0)