Skip to content

Commit

Permalink
Merge pull request #24 from stackitcloud/vp/output-format-2
Browse files Browse the repository at this point in the history
Add output format for list and describe commands
  • Loading branch information
vicentepinto98 authored Nov 24, 2023
2 parents 53fe3cb + e2c9890 commit 0be5cb5
Show file tree
Hide file tree
Showing 15 changed files with 333 additions and 141 deletions.
4 changes: 2 additions & 2 deletions internal/cmd/config/unset/unset.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ func NewCmd() *cobra.Command {
viper.Set(config.ProjectIdKey, "")
}
if model.OutputFormat {
viper.Set(config.OutputFormatKey, "default")
viper.Set(config.OutputFormatKey, "")
}
if model.DNSCustomEndpoint {
viper.Set(config.DNSCustomEndpointKey, "")
}
if model.PostgreSQLCustomEndpoint {
viper.Set(config.PostgreSQLCustomEndpointKey, "")
}
if model.PostgreSQLCustomEndpoint {
if model.SKECustomEndpoint {
viper.Set(config.SKECustomEndpointKey, "")
}

Expand Down
35 changes: 26 additions & 9 deletions internal/cmd/dns/record-set/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -50,16 +51,9 @@ func NewCmd() *cobra.Command {
if err != nil {
return fmt.Errorf("read DNS record set: %w", err)
}
recordSet := *resp.Rrset
recordSet := resp.Rrset

// Show details
details, err := json.MarshalIndent(recordSet, "", " ")
if err != nil {
return fmt.Errorf("marshal DNS record set: %w", err)
}
cmd.Println(string(details))

return nil
return outputResult(cmd, model.OutputFormat, recordSet)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -91,3 +85,26 @@ func buildRequest(ctx context.Context, model *flagModel, apiClient *dns.APIClien
req := apiClient.GetRecordSet(ctx, model.ProjectId, model.ZoneId, model.RecordSetId)
return req
}

func outputResult(cmd *cobra.Command, outputFormat string, recordSet *dns.RecordSet) error {
switch outputFormat {
case globalflags.TableOutputFormat:
table := tables.NewTable()
table.SetHeader("ID", "Name", "Type", "State")
table.AddRow(*recordSet.Id, *recordSet.Name, *recordSet.Type, *recordSet.State)
err := table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
default:
details, err := json.MarshalIndent(recordSet, "", " ")
if err != nil {
return fmt.Errorf("marshal DNS record set: %w", err)
}
cmd.Println(string(details))

return nil
}
}
42 changes: 28 additions & 14 deletions internal/cmd/dns/record-set/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package list

import (
"context"
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -62,20 +63,7 @@ func NewCmd() *cobra.Command {
cmd.Printf("No record sets found for zone %s in project with ID %s\n", model.ZoneId, model.ProjectId)
return nil
}

// Show output as table
table := tables.NewTable()
table.SetHeader("ID", "Name", "Type", "State")
for i := range recordSets {
rs := recordSets[i]
table.AddRow(*rs.Id, *rs.Name, *rs.Type, *rs.State)
}
err = table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
return outputResult(cmd, model.OutputFormat, recordSets)
},
}

Expand Down Expand Up @@ -179,3 +167,29 @@ func fetchRecordSets(ctx context.Context, model *flagModel, apiClient dnsClient)
}
return recordSets, nil
}

func outputResult(cmd *cobra.Command, outputFormat string, recordSets []dns.RecordSet) error {
switch outputFormat {
case globalflags.JSONOutputFormat:
details, err := json.MarshalIndent(recordSets, "", " ")
if err != nil {
return fmt.Errorf("marshal DNS record set list: %w", err)
}
cmd.Println(string(details))

return nil
default:
table := tables.NewTable()
table.SetHeader("ID", "Name", "Type", "State")
for i := range recordSets {
rs := recordSets[i]
table.AddRow(*rs.Id, *rs.Name, *rs.Type, *rs.State)
}
err := table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
}
}
35 changes: 26 additions & 9 deletions internal/cmd/dns/zone/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -47,16 +48,9 @@ func NewCmd() *cobra.Command {
if err != nil {
return fmt.Errorf("read DNS zone: %w", err)
}
zone := *resp.Zone
zone := resp.Zone

// Show details
details, err := json.MarshalIndent(zone, "", " ")
if err != nil {
return fmt.Errorf("marshal DNS zone: %w", err)
}
cmd.Println(string(details))

return nil
return outputResult(cmd, model.OutputFormat, zone)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -86,3 +80,26 @@ func buildRequest(ctx context.Context, model *flagModel, apiClient *dns.APIClien
req := apiClient.GetZone(ctx, model.ProjectId, model.ZoneId)
return req
}

func outputResult(cmd *cobra.Command, outputFormat string, zone *dns.Zone) error {
switch outputFormat {
case globalflags.TableOutputFormat:
table := tables.NewTable()
table.SetHeader("ID", "NAME", "DNS_NAME", "STATE")
table.AddRow(*zone.Id, *zone.Name, *zone.DnsName, *zone.State)
err := table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
default:
details, err := json.MarshalIndent(zone, "", " ")
if err != nil {
return fmt.Errorf("marshal DNS zone: %w", err)
}
cmd.Println(string(details))

return nil
}
}
42 changes: 29 additions & 13 deletions internal/cmd/dns/zone/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package list

import (
"context"
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -61,19 +62,7 @@ func NewCmd() *cobra.Command {
return nil
}

// Show output as table
table := tables.NewTable()
table.SetHeader("ID", "NAME", "DNS_NAME", "STATE")
for i := range zones {
z := zones[i]
table.AddRow(*z.Id, *z.Name, *z.DnsName, *z.State)
}
err = table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
return outputResult(cmd, model.OutputFormat, zones)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -171,3 +160,30 @@ func fetchZones(ctx context.Context, model *flagModel, apiClient dnsClient) ([]d
}
return zones, nil
}

func outputResult(cmd *cobra.Command, outputFormat string, zones []dns.Zone) error {
switch outputFormat {
case globalflags.JSONOutputFormat:
// Show details
details, err := json.MarshalIndent(zones, "", " ")
if err != nil {
return fmt.Errorf("marshal DNS zone list: %w", err)
}
cmd.Println(string(details))

return nil
default:
table := tables.NewTable()
table.SetHeader("ID", "NAME", "DNS_NAME", "STATE")
for i := range zones {
z := zones[i]
table.AddRow(*z.Id, *z.Name, *z.DnsName, *z.State)
}
err := table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
}
}
33 changes: 25 additions & 8 deletions internal/cmd/postgresql/credential/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresql/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -51,14 +52,7 @@ func NewCmd() *cobra.Command {
return fmt.Errorf("describe PostgreSQL credential: %w", err)
}

// Show details
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return fmt.Errorf("marshal PostgreSQL credential: %w", err)
}
cmd.Println(string(details))

return nil
return outputResult(cmd, model.OutputFormat, resp)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -92,3 +86,26 @@ func buildRequest(ctx context.Context, model *flagModel, apiClient *postgresql.A
req := apiClient.GetCredentials(ctx, model.ProjectId, model.InstanceId, model.CredentialId)
return req
}

func outputResult(cmd *cobra.Command, outputFormat string, credential *postgresql.CredentialsResponse) error {
switch outputFormat {
case globalflags.TableOutputFormat:
table := tables.NewTable()
table.SetHeader("ID")
table.AddRow(*credential.Id)
err := table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
default:
details, err := json.MarshalIndent(credential, "", " ")
if err != nil {
return fmt.Errorf("marshal PostgreSQL credential: %w", err)
}
cmd.Println(string(details))

return nil
}
}
42 changes: 28 additions & 14 deletions internal/cmd/postgresql/credential/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package list

import (
"context"
"encoding/json"
"fmt"

"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
Expand Down Expand Up @@ -60,20 +61,7 @@ func NewCmd() *cobra.Command {
if model.Limit != nil && len(credentials) > int(*model.Limit) {
credentials = credentials[:*model.Limit]
}

// Show output as table
table := tables.NewTable()
table.SetHeader("ID")
for i := range credentials {
c := credentials[i]
table.AddRow(*c.Id)
}
err = table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
return outputResult(cmd, model.OutputFormat, credentials)
},
}
configureFlags(cmd)
Expand Down Expand Up @@ -110,3 +98,29 @@ func buildRequest(ctx context.Context, model *flagModel, apiClient *postgresql.A
req := apiClient.GetCredentialsIds(ctx, model.ProjectId, model.InstanceId)
return req
}

func outputResult(cmd *cobra.Command, outputFormat string, credentials []postgresql.CredentialsListItem) error {
switch outputFormat {
case globalflags.JSONOutputFormat:
details, err := json.MarshalIndent(credentials, "", " ")
if err != nil {
return fmt.Errorf("marshal PostgreSQL credential list: %w", err)
}
cmd.Println(string(details))

return nil
default:
table := tables.NewTable()
table.SetHeader("ID")
for i := range credentials {
c := credentials[i]
table.AddRow(*c.Id)
}
err := table.Render(cmd)
if err != nil {
return fmt.Errorf("render table: %w", err)
}

return nil
}
}
Loading

0 comments on commit 0be5cb5

Please sign in to comment.