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: add project list command #248

Merged
merged 1 commit into from
Jan 30, 2024
Merged
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
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 _, 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")
}
Loading