From 285079653b9540fec2d744ef08222f2dd2adba3e Mon Sep 17 00:00:00 2001 From: BenjaminCh Date: Tue, 30 Jan 2024 10:59:09 +0100 Subject: [PATCH] feat: add project list command Ticket: ENG-1687 --- cmd/project.go | 14 ++++++++ cmd/project_list.go | 79 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 cmd/project.go create mode 100644 cmd/project_list.go diff --git a/cmd/project.go b/cmd/project.go new file mode 100644 index 00000000..de4ffcb2 --- /dev/null +++ b/cmd/project.go @@ -0,0 +1,14 @@ +package cmd + +import ( + "github.com/spf13/cobra" +) + +var projectCmd = &cobra.Command{ + Use: "project", + Short: "Manage Project", +} + +func init() { + rootCmd.AddCommand(projectCmd) +} diff --git a/cmd/project_list.go b/cmd/project_list.go new file mode 100644 index 00000000..ea312e77 --- /dev/null +++ b/cmd/project_list.go @@ -0,0 +1,79 @@ +package cmd + +import ( + "context" + "encoding/json" + "github.com/qovery/qovery-client-go" + "os" + + "github.com/qovery/qovery-cli/utils" + "github.com/spf13/cobra" +) + +var projectListCmd = &cobra.Command{ + Use: "list", + Short: "List projects", + Run: func(cmd *cobra.Command, args []string) { + utils.Capture(cmd) + + tokenType, token, err := utils.GetAccessToken() + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + client := utils.GetQoveryClient(tokenType, token) + organizationID, err := getOrganizationContextResourceId(client, organizationName) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + projects, _, err := client.ProjectsAPI.ListProject(context.Background(), organizationID).Execute() + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + if jsonFlag { + utils.Println(getProjectJsonOutput(projects.GetResults())) + return + } + + var data [][]string + + for _, project := range projects.GetResults() { + data = append(data, []string{project.Id, project.GetName()}) + } + + err = utils.PrintTable([]string{"Id", "Name"}, data) + + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + }, +} + +func getProjectJsonOutput(projects []qovery.Project) string { + projectJSON, err := json.Marshal(projects) + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + return string(projectJSON) +} + +func init() { + projectCmd.AddCommand(projectListCmd) + projectListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") + projectListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output") +}