From 021258dff1f62db0fefdc809387e8a05063a490b Mon Sep 17 00:00:00 2001 From: nate-christensen Date: Mon, 12 Aug 2024 08:36:26 -0500 Subject: [PATCH] feat: list all projects in any one group. --- pkg/cmd/projectgroup/project-group.go | 2 + pkg/cmd/projectgroup/projects/list/list.go | 102 +++++++++++++++++++++ pkg/cmd/projectgroup/projects/projects.go | 23 +++++ 3 files changed, 127 insertions(+) create mode 100644 pkg/cmd/projectgroup/projects/list/list.go create mode 100644 pkg/cmd/projectgroup/projects/projects.go diff --git a/pkg/cmd/projectgroup/project-group.go b/pkg/cmd/projectgroup/project-group.go index 311d52ed..2d886ab0 100644 --- a/pkg/cmd/projectgroup/project-group.go +++ b/pkg/cmd/projectgroup/project-group.go @@ -5,6 +5,7 @@ import ( createCmd "github.com/OctopusDeploy/cli/pkg/cmd/projectgroup/create" deleteCmd "github.com/OctopusDeploy/cli/pkg/cmd/projectgroup/delete" listCmd "github.com/OctopusDeploy/cli/pkg/cmd/projectgroup/list" + cmdListProjects "github.com/OctopusDeploy/cli/pkg/cmd/projectgroup/projects" viewCmd "github.com/OctopusDeploy/cli/pkg/cmd/projectgroup/view" "github.com/OctopusDeploy/cli/pkg/constants" "github.com/OctopusDeploy/cli/pkg/constants/annotations" @@ -30,6 +31,7 @@ func NewCmdProjectGroup(f factory.Factory) *cobra.Command { cmd.AddCommand(listCmd.NewCmdList(f)) cmd.AddCommand(deleteCmd.NewCmdList(f)) cmd.AddCommand(viewCmd.NewCmdView(f)) + cmd.AddCommand(cmdListProjects.NewCmdListProjects(f)) return cmd } diff --git a/pkg/cmd/projectgroup/projects/list/list.go b/pkg/cmd/projectgroup/projects/list/list.go new file mode 100644 index 00000000..9133cbd9 --- /dev/null +++ b/pkg/cmd/projectgroup/projects/list/list.go @@ -0,0 +1,102 @@ +package list + +import ( + "github.com/MakeNowJust/heredoc/v2" + "github.com/OctopusDeploy/cli/pkg/cmd" + "github.com/OctopusDeploy/cli/pkg/constants" + "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/projects" + "github.com/spf13/cobra" +) + +const ( + FlagGroup = "group" +) + +type ListFlags struct { + Group *flag.Flag[string] +} + +func NewListFlags() *ListFlags { + return &ListFlags{ + Group: flag.New[string](FlagGroup, false), + } +} + +type ListOptions struct { + *ListFlags + Command *cobra.Command + *cmd.Dependencies +} + +func NewListOptions(flags *ListFlags, command *cobra.Command, dependencies *cmd.Dependencies) *ListOptions { + return &ListOptions{ + ListFlags: flags, + Command: command, + Dependencies: dependencies, + } +} + +type ProjectListAsJson struct { + Id string `json:"Id"` + Name string `json:"Name"` + Description string `json:"Description"` +} + +func NewCmdList(f factory.Factory) *cobra.Command { + listFlags := NewListFlags() + cmd := &cobra.Command{ + Use: "list", + Short: "List all projects in a project group", + Long: "List all projects in a project group in Octopus Deploy", + Example: heredoc.Docf(` + $ %[1]s project-group projects list --group "Group Name" + $ %[1]s project-group projects ls -g "Group Name" + `, constants.ExecutableName), + Aliases: []string{"ls"}, + RunE: func(c *cobra.Command, args []string) error { + opts := NewListOptions(listFlags, c, cmd.NewDependencies(f, c)) + return listRun(opts) + }, + } + + flags := cmd.Flags() + flags.StringVarP(&listFlags.Group.Value, "group", "g", "filter packages to match only ones that contain the given string", "") + return cmd + +} + +func listRun(opts *ListOptions) error { + groupIdOrName := opts.Group.Value + + projectGroup, err := opts.Client.ProjectGroups.GetByIDOrName(groupIdOrName) + if err != nil { + return err + } + groupProjects, err := opts.Client.ProjectGroups.GetProjects(projectGroup) + if err != nil { + return err + } + + return output.PrintArray(groupProjects, opts.Command, output.Mappers[*projects.Project]{ + Json: func(p *projects.Project) any { + return ProjectListAsJson{ + Id: p.GetID(), + Name: p.GetName(), + Description: p.Description, + } + }, + Table: output.TableDefinition[*projects.Project]{ + Header: []string{"NAME", "DESCRIPTION"}, + Row: func(p *projects.Project) []string { + return []string{output.Bold(p.Name), p.Description} + + }, + }, + Basic: func(p *projects.Project) string { + return p.GetName() + }, + }) +} diff --git a/pkg/cmd/projectgroup/projects/projects.go b/pkg/cmd/projectgroup/projects/projects.go new file mode 100644 index 00000000..39140ba6 --- /dev/null +++ b/pkg/cmd/projectgroup/projects/projects.go @@ -0,0 +1,23 @@ +package groupprojects + +import ( + "fmt" + + cmdList "github.com/OctopusDeploy/cli/pkg/cmd/projectgroup/projects/list" + "github.com/OctopusDeploy/cli/pkg/constants" + "github.com/OctopusDeploy/cli/pkg/factory" + "github.com/spf13/cobra" +) + +func NewCmdListProjects(f factory.Factory) *cobra.Command { + cmd := &cobra.Command{ + Use: "projects ", + Short: "List all projects", + Long: "List all projects in a group in Octopus Deploy", + Example: fmt.Sprintf("$ %s project-group projects list --group ", constants.ExecutableName), + } + + cmd.AddCommand(cmdList.NewCmdList(f)) + + return cmd +}