Skip to content

Commit a9cb95c

Browse files
committed
Commands for binding and unbinding packages to applications
1 parent 6977717 commit a9cb95c

File tree

5 files changed

+91
-9
lines changed

5 files changed

+91
-9
lines changed

apptrust/commands/package/bind_package_cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ func GetBindPackageCommand(appContext app.Context) components.Command {
7373
Description: "Package name.",
7474
},
7575
{
76-
Name: "package-version",
77-
Description: "Package version.",
76+
Name: "package-versions",
77+
Description: "Comma-separated versions of the package to bind (e.g., '1.0.0,1.1.0,1.2.0').",
7878
},
7979
},
8080
Flags: commands.GetCommandFlags(commands.PackageBind),

apptrust/commands/package/unbind_package_cmd.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (up *unbindPackageCommand) CommandName() string {
3636
}
3737

3838
func (up *unbindPackageCommand) prepareAndRunCommand(ctx *components.Context) error {
39-
if len(ctx.Arguments) != 4 {
39+
if len(ctx.Arguments) < 3 || len(ctx.Arguments) > 4 {
4040
return pluginsCommon.WrongNumberOfArgumentsHandler(ctx)
4141
}
4242

@@ -73,8 +73,8 @@ func GetUnbindPackageCommand(appContext app.Context) components.Command {
7373
Description: "Package name.",
7474
},
7575
{
76-
Name: "package-version",
77-
Description: "Package version.",
76+
Name: "package-versions",
77+
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.",
7878
},
7979
},
8080
Flags: commands.GetCommandFlags(commands.PackageUnbind),

apptrust/commands/package/utils.go

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,44 @@
11
package packagecmds
22

33
import (
4+
"strings"
5+
46
"github.com/jfrog/jfrog-cli-application/apptrust/model"
57
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
68
)
79

810
// BuildPackageRequestPayload creates a BindPackageRequest from command arguments.
911
// This function is shared between bind and unbind package commands.
10-
// It expects the following arguments in order: <application_key> <package_type> <package_name> <package_version>.
12+
// It expects the following arguments in order: <application_key> <package_type> <package_name> [<package_versions>].
1113
func BuildPackageRequestPayload(ctx *components.Context) (*model.BindPackageRequest, error) {
1214
applicationKey := ctx.Arguments[0]
1315
packageType := ctx.Arguments[1]
1416
packageName := ctx.Arguments[2]
15-
packageVersion := ctx.Arguments[3]
17+
18+
var versions []string
19+
if len(ctx.Arguments) > 3 {
20+
// Parse comma-separated versions
21+
versions = parseVersions(ctx.Arguments[3])
22+
}
1623

1724
return &model.BindPackageRequest{
1825
ApplicationKey: applicationKey,
1926
Type: packageType,
2027
Name: packageName,
21-
Versions: []string{packageVersion},
28+
Versions: versions,
2229
}, nil
2330
}
31+
32+
// parseVersions parses a comma-separated string of versions into a slice.
33+
// It trims whitespaces from each version and filters out empty strings.
34+
func parseVersions(versionsString string) []string {
35+
parts := strings.Split(versionsString, ",")
36+
var versions []string
37+
for _, part := range parts {
38+
trimmed := strings.TrimSpace(part)
39+
if trimmed != "" {
40+
versions = append(versions, trimmed)
41+
}
42+
}
43+
return versions
44+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package packagecmds
2+
3+
import (
4+
"testing"
5+
6+
"github.com/jfrog/jfrog-cli-core/v2/plugins/components"
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestBuildPackageRequestPayload(t *testing.T) {
11+
tests := []struct {
12+
name string
13+
arguments []string
14+
expectedKey string
15+
expectedType string
16+
expectedName string
17+
expectedVersions []string
18+
}{
19+
{
20+
name: "Valid package request with single version",
21+
arguments: []string{"my-app", "npm", "my-package", "1.0.0"},
22+
expectedKey: "my-app",
23+
expectedType: "npm",
24+
expectedName: "my-package",
25+
expectedVersions: []string{"1.0.0"},
26+
},
27+
{
28+
name: "Valid package request with multiple versions",
29+
arguments: []string{"my-app", "npm", "my-package", "1.0.0,1.1.0 , 1.2.0"},
30+
expectedKey: "my-app",
31+
expectedType: "npm",
32+
expectedName: "my-package",
33+
expectedVersions: []string{"1.0.0", "1.1.0", "1.2.0"},
34+
},
35+
{
36+
name: "Package request without versions",
37+
arguments: []string{"my-app", "npm", "my-package"},
38+
expectedKey: "my-app",
39+
expectedType: "npm",
40+
expectedName: "my-package",
41+
expectedVersions: nil,
42+
},
43+
}
44+
45+
for _, tt := range tests {
46+
t.Run(tt.name, func(t *testing.T) {
47+
ctx := &components.Context{
48+
Arguments: tt.arguments,
49+
}
50+
51+
result, err := BuildPackageRequestPayload(ctx)
52+
53+
assert.NoError(t, err)
54+
assert.NotNil(t, result)
55+
assert.Equal(t, tt.expectedKey, result.ApplicationKey)
56+
assert.Equal(t, tt.expectedType, result.Type)
57+
assert.Equal(t, tt.expectedName, result.Name)
58+
assert.Equal(t, tt.expectedVersions, result.Versions)
59+
})
60+
}
61+
}

apptrust/model/bind_package_request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ type BindPackageRequest struct {
44
ApplicationKey string `json:"application_key"`
55
Type string `json:"type"`
66
Name string `json:"name"`
7-
Versions []string `json:"versions"`
7+
Versions []string `json:"versions,omitempty"`
88
}

0 commit comments

Comments
 (0)