Skip to content

Commit

Permalink
feat: add support of json output for helm, container and application (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
pggb25 authored Feb 5, 2024
1 parent 29a524f commit d9f4446
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 2 deletions.
33 changes: 33 additions & 0 deletions cmd/application_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 @@ -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()})
Expand All @@ -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")
}

32 changes: 32 additions & 0 deletions cmd/container_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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() {
Expand All @@ -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")
}
32 changes: 32 additions & 0 deletions cmd/helm_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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() {
Expand All @@ -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")
}
15 changes: 13 additions & 2 deletions cmd/service_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit d9f4446

Please sign in to comment.