From d9f4446fe4c5fccfdac8a03092a609773737ee9e Mon Sep 17 00:00:00 2001 From: Pierre G Date: Mon, 5 Feb 2024 20:02:02 +0100 Subject: [PATCH] feat: add support of json output for helm, container and application (#252) --- cmd/application_list.go | 33 +++++++++++++++++++++++++++++++++ cmd/container_list.go | 32 ++++++++++++++++++++++++++++++++ cmd/helm_list.go | 32 ++++++++++++++++++++++++++++++++ cmd/service_list.go | 15 +++++++++++++-- 4 files changed, 110 insertions(+), 2 deletions(-) diff --git a/cmd/application_list.go b/cmd/application_list.go index f0eb0a3f..28ff08b9 100644 --- a/cmd/application_list.go +++ b/cmd/application_list.go @@ -2,6 +2,8 @@ package cmd import ( "context" + "encoding/json" + "github.com/qovery/qovery-client-go" "os" "github.com/qovery/qovery-cli/utils" @@ -49,6 +51,11 @@ var applicationListCmd = &cobra.Command{ var data [][]string + if jsonFlag { + utils.Println(getAppJsonOutput(applications.GetResults(), statuses)) + return + } + for _, application := range applications.GetResults() { data = append(data, []string{application.Id, application.Name, "Application", utils.FindStatusTextWithColor(statuses.GetApplications(), application.Id), application.UpdatedAt.String()}) @@ -64,9 +71,35 @@ var applicationListCmd = &cobra.Command{ }, } +func getAppJsonOutput(applications []qovery.Application, statuses *qovery.EnvironmentStatuses) string { + var results []interface{} + + for _, application := range applications { + results = append(results, map[string]interface{}{ + "id": application.Id, + "name": application.Name, + "type": "Application", + "status": utils.FindStatus(statuses.GetApplications(), application.Id), + "last_update": application.UpdatedAt.String(), + }) + } + + 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() { applicationCmd.AddCommand(applicationListCmd) applicationListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") applicationListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") applicationListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + applicationListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output") } + diff --git a/cmd/container_list.go b/cmd/container_list.go index 17eb3867..7e4d6d92 100644 --- a/cmd/container_list.go +++ b/cmd/container_list.go @@ -2,7 +2,9 @@ package cmd import ( "context" + "encoding/json" "github.com/qovery/qovery-cli/utils" + "github.com/qovery/qovery-client-go" "github.com/spf13/cobra" "os" ) @@ -45,6 +47,11 @@ var containerListCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } + if jsonFlag { + utils.Println(getContainerJsonOutput(containers.GetResults(), statuses)) + return + } + var data [][]string for _, container := range containers.GetResults() { @@ -62,9 +69,34 @@ var containerListCmd = &cobra.Command{ }, } +func getContainerJsonOutput(containers []qovery.ContainerResponse, statuses *qovery.EnvironmentStatuses) string { + var results []interface{} + + for _, container := range containers { + results = append(results, map[string]interface{}{ + "id": container.Id, + "name": container.Name, + "type": "Container", + "status": utils.FindStatus(statuses.GetApplications(), container.Id), + "last_update": container.UpdatedAt.String(), + }) + } + + 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() { containerCmd.AddCommand(containerListCmd) containerListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") containerListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") containerListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + containerListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output") } diff --git a/cmd/helm_list.go b/cmd/helm_list.go index 3b51d66b..b317151c 100644 --- a/cmd/helm_list.go +++ b/cmd/helm_list.go @@ -2,7 +2,9 @@ package cmd import ( "context" + "encoding/json" "github.com/qovery/qovery-cli/utils" + "github.com/qovery/qovery-client-go" "github.com/spf13/cobra" "os" ) @@ -45,6 +47,11 @@ var helmListCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } + if jsonFlag { + utils.Println(getHelmJsonOutput(helms.GetResults(), statuses)) + return + } + var data [][]string for _, helm := range helms.GetResults() { @@ -62,9 +69,34 @@ var helmListCmd = &cobra.Command{ }, } +func getHelmJsonOutput(helms []qovery.HelmResponse, statuses *qovery.EnvironmentStatuses) string { + var results []interface{} + + for _, helm := range helms { + results = append(results, map[string]interface{}{ + "id": helm.Id, + "name": helm.Name, + "type": "Helm", + "status": utils.FindStatus(statuses.GetHelms(), helm.Id), + "last_update": helm.UpdatedAt.String(), + }) + } + + 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() { helmCmd.AddCommand(helmListCmd) helmListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") helmListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") helmListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + helmListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output") } diff --git a/cmd/service_list.go b/cmd/service_list.go index 45bbcad5..a7fd99f6 100644 --- a/cmd/service_list.go +++ b/cmd/service_list.go @@ -98,7 +98,7 @@ var serviceListCmd = &cobra.Command{ } if jsonFlag { - j := getServiceJsonOutput(*statuses, apps.GetResults(), containers.GetResults(), jobs.GetResults(), databases.GetResults()) + j := getServiceJsonOutput(*statuses, apps.GetResults(), containers.GetResults(), jobs.GetResults(), databases.GetResults(), helms.GetResults()) fmt.Print(j) return } @@ -352,7 +352,7 @@ func getHelmContextResource(qoveryAPIClient *qovery.APIClient, helmName string, } -func getServiceJsonOutput(statuses qovery.EnvironmentStatuses, apps []qovery.Application, containers []qovery.ContainerResponse, jobs []qovery.JobResponse, databases []qovery.Database) string { +func getServiceJsonOutput(statuses qovery.EnvironmentStatuses, apps []qovery.Application, containers []qovery.ContainerResponse, jobs []qovery.JobResponse, databases []qovery.Database, helms []qovery.HelmResponse) string { var results []interface{} for _, app := range apps { @@ -393,6 +393,17 @@ func getServiceJsonOutput(statuses qovery.EnvironmentStatuses, apps []qovery.App results = append(results, m) } + for _, helm := range helms { + m := map[string]interface{}{ + "id": helm.Id, + "name": helm.Name, + "type": "helm", + "status": utils.FindStatus(statuses.GetHelms(), helm.Id), + } + + results = append(results, m) + } + for _, db := range databases { m := map[string]interface{}{ "id": db.Id,