Skip to content

Commit

Permalink
feat: add --json flag for all list commands to export into JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
evoxmusic committed Sep 11, 2023
1 parent 60d7c8c commit a8fe69a
Show file tree
Hide file tree
Showing 19 changed files with 561 additions and 28 deletions.
58 changes: 50 additions & 8 deletions cmd/application_domain_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"github.com/qovery/qovery-client-go"
"os"
"strings"

Expand Down Expand Up @@ -58,6 +60,19 @@ var applicationDomainListCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

links, _, err := client.ApplicationMainCallsApi.ListApplicationLinks(context.Background(), application.Id).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(getApplicationDomainJsonOutput(links.GetResults(), customDomains.GetResults()))
return
}

customDomainsSet := make(map[string]bool)
var data [][]string

Expand All @@ -72,14 +87,6 @@ var applicationDomainListCmd = &cobra.Command{
})
}

links, _, err := client.ApplicationMainCallsApi.ListApplicationLinks(context.Background(), application.Id).Execute()

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

for _, link := range links.GetResults() {
if link.Url != nil {
domain := strings.ReplaceAll(*link.Url, "https://", "")
Expand All @@ -104,12 +111,47 @@ var applicationDomainListCmd = &cobra.Command{
},
}

func getApplicationDomainJsonOutput(links []qovery.Link, domains []qovery.CustomDomain) string {
var results []interface{}

for _, link := range links {
if link.Url != nil {
results = append(results, map[string]interface{}{
"id": nil,
"type": "BUILT_IN_DOMAIN",
"domain": strings.ReplaceAll(*link.Url, "https://", ""),
"validation_domain": nil,
})
}
}

for _, domain := range domains {
results = append(results, map[string]interface{}{
"id": domain.Id,
"type": "CUSTOM_DOMAIN",
"domain": domain.Domain,
"validation_domain": *domain.ValidationDomain,
})
}

j, err := json.Marshal(results)

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

return string(j)
}

func init() {
applicationDomainCmd.AddCommand(applicationDomainListCmd)
applicationDomainListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
applicationDomainListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
applicationDomainListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
applicationDomainListCmd.Flags().StringVarP(&applicationName, "application", "n", "", "Application Name")
applicationDomainListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output")

_ = applicationDomainListCmd.MarkFlagRequired("application")
}
15 changes: 13 additions & 2 deletions cmd/application_env_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,23 @@ var applicationEnvListCmd = &cobra.Command{
}

envVarLines := utils.NewEnvVarLines()
var variables []utils.EnvVarLineOutput

for _, envVar := range envVars.GetResults() {
envVarLines.Add(utils.FromEnvironmentVariableToEnvVarLineOutput(envVar))
s := utils.FromEnvironmentVariableToEnvVarLineOutput(envVar)
variables = append(variables, s)
envVarLines.Add(s)
}

for _, secret := range secrets.GetResults() {
envVarLines.Add(utils.FromSecretToEnvVarLineOutput(secret))
s := utils.FromSecretToEnvVarLineOutput(secret)
variables = append(variables, s)
envVarLines.Add(s)
}

if jsonFlag {
utils.Println(utils.GetEnvVarJsonOutput(variables))
return
}

err = utils.PrintTable(envVarLines.Header(utils.PrettyPrint), envVarLines.Lines(utils.ShowValues, utils.PrettyPrint))
Expand All @@ -99,6 +109,7 @@ func init() {
applicationEnvListCmd.Flags().StringVarP(&applicationName, "application", "n", "", "Application Name")
applicationEnvListCmd.Flags().BoolVarP(&utils.ShowValues, "show-values", "", false, "Show env var values")
applicationEnvListCmd.Flags().BoolVarP(&utils.PrettyPrint, "pretty-print", "", false, "Pretty print output")
applicationEnvListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output")

_ = applicationEnvListCmd.MarkFlagRequired("application")
}
2 changes: 1 addition & 1 deletion cmd/application_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var applicationListCmd = &cobra.Command{

for _, application := range applications.GetResults() {
data = append(data, []string{application.Id, *application.Name, "Application",
utils.GetStatus(statuses.GetApplications(), application.Id), application.UpdatedAt.String()})
utils.FindStatusTextWithColor(statuses.GetApplications(), application.Id), application.UpdatedAt.String()})
}

err = utils.PrintTable([]string{"Id", "Name", "Type", "Status", "Last Update"}, data)
Expand Down
32 changes: 32 additions & 0 deletions cmd/cluster_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package cmd

import (
"context"
"encoding/json"
"github.com/qovery/qovery-client-go"
"os"

"github.com/qovery/qovery-cli/utils"
Expand Down Expand Up @@ -39,6 +41,11 @@ var clusterListCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if jsonFlag {
utils.Println(getClusterJsonOutput(clusters.GetResults()))
return
}

var data [][]string

for _, cluster := range clusters.GetResults() {
Expand All @@ -56,7 +63,32 @@ var clusterListCmd = &cobra.Command{
},
}

func getClusterJsonOutput(clusters []qovery.Cluster) string {
var results []interface{}

for _, cluster := range clusters {
results = append(results, map[string]interface{}{
"id": cluster.Id,
"updated_at": utils.ToIso8601(cluster.UpdatedAt),
"type": "cluster",
"name": cluster.Name,
"status": cluster.Status,
})
}

j, err := json.Marshal(results)

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

return string(j)
}

func init() {
clusterCmd.AddCommand(clusterListCmd)
clusterListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
clusterListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output")
}
42 changes: 42 additions & 0 deletions cmd/container_domain_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"github.com/qovery/qovery-client-go"
"os"
"strings"

Expand Down Expand Up @@ -80,6 +82,11 @@ var containerDomainListCmd = &cobra.Command{
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

if jsonFlag {
utils.Println(getContainerDomainJsonOutput(links.GetResults(), customDomains.GetResults()))
return
}

for _, link := range links.GetResults() {
if link.Url != nil {
domain := strings.ReplaceAll(*link.Url, "https://", "")
Expand All @@ -104,12 +111,47 @@ var containerDomainListCmd = &cobra.Command{
},
}

func getContainerDomainJsonOutput(links []qovery.Link, domains []qovery.CustomDomain) string {
var results []interface{}

for _, link := range links {
if link.Url != nil {
results = append(results, map[string]interface{}{
"id": nil,
"type": "BUILT_IN_DOMAIN",
"domain": strings.ReplaceAll(*link.Url, "https://", ""),
"validation_domain": nil,
})
}
}

for _, domain := range domains {
results = append(results, map[string]interface{}{
"id": domain.Id,
"type": "CUSTOM_DOMAIN",
"domain": domain.Domain,
"validation_domain": *domain.ValidationDomain,
})
}

j, err := json.Marshal(results)

if err != nil {
utils.PrintlnError(err)
os.Exit(1)
panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011
}

return string(j)
}

func init() {
containerDomainCmd.AddCommand(containerDomainListCmd)
containerDomainListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name")
containerDomainListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name")
containerDomainListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name")
containerDomainListCmd.Flags().StringVarP(&containerName, "container", "n", "", "Container Name")
containerDomainListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output")

_ = containerDomainListCmd.MarkFlagRequired("container")
}
15 changes: 13 additions & 2 deletions cmd/container_env_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,23 @@ var containerEnvListCmd = &cobra.Command{
}

envVarLines := utils.NewEnvVarLines()
var variables []utils.EnvVarLineOutput

for _, envVar := range envVars.GetResults() {
envVarLines.Add(utils.FromEnvironmentVariableToEnvVarLineOutput(envVar))
s := utils.FromEnvironmentVariableToEnvVarLineOutput(envVar)
variables = append(variables, s)
envVarLines.Add(s)
}

for _, secret := range secrets.GetResults() {
envVarLines.Add(utils.FromSecretToEnvVarLineOutput(secret))
s := utils.FromSecretToEnvVarLineOutput(secret)
variables = append(variables, s)
envVarLines.Add(s)
}

if jsonFlag {
utils.Println(utils.GetEnvVarJsonOutput(variables))
return
}

err = utils.PrintTable(envVarLines.Header(utils.PrettyPrint), envVarLines.Lines(utils.ShowValues, utils.PrettyPrint))
Expand All @@ -99,6 +109,7 @@ func init() {
containerEnvListCmd.Flags().StringVarP(&containerName, "container", "n", "", "Container Name")
containerEnvListCmd.Flags().BoolVarP(&utils.ShowValues, "show-values", "", false, "Show env var values")
containerEnvListCmd.Flags().BoolVarP(&utils.PrettyPrint, "pretty-print", "", false, "Pretty print output")
containerEnvListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output")

_ = containerEnvListCmd.MarkFlagRequired("container")
}
2 changes: 1 addition & 1 deletion cmd/container_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var containerListCmd = &cobra.Command{

for _, container := range containers.GetResults() {
data = append(data, []string{container.Id, container.Name, "Container",
utils.GetStatus(statuses.GetContainers(), container.Id), container.UpdatedAt.String()})
utils.FindStatusTextWithColor(statuses.GetContainers(), container.Id), container.UpdatedAt.String()})
}

err = utils.PrintTable([]string{"Id", "Name", "Type", "Status", "Last Update"}, data)
Expand Down
15 changes: 13 additions & 2 deletions cmd/cronjob_env_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,23 @@ var cronjobEnvListCmd = &cobra.Command{
}

envVarLines := utils.NewEnvVarLines()
var variables []utils.EnvVarLineOutput

for _, envVar := range envVars.GetResults() {
envVarLines.Add(utils.FromEnvironmentVariableToEnvVarLineOutput(envVar))
s := utils.FromEnvironmentVariableToEnvVarLineOutput(envVar)
variables = append(variables, s)
envVarLines.Add(s)
}

for _, secret := range secrets.GetResults() {
envVarLines.Add(utils.FromSecretToEnvVarLineOutput(secret))
s := utils.FromSecretToEnvVarLineOutput(secret)
variables = append(variables, s)
envVarLines.Add(s)
}

if jsonFlag {
utils.Println(utils.GetEnvVarJsonOutput(variables))
return
}

err = utils.PrintTable(envVarLines.Header(utils.PrettyPrint), envVarLines.Lines(utils.ShowValues, utils.PrettyPrint))
Expand All @@ -99,6 +109,7 @@ func init() {
cronjobEnvListCmd.Flags().StringVarP(&cronjobName, "cronjob", "n", "", "Cronjob Name")
cronjobEnvListCmd.Flags().BoolVarP(&utils.ShowValues, "show-values", "", false, "Show env var values")
cronjobEnvListCmd.Flags().BoolVarP(&utils.PrettyPrint, "pretty-print", "", false, "Pretty print output")
cronjobEnvListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output")

_ = cronjobEnvListCmd.MarkFlagRequired("cronjob")
}
Loading

0 comments on commit a8fe69a

Please sign in to comment.