-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |