-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: package list feat: package versions
- Loading branch information
Showing
16 changed files
with
1,255 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package list | ||
|
||
import ( | ||
"github.com/MakeNowJust/heredoc/v2" | ||
"github.com/OctopusDeploy/cli/pkg/factory" | ||
"github.com/OctopusDeploy/cli/pkg/output" | ||
"github.com/OctopusDeploy/cli/pkg/util/flag" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/packages" | ||
"github.com/spf13/cobra" | ||
"math" | ||
) | ||
|
||
const ( | ||
FlagLimit = "limit" | ||
FlagFilter = "filter" | ||
) | ||
|
||
type ListFlags struct { | ||
Limit *flag.Flag[int32] | ||
Filter *flag.Flag[string] | ||
} | ||
|
||
func NewListFlags() *ListFlags { | ||
return &ListFlags{ | ||
Limit: flag.New[int32](FlagLimit, false), | ||
Filter: flag.New[string](FlagFilter, false), | ||
} | ||
} | ||
|
||
func NewCmdList(f factory.Factory) *cobra.Command { | ||
listFlags := NewListFlags() | ||
|
||
cmd := &cobra.Command{ | ||
Use: "list", | ||
Short: "List packages in Octopus Deploy", | ||
Long: "List packages in Octopus Deploy.", | ||
Example: heredoc.Doc(` | ||
$ octopus package list | ||
$ octopus package list --limit 50 --filter SomePackage | ||
$ octopus package ls -n 30 -q SomePackage | ||
`), | ||
Aliases: []string{"ls"}, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
|
||
return listRun(cmd, f, listFlags) | ||
}, | ||
} | ||
|
||
flags := cmd.Flags() | ||
flags.Int32VarP(&listFlags.Limit.Value, listFlags.Limit.Name, "n", 0, "limit the maximum number of results that will be returned") | ||
flags.StringVarP(&listFlags.Filter.Value, listFlags.Filter.Name, "q", "", "filter packages to match only ones that contain the given string") | ||
return cmd | ||
} | ||
|
||
type PackageViewModel struct { | ||
ID string | ||
Version string | ||
Description string | ||
} | ||
|
||
func listRun(cmd *cobra.Command, f factory.Factory, flags *ListFlags) error { | ||
limit := flags.Limit.Value | ||
filter := flags.Filter.Value | ||
|
||
octopus, err := f.GetSpacedClient() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// the underlying API has skip, take and paginated results, and will return 30 packages by default. | ||
// this kind of behaviour isn't going to be the expected default for a CLI, so we instead default to | ||
// returning "everything" if a limit is unspecified | ||
if limit <= 0 { | ||
limit = math.MaxInt32 | ||
} | ||
|
||
page, err := packages.List(octopus, f.GetCurrentSpace().ID, filter, int(limit)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return output.PrintArray(page.Items, cmd, output.Mappers[*packages.Package]{ | ||
Json: func(item *packages.Package) any { | ||
return PackageViewModel{ | ||
ID: item.PackageID, | ||
Version: item.Version, | ||
Description: item.Description, | ||
} | ||
}, | ||
Table: output.TableDefinition[*packages.Package]{ | ||
Header: []string{"ID", "HIGHEST VERSION", "DESCRIPTION"}, | ||
Row: func(item *packages.Package) []string { | ||
return []string{item.PackageID, item.Version, item.Description} | ||
}}, | ||
Basic: func(item *packages.Package) string { | ||
return item.PackageID | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package list_test | ||
|
||
import ( | ||
"bytes" | ||
"github.com/MakeNowJust/heredoc/v2" | ||
cmdRoot "github.com/OctopusDeploy/cli/pkg/cmd/root" | ||
"github.com/OctopusDeploy/cli/test/fixtures" | ||
"github.com/OctopusDeploy/cli/test/testutil" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/packages" | ||
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/resources" | ||
"github.com/spf13/cobra" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
var rootResource = testutil.NewRootResource() | ||
|
||
func TestPackageList(t *testing.T) { | ||
const spaceID = "Spaces-1" | ||
space1 := fixtures.NewSpace(spaceID, "Default Space") | ||
|
||
tests := []struct { | ||
name string | ||
run func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) | ||
}{ | ||
{"list all packages by default", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { | ||
cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { | ||
defer api.Close() | ||
rootCmd.SetArgs([]string{"package", "list"}) | ||
return rootCmd.ExecuteC() | ||
}) | ||
|
||
api.ExpectRequest(t, "GET", "/api").RespondWith(rootResource) | ||
api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource) | ||
|
||
api.ExpectRequest(t, "GET", "/api/Spaces-1/packages?take=2147483647").RespondWith(&resources.Resources[*packages.Package]{ | ||
Items: []*packages.Package{ | ||
{PackageID: "pterm", Version: "0.12.51", Description: "some sort of package"}, | ||
{PackageID: "NuGet.CommandLine", Version: "6.2.1"}, | ||
}, | ||
}) | ||
|
||
_, err := testutil.ReceivePair(cmdReceiver) | ||
assert.Nil(t, err) | ||
assert.Equal(t, heredoc.Doc(` | ||
ID HIGHEST VERSION DESCRIPTION | ||
pterm 0.12.51 some sort of package | ||
NuGet.CommandLine 6.2.1 | ||
`), stdOut.String()) | ||
assert.Equal(t, "", stdErr.String()) | ||
}}, | ||
|
||
{"pass through filter and limit", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { | ||
cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { | ||
defer api.Close() | ||
rootCmd.SetArgs([]string{"package", "list", "--filter", "pterm", "--limit", "1"}) | ||
return rootCmd.ExecuteC() | ||
}) | ||
|
||
api.ExpectRequest(t, "GET", "/api").RespondWith(rootResource) | ||
api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource) | ||
|
||
api.ExpectRequest(t, "GET", "/api/Spaces-1/packages?filter=pterm&take=1").RespondWith(&resources.Resources[*packages.Package]{ | ||
Items: []*packages.Package{ | ||
{PackageID: "pterm", Version: "0.12.51", Description: "some sort of package"}, | ||
}, | ||
}) | ||
|
||
_, err := testutil.ReceivePair(cmdReceiver) | ||
assert.Nil(t, err) | ||
assert.Equal(t, heredoc.Doc(` | ||
ID HIGHEST VERSION DESCRIPTION | ||
pterm 0.12.51 some sort of package | ||
`), stdOut.String()) | ||
assert.Equal(t, "", stdErr.String()) | ||
}}, | ||
|
||
{"outputformat json", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { | ||
cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { | ||
defer api.Close() | ||
rootCmd.SetArgs([]string{"package", "list", "--output-format", "json"}) | ||
return rootCmd.ExecuteC() | ||
}) | ||
|
||
api.ExpectRequest(t, "GET", "/api").RespondWith(rootResource) | ||
api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource) | ||
|
||
api.ExpectRequest(t, "GET", "/api/Spaces-1/packages?take=2147483647").RespondWith(&resources.Resources[*packages.Package]{ | ||
Items: []*packages.Package{ | ||
{PackageID: "pterm", Version: "0.12.51", Description: "some sort of package"}, | ||
{PackageID: "NuGet.CommandLine", Version: "6.2.1"}, | ||
}, | ||
}) | ||
|
||
_, err := testutil.ReceivePair(cmdReceiver) | ||
assert.Nil(t, err) | ||
|
||
type x struct { | ||
ID string | ||
Version string | ||
Description string | ||
} | ||
parsedStdout, err := testutil.ParseJsonStrict[[]x](stdOut) | ||
assert.Nil(t, err) | ||
|
||
assert.Equal(t, []x{ | ||
{ID: "pterm", Version: "0.12.51", Description: "some sort of package"}, | ||
{ID: "NuGet.CommandLine", Version: "6.2.1"}, | ||
}, parsedStdout) | ||
|
||
assert.Equal(t, "", stdErr.String()) | ||
}}, | ||
|
||
{"outputformat basic", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { | ||
cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { | ||
defer api.Close() | ||
rootCmd.SetArgs([]string{"package", "list", "--output-format", "basic"}) | ||
return rootCmd.ExecuteC() | ||
}) | ||
|
||
api.ExpectRequest(t, "GET", "/api").RespondWith(rootResource) | ||
api.ExpectRequest(t, "GET", "/api/Spaces-1").RespondWith(rootResource) | ||
|
||
api.ExpectRequest(t, "GET", "/api/Spaces-1/packages?take=2147483647").RespondWith(&resources.Resources[*packages.Package]{ | ||
Items: []*packages.Package{ | ||
{PackageID: "pterm", Version: "0.12.51", Description: "some sort of package"}, | ||
{PackageID: "NuGet.CommandLine", Version: "6.2.1"}, | ||
}, | ||
}) | ||
|
||
_, err := testutil.ReceivePair(cmdReceiver) | ||
assert.Nil(t, err) | ||
assert.Equal(t, heredoc.Doc(` | ||
pterm | ||
NuGet.CommandLine | ||
`), stdOut.String()) | ||
assert.Equal(t, "", stdErr.String()) | ||
}}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
stdout, stderr := &bytes.Buffer{}, &bytes.Buffer{} | ||
api := testutil.NewMockHttpServer() | ||
fac := testutil.NewMockFactoryWithSpaceAndPrompt(api, space1, nil) | ||
rootCmd := cmdRoot.NewCmdRoot(fac, nil, nil) | ||
rootCmd.SetOut(stdout) | ||
rootCmd.SetErr(stderr) | ||
test.run(t, api, rootCmd, stdout, stderr) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package _package | ||
|
||
import ( | ||
"fmt" | ||
cmdList "github.com/OctopusDeploy/cli/pkg/cmd/package/list" | ||
cmdUpload "github.com/OctopusDeploy/cli/pkg/cmd/package/upload" | ||
cmdVersions "github.com/OctopusDeploy/cli/pkg/cmd/package/versions" | ||
"github.com/OctopusDeploy/cli/pkg/constants" | ||
"github.com/OctopusDeploy/cli/pkg/constants/annotations" | ||
"github.com/OctopusDeploy/cli/pkg/factory" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func NewCmdPackage(f factory.Factory) *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "package <command>", | ||
Short: "Manage packages", | ||
Long: `Work with Octopus Deploy packages.`, | ||
Example: fmt.Sprintf("$ %s package upload", constants.ExecutableName), | ||
Annotations: map[string]string{ | ||
annotations.IsCore: "true", | ||
}, | ||
} | ||
|
||
cmd.AddCommand(cmdUpload.NewCmdUpload(f)) | ||
cmd.AddCommand(cmdList.NewCmdList(f)) | ||
cmd.AddCommand(cmdVersions.NewCmdVersions(f)) | ||
return cmd | ||
} |
Oops, something went wrong.