Skip to content

Commit

Permalink
feat: add project list command
Browse files Browse the repository at this point in the history
Ticket: ENG-1687
  • Loading branch information
benjaminch committed Jan 30, 2024
1 parent 313c1ff commit 021ef62
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmd/project.go
Original file line number Diff line number Diff line change
@@ -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)
}
79 changes: 79 additions & 0 deletions cmd/project_list.go
Original file line number Diff line number Diff line change
@@ -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 _, env := range projects.GetResults() {
data = append(data, []string{env.Id, env.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")
}

0 comments on commit 021ef62

Please sign in to comment.