diff --git a/cmd/application_domain_list.go b/cmd/application_domain_list.go index 2bf9fa84..101210f3 100644 --- a/cmd/application_domain_list.go +++ b/cmd/application_domain_list.go @@ -2,7 +2,9 @@ package cmd import ( "context" + "encoding/json" "fmt" + "github.com/qovery/qovery-client-go" "os" "strings" @@ -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 @@ -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://", "") @@ -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") } diff --git a/cmd/application_env_list.go b/cmd/application_env_list.go index 5969964a..864bc0da 100644 --- a/cmd/application_env_list.go +++ b/cmd/application_env_list.go @@ -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)) @@ -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") } diff --git a/cmd/application_list.go b/cmd/application_list.go index 2b4e8e54..af82524d 100644 --- a/cmd/application_list.go +++ b/cmd/application_list.go @@ -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) diff --git a/cmd/cluster_list.go b/cmd/cluster_list.go index fca04549..e50176fd 100644 --- a/cmd/cluster_list.go +++ b/cmd/cluster_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" @@ -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() { @@ -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") } diff --git a/cmd/container_domain_list.go b/cmd/container_domain_list.go index cc944e62..cd9d552f 100644 --- a/cmd/container_domain_list.go +++ b/cmd/container_domain_list.go @@ -2,7 +2,9 @@ package cmd import ( "context" + "encoding/json" "fmt" + "github.com/qovery/qovery-client-go" "os" "strings" @@ -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://", "") @@ -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") } diff --git a/cmd/container_env_list.go b/cmd/container_env_list.go index cc16d0f1..df2bc830 100644 --- a/cmd/container_env_list.go +++ b/cmd/container_env_list.go @@ -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)) @@ -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") } diff --git a/cmd/container_list.go b/cmd/container_list.go index a232eb0c..6c7347fe 100644 --- a/cmd/container_list.go +++ b/cmd/container_list.go @@ -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) diff --git a/cmd/cronjob_env_list.go b/cmd/cronjob_env_list.go index c27541ca..90b58bf5 100644 --- a/cmd/cronjob_env_list.go +++ b/cmd/cronjob_env_list.go @@ -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)) @@ -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") } diff --git a/cmd/cronjob_list.go b/cmd/cronjob_list.go index e9327314..564e01b8 100644 --- a/cmd/cronjob_list.go +++ b/cmd/cronjob_list.go @@ -2,6 +2,9 @@ package cmd import ( "context" + "encoding/json" + "fmt" + "github.com/qovery/qovery-client-go" "os" "github.com/qovery/qovery-cli/utils" @@ -46,11 +49,16 @@ var cronjobListCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } + if jsonFlag { + fmt.Print(getCronjobJsonOutput(statuses.GetJobs(), cronjobs)) + return + } + var data [][]string for _, cronjob := range cronjobs { data = append(data, []string{cronjob.Id, cronjob.Name, "Cronjob", - utils.GetStatus(statuses.GetJobs(), cronjob.Id), cronjob.UpdatedAt.String()}) + utils.FindStatusTextWithColor(statuses.GetJobs(), cronjob.Id), cronjob.UpdatedAt.String()}) } err = utils.PrintTable([]string{"Id", "Name", "Type", "Status", "Last Update"}, data) @@ -63,9 +71,36 @@ var cronjobListCmd = &cobra.Command{ }, } +func getCronjobJsonOutput(statuses []qovery.Status, cronjobs []qovery.JobResponse) string { + var results []interface{} + + for _, cronjob := range cronjobs { + if cronjob.Schedule.Cronjob != nil { + results = append(results, map[string]interface{}{ + "id": cronjob.Id, + "name": cronjob.Name, + "type": "Cronjob", + "status": utils.FindStatus(statuses, cronjob.Id), + "updated_at": utils.ToIso8601(cronjob.UpdatedAt), + }) + } + } + + 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() { cronjobCmd.AddCommand(cronjobListCmd) cronjobListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") cronjobListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") cronjobListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + cronjobListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output") } diff --git a/cmd/database_list.go b/cmd/database_list.go index 2ba396a6..1dd0a379 100644 --- a/cmd/database_list.go +++ b/cmd/database_list.go @@ -2,6 +2,8 @@ package cmd import ( "context" + "encoding/json" + "github.com/qovery/qovery-client-go" "os" "strconv" @@ -48,6 +50,11 @@ var databaseListCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } + if jsonFlag { + utils.Println(getDatabaseJsonOutput(*client, statuses.GetDatabases(), databases.GetResults())) + return + } + var data [][]string for _, database := range databases.GetResults() { @@ -72,7 +79,7 @@ var databaseListCmd = &cobra.Command{ } data = append(data, []string{database.Id, database.Name, "Database", - utils.GetStatus(statuses.GetDatabases(), database.Id), res.Host, strconv.Itoa(int(res.Port)), login, password, database.UpdatedAt.String()}) + utils.FindStatusTextWithColor(statuses.GetDatabases(), database.Id), res.Host, strconv.Itoa(int(res.Port)), login, password, database.UpdatedAt.String()}) } err = utils.PrintTable([]string{"Id", "Name", "Type", "Status", "Host", "Port", "Login", "Password", "Last Update"}, data) @@ -85,10 +92,47 @@ var databaseListCmd = &cobra.Command{ }, } +func getDatabaseJsonOutput(client qovery.APIClient, statuses []qovery.Status, databases []qovery.Database) string { + var results []interface{} + + for _, database := range databases { + res, _, err := client.DatabaseMainCallsApi.GetDatabaseMasterCredentials(context.Background(), database.Id).Execute() + if err != nil { + utils.PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + results = append(results, map[string]interface{}{ + "id": database.Id, + "updated_at": utils.ToIso8601(database.UpdatedAt), + "name": database.Name, + "type": "Database", + "database_type": database.Type, + "status": utils.FindStatus(statuses, database.Id), + "host": database.Host, + "port": res.Port, + "login": res.Login, + "password": res.Password, + }) + } + + 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() { databaseCmd.AddCommand(databaseListCmd) databaseListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") databaseListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") databaseListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") databaseListCmd.Flags().BoolVarP(&showCredentials, "show-credentials", "", false, "Show Credentials") + databaseListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output") } diff --git a/cmd/environment_deployment_list.go b/cmd/environment_deployment_list.go index bac4db75..0f21fc29 100644 --- a/cmd/environment_deployment_list.go +++ b/cmd/environment_deployment_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" ) @@ -37,6 +39,11 @@ var environmentDeploymentListCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } + if jsonFlag { + utils.Println(toDeploymentListJsonOutput(deployments.GetResults())) + return + } + var data [][]string for _, deployment := range deployments.GetResults() { @@ -58,9 +65,33 @@ var environmentDeploymentListCmd = &cobra.Command{ }, } +func toDeploymentListJsonOutput(deployments []qovery.DeploymentHistoryEnvironment) string { + var results []interface{} + + for _, deployment := range deployments { + results = append(results, map[string]interface{}{ + "id": deployment.Id, + "created_at": utils.ToIso8601(&deployment.CreatedAt), + "status": deployment.GetStatus(), + "deployment_duration_in_seconds": int(deployment.GetUpdatedAt().Sub(deployment.GetCreatedAt()).Seconds()), + }) + } + + 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() { environmentDeploymentCmd.AddCommand(environmentDeploymentListCmd) environmentDeploymentListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") environmentDeploymentListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") environmentDeploymentListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + environmentDeploymentListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output") } diff --git a/cmd/environment_list.go b/cmd/environment_list.go index f94d4496..6ae8589b 100644 --- a/cmd/environment_list.go +++ b/cmd/environment_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" @@ -46,11 +48,16 @@ var environmentListCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } + if jsonFlag { + utils.Println(getEnvironmentJsonOutput(statuses.GetResults(), environments.GetResults())) + return + } + var data [][]string for _, env := range environments.GetResults() { data = append(data, []string{env.Id, env.GetName(), *env.ClusterName, string(env.Mode), - utils.GetEnvironmentStatus(statuses.GetResults(), env.Id), env.UpdatedAt.String()}) + utils.GetEnvironmentStatusWithColor(statuses.GetResults(), env.Id), env.UpdatedAt.String()}) } err = utils.PrintTable([]string{"Id", "Name", "Cluster", "Type", "Status", "Last Update"}, data) @@ -63,8 +70,36 @@ var environmentListCmd = &cobra.Command{ }, } +func getEnvironmentJsonOutput(statuses []qovery.EnvironmentStatus, environments []qovery.Environment) string { + var results []interface{} + + for _, env := range environments { + results = append(results, map[string]interface{}{ + "id": env.Id, + "created_at": utils.ToIso8601(&env.CreatedAt), + "updated_at": utils.ToIso8601(env.UpdatedAt), + "name": env.GetName(), + "cluster_name": *env.ClusterName, + "cluster_id": env.ClusterId, + "type": string(env.Mode), + "status": utils.GetEnvironmentStatus(statuses, env.Id), + }) + } + + 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() { environmentCmd.AddCommand(environmentListCmd) environmentListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") environmentListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") + environmentListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output") } diff --git a/cmd/environment_stage_list.go b/cmd/environment_stage_list.go index 92b31d0a..8afd86a7 100644 --- a/cmd/environment_stage_list.go +++ b/cmd/environment_stage_list.go @@ -2,7 +2,9 @@ package cmd import ( "context" + "encoding/json" "github.com/pterm/pterm" + "github.com/qovery/qovery-client-go" "os" "strconv" @@ -40,6 +42,11 @@ var environmentStageListCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } + if jsonFlag { + utils.Println(getEnvironmentStageJsonOutput(*client, stages.GetResults())) + return + } + for _, stage := range stages.GetResults() { pterm.DefaultSection.WithBottomPadding(0).Println("deployment stage " + strconv.Itoa(int(stage.GetDeploymentOrder()+1)) + ": \"" + stage.GetName() + "\"") if stage.GetDescription() != "" { @@ -74,9 +81,44 @@ var environmentStageListCmd = &cobra.Command{ }, } +func getEnvironmentStageJsonOutput(client qovery.APIClient, stages []qovery.DeploymentStageResponse) string { + var results []interface{} + + for idx, stage := range stages { + var services []interface{} + + for _, service := range stage.Services { + services = append(services, map[string]interface{}{ + "id": service.ServiceId, + "type": service.ServiceType, + "name": utils.GetServiceNameByIdAndType(&client, service.GetServiceId(), service.GetServiceType()), + }) + } + + results = append(results, map[string]interface{}{ + "stage_order": idx + 1, + "stage_id": stage.Id, + "stage_name": stage.Name, + "stage_description": stage.Description, + "services": services, + }) + } + + 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() { environmentStageCmd.AddCommand(environmentStageListCmd) environmentStageListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") environmentStageListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") environmentStageListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + environmentStageListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output") } diff --git a/cmd/lifecycle_env_list.go b/cmd/lifecycle_env_list.go index 6d824c9e..f6bf130d 100644 --- a/cmd/lifecycle_env_list.go +++ b/cmd/lifecycle_env_list.go @@ -72,13 +72,23 @@ var lifecycleEnvListCmd = &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)) @@ -99,6 +109,7 @@ func init() { lifecycleEnvListCmd.Flags().StringVarP(&lifecycleName, "lifecycle", "n", "", "Lifecycle Name") lifecycleEnvListCmd.Flags().BoolVarP(&utils.ShowValues, "show-values", "", false, "Show env var values") lifecycleEnvListCmd.Flags().BoolVarP(&utils.PrettyPrint, "pretty-print", "", false, "Pretty print output") + lifecycleEnvListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output") _ = lifecycleEnvListCmd.MarkFlagRequired("lifecycle") } diff --git a/cmd/lifecycle_list.go b/cmd/lifecycle_list.go index 6c341ce6..8af55cdc 100644 --- a/cmd/lifecycle_list.go +++ b/cmd/lifecycle_list.go @@ -2,6 +2,9 @@ package cmd import ( "context" + "encoding/json" + "fmt" + "github.com/qovery/qovery-client-go" "os" "github.com/qovery/qovery-cli/utils" @@ -47,11 +50,16 @@ var lifecycleListCmd = &cobra.Command{ panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 } + if jsonFlag { + fmt.Print(getLifecycleJsonOutput(statuses.GetJobs(), lifecycles)) + return + } + var data [][]string for _, lifecycle := range lifecycles { data = append(data, []string{lifecycle.Id, lifecycle.Name, "Lifecycle", - utils.GetStatus(statuses.GetJobs(), lifecycle.Id), lifecycle.UpdatedAt.String()}) + utils.FindStatusTextWithColor(statuses.GetJobs(), lifecycle.Id), lifecycle.UpdatedAt.String()}) } err = utils.PrintTable([]string{"Id", "Name", "Type", "Status", "Last Update"}, data) @@ -64,9 +72,36 @@ var lifecycleListCmd = &cobra.Command{ }, } +func getLifecycleJsonOutput(statuses []qovery.Status, lifecycles []qovery.JobResponse) string { + var results []interface{} + + for _, lifecycle := range lifecycles { + if lifecycle.Schedule.Cronjob == nil { + results = append(results, map[string]interface{}{ + "id": lifecycle.Id, + "name": lifecycle.Name, + "type": "Lifecycle", + "status": utils.FindStatus(statuses, lifecycle.Id), + "updated_at": utils.ToIso8601(lifecycle.UpdatedAt), + }) + } + } + + 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() { lifecycleCmd.AddCommand(lifecycleListCmd) lifecycleListCmd.Flags().StringVarP(&organizationName, "organization", "", "", "Organization Name") lifecycleListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") lifecycleListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") + lifecycleListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output") } diff --git a/cmd/service_list.go b/cmd/service_list.go index 49fdb8c8..d14de22e 100644 --- a/cmd/service_list.go +++ b/cmd/service_list.go @@ -2,6 +2,7 @@ package cmd import ( "context" + "encoding/json" "fmt" "github.com/go-errors/errors" "os" @@ -18,6 +19,7 @@ var projectName string var environmentName string var watchFlag bool var markdownFlag bool +var jsonFlag bool var serviceListCmd = &cobra.Command{ Use: "list", @@ -87,22 +89,33 @@ var serviceListCmd = &cobra.Command{ return } + if jsonFlag { + j := getServiceJsonOutput(*statuses, apps.GetResults(), containers.GetResults(), jobs.GetResults(), databases.GetResults()) + fmt.Print(j) + return + } + var data [][]string for _, app := range apps.GetResults() { - data = append(data, []string{app.GetName(), "Application", utils.GetStatus(statuses.GetApplications(), app.Id)}) + data = append(data, []string{app.GetName(), "Application", utils.FindStatusTextWithColor(statuses.GetApplications(), app.Id)}) } for _, container := range containers.GetResults() { - data = append(data, []string{container.Name, "Container", utils.GetStatus(statuses.GetContainers(), container.Id)}) + data = append(data, []string{container.Name, "Container", utils.FindStatusTextWithColor(statuses.GetContainers(), container.Id)}) } for _, job := range jobs.GetResults() { - data = append(data, []string{job.Name, "Job", utils.GetStatus(statuses.GetJobs(), job.Id)}) + jobType := "Lifecycle" + if job.Schedule.Cronjob != nil { + jobType = "Cronjob" + } + + data = append(data, []string{job.Name, jobType, utils.FindStatusTextWithColor(statuses.GetJobs(), job.Id)}) } for _, database := range databases.GetResults() { - data = append(data, []string{database.Name, "Database", utils.GetStatus(statuses.GetDatabases(), database.Id)}) + data = append(data, []string{database.Name, "Database", utils.FindStatusTextWithColor(statuses.GetDatabases(), database.Id)}) } err = utils.PrintTable([]string{"Name", "Type", "Status"}, data) @@ -304,6 +317,69 @@ func getJobContextResource(qoveryAPIClient *qovery.APIClient, jobName string, en return job, nil } +func getServiceJsonOutput(statuses qovery.GetEnvironmentStatuses200Response, apps []qovery.Application, containers []qovery.ContainerResponse, jobs []qovery.JobResponse, databases []qovery.Database) string { + var results []interface{} + + for _, app := range apps { + m := map[string]interface{}{ + "id": app.Id, + "name": app.Name, + "type": "application", + "status": utils.FindStatus(statuses.GetApplications(), app.Id), + } + + results = append(results, m) + } + + for _, container := range containers { + m := map[string]interface{}{ + "id": container.Id, + "name": container.Name, + "type": "container", + "status": utils.FindStatus(statuses.GetContainers(), container.Id), + } + + results = append(results, m) + } + + for _, job := range jobs { + jobType := "lifecycle" + if job.Schedule.Cronjob != nil { + jobType = "cronjob" + } + + m := map[string]interface{}{ + "id": job.Id, + "name": job.Name, + "type": jobType, + "status": utils.FindStatus(statuses.GetJobs(), job.Id), + } + + results = append(results, m) + } + + for _, db := range databases { + m := map[string]interface{}{ + "id": db.Id, + "name": db.Name, + "type": "database", + "status": utils.FindStatus(statuses.GetDatabases(), db.Id), + } + + results = append(results, m) + } + + 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 getMarkdownOutput(client qovery.APIClient, orgId string, projectId string, envId string, apps []qovery.Application, containers []qovery.ContainerResponse, jobs []qovery.JobResponse, databases []qovery.Database) string { env, _, err := client.EnvironmentMainCallsApi.GetEnvironment(context.Background(), envId).Execute() if err != nil { @@ -321,7 +397,7 @@ Click on the links below to access the different services: `, env.Name, fmt.Sprintf("https://console.qovery.com/organization/%s/project/%s/environment/%s", orgId, projectId, envId)) body := ` -| ServiceLevel | Logs | Preview URL | +| Service | Logs | Preview URL | |---------|------|-------------|` footer := ` @@ -415,4 +491,5 @@ func init() { serviceListCmd.Flags().StringVarP(&projectName, "project", "", "", "Project Name") serviceListCmd.Flags().StringVarP(&environmentName, "environment", "", "", "Environment Name") serviceListCmd.Flags().BoolVarP(&markdownFlag, "markdown", "", false, "Markdown output") + serviceListCmd.Flags().BoolVarP(&jsonFlag, "json", "", false, "JSON output") } diff --git a/utils/date.go b/utils/date.go new file mode 100644 index 00000000..f4105685 --- /dev/null +++ b/utils/date.go @@ -0,0 +1,12 @@ +package utils + +import "time" + +func ToIso8601(v *time.Time) *string { + if v == nil { + return nil + } + + x := v.Format("2006-01-02T15:04:05.000Z") + return &x +} diff --git a/utils/env_var.go b/utils/env_var.go index c559b7ca..fa620784 100644 --- a/utils/env_var.go +++ b/utils/env_var.go @@ -2,10 +2,12 @@ package utils import ( "context" + "encoding/json" "errors" "fmt" "github.com/pterm/pterm" "github.com/qovery/qovery-client-go" + "os" "strings" "time" ) @@ -78,8 +80,10 @@ func (e EnvVarLines) Lines(showValues bool, prettyPrint bool) [][]string { } type EnvVarLineOutput struct { + Id string Key string Value *string + CreatedAt time.Time UpdatedAt *time.Time Service *string Scope string @@ -130,8 +134,10 @@ func FromEnvironmentVariableToEnvVarLineOutput(envVar qovery.EnvironmentVariable } return EnvVarLineOutput{ + Id: envVar.Id, Key: envVar.Key, Value: envVar.Value, + CreatedAt: envVar.CreatedAt, UpdatedAt: envVar.UpdatedAt, Service: envVar.ServiceName, Scope: string(envVar.Scope), @@ -153,8 +159,10 @@ func FromSecretToEnvVarLineOutput(secret qovery.Secret) EnvVarLineOutput { } return EnvVarLineOutput{ + Id: secret.Id, Key: secret.Key, Value: nil, + CreatedAt: secret.CreatedAt, UpdatedAt: secret.UpdatedAt, Service: secret.ServiceName, Scope: string(secret.Scope), @@ -826,3 +834,33 @@ func CreateOverride( return fmt.Errorf("Environment variable or secret %s not found", pterm.FgRed.Sprintf(key)) } + +func GetEnvVarJsonOutput(variables []EnvVarLineOutput) string { + var results []interface{} + + for _, v := range variables { + // TODO improve this + + results = append(results, map[string]interface{}{ + "id": v.Id, + "created_at": ToIso8601(&v.CreatedAt), + "updated_at": ToIso8601(v.UpdatedAt), + "key": v.Key, + "value": v.Value, + "service_name": v.Service, + "scope": v.Scope, + "alias_parent_key": v.AliasParentKey, + "override_parent_value": v.OverrideParentKey, + }) + } + + j, err := json.Marshal(results) + + if err != nil { + PrintlnError(err) + os.Exit(1) + panic("unreachable") // staticcheck false positive: https://staticcheck.io/docs/checks#SA5011 + } + + return string(j) +} diff --git a/utils/qovery.go b/utils/qovery.go index 63cd4457..7c7276a9 100644 --- a/utils/qovery.go +++ b/utils/qovery.go @@ -757,7 +757,19 @@ func SelectTokenInformation() (*TokenInformation, error) { }, nil } -func GetStatus(statuses []qovery.Status, serviceId string) string { +func FindStatus(statuses []qovery.Status, serviceId string) string { + status := "Unknown" + + for _, s := range statuses { + if serviceId == s.Id { + return string(s.State) + } + } + + return status +} + +func FindStatusTextWithColor(statuses []qovery.Status, serviceId string) string { status := "Unknown" for _, s := range statuses { @@ -772,6 +784,18 @@ func GetStatus(statuses []qovery.Status, serviceId string) string { func GetEnvironmentStatus(statuses []qovery.EnvironmentStatus, serviceId string) string { status := "Unknown" + for _, s := range statuses { + if serviceId == s.Id { + return string(s.State) + } + } + + return status +} + +func GetEnvironmentStatusWithColor(statuses []qovery.EnvironmentStatus, serviceId string) string { + status := "Unknown" + for _, s := range statuses { if serviceId == s.Id { return GetStatusTextWithColor(s.State)