Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: list all projects in any one group. #388

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions pkg/cmd/projectgroup/project-group.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
}
102 changes: 102 additions & 0 deletions pkg/cmd/projectgroup/projects/list/list.go
Original file line number Diff line number Diff line change
@@ -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()
},
})
}
23 changes: 23 additions & 0 deletions pkg/cmd/projectgroup/projects/projects.go
Original file line number Diff line number Diff line change
@@ -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 <command>",
Short: "List all projects",
Long: "List all projects in a group in Octopus Deploy",
Example: fmt.Sprintf("$ %s project-group projects list --group <GroupName>", constants.ExecutableName),
}

cmd.AddCommand(cmdList.NewCmdList(f))

return cmd
}