From a0d8b95919665ad240e26e50ede6c938207119b5 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Tue, 11 Jun 2024 13:38:17 +0200 Subject: [PATCH] Use printers from genericcli except for table printer. (#302) --- cmd/audit.go | 5 ++-- cmd/billing.go | 21 +++++++------- cmd/cluster.go | 50 ++++++++++++++++----------------- cmd/context.go | 3 +- cmd/health.go | 5 ++-- cmd/ip.go | 11 ++++---- cmd/output/printer.go | 64 ++----------------------------------------- cmd/postgres.go | 47 ++++++++++++++++--------------- cmd/printers.go | 46 +++++++++++++++++++++++++++++++ cmd/project.go | 15 +++++----- cmd/root.go | 18 ++++++++---- cmd/s3.go | 17 ++++++------ cmd/tenant.go | 11 ++++---- cmd/version.go | 3 +- cmd/version_test.go | 46 +++++++++++-------------------- cmd/volume.go | 20 +++++++------- 16 files changed, 176 insertions(+), 206 deletions(-) create mode 100644 cmd/printers.go diff --git a/cmd/audit.go b/cmd/audit.go index 4c95e046..6ac277ea 100644 --- a/cmd/audit.go +++ b/cmd/audit.go @@ -8,7 +8,6 @@ import ( "github.com/fi-ts/cloud-go/api/client/audit" "github.com/fi-ts/cloud-go/api/models" - "github.com/fi-ts/cloudctl/cmd/output" "github.com/go-openapi/strfmt" "github.com/metal-stack/metal-lib/pkg/genericcli" "github.com/spf13/cobra" @@ -106,7 +105,7 @@ func (c *config) auditList() error { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) auditDescribe(args []string) error { @@ -139,7 +138,7 @@ func (c *config) auditDescribe(args []string) error { } } - return output.New().Print(trace) + return c.describePrinter.Print(trace) } func eventuallyRelativeDateTime(s string) (strfmt.DateTime, error) { diff --git a/cmd/billing.go b/cmd/billing.go index da886f92..b29a30de 100644 --- a/cmd/billing.go +++ b/cmd/billing.go @@ -6,7 +6,6 @@ import ( "github.com/fi-ts/cloud-go/api/client/accounting" "github.com/fi-ts/cloud-go/api/models" - "github.com/fi-ts/cloudctl/cmd/output" "github.com/go-openapi/strfmt" "github.com/go-playground/validator/v10" "github.com/jinzhu/now" @@ -413,7 +412,7 @@ func (c *config) projectsBilling() error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) clusterUsage() error { @@ -447,7 +446,7 @@ func (c *config) clusterUsageJSON(cur *models.V1ClusterUsageRequest) error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) clusterUsageCSV(cur *models.V1ClusterUsageRequest) error { @@ -493,7 +492,7 @@ func (c *config) machineUsage() error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) productOptionUsage() error { @@ -520,7 +519,7 @@ func (c *config) productOptionUsage() error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) containerUsage() error { @@ -560,7 +559,7 @@ func (c *config) containerUsageJSON(cur *models.V1ContainerUsageRequest) error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) containerUsageCSV(cur *models.V1ContainerUsageRequest) error { @@ -607,7 +606,7 @@ func (c *config) ipUsageJSON(iur *models.V1IPUsageRequest) error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) ipUsageCSV(iur *models.V1IPUsageRequest) error { @@ -657,7 +656,7 @@ func (c *config) networkTrafficUsageJSON(cur *models.V1NetworkUsageRequest) erro return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) networkTrafficUsageCSV(cur *models.V1NetworkUsageRequest) error { @@ -701,7 +700,7 @@ func (c *config) s3UsageJSON(sur *models.V1S3UsageRequest) error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) s3UsageCSV(sur *models.V1S3UsageRequest) error { @@ -754,7 +753,7 @@ func (c *config) volumeUsageJSON(vur *models.V1VolumeUsageRequest) error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) volumeUsageCSV(vur *models.V1VolumeUsageRequest) error { @@ -807,7 +806,7 @@ func (c *config) postgresUsageJSON(cur *models.V1PostgresUsageRequest) error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) postgresUsageCSV(cur *models.V1PostgresUsageRequest) error { diff --git a/cmd/cluster.go b/cmd/cluster.go index 3ebc08fc..0afa5ff3 100644 --- a/cmd/cluster.go +++ b/cmd/cluster.go @@ -719,7 +719,7 @@ WARNING: You are going to create a cluster that has no default internet access w if err != nil { return err } - return output.New().Print(shoot.Payload) + return c.describePrinter.Print(shoot.Payload) } func (c *config) clusterList() error { @@ -775,7 +775,7 @@ func (c *config) clusterList() error { if err != nil { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } request := cluster.NewListClustersParams() @@ -783,7 +783,7 @@ func (c *config) clusterList() error { if err != nil { return err } - return output.New().Print(shoots.Payload) + return c.listPrinter.Print(shoots.Payload) } func (c *config) clusterKubeconfig(args []string) error { @@ -896,7 +896,7 @@ func (c *config) reconcileCluster(args []string) error { if err != nil { return err } - return output.New().Print(shoot.Payload) + return c.describePrinter.Print(shoot.Payload) } func (c *config) updateCluster(args []string) error { @@ -1341,7 +1341,7 @@ func (c *config) updateCluster(args []string) error { if err != nil { return err } - return output.New().Print(shoot.Payload) + return c.describePrinter.Print(shoot.Payload) } func (c *config) clusterDelete(args []string) error { @@ -1360,7 +1360,7 @@ func (c *config) clusterDelete(args []string) error { return err } - genericcli.Must(output.New().Print(resp.Payload)) + genericcli.Must(c.listPrinter.Print(resp.Payload)) firstPartOfClusterID := strings.Split(*resp.Payload.ID, "-")[0] fmt.Println("Please answer some security questions to delete this cluster") @@ -1379,7 +1379,7 @@ func (c *config) clusterDelete(args []string) error { if err != nil { return err } - return output.New().Print(cl.Payload) + return c.describePrinter.Print(cl.Payload) } func (c *config) clusterDescribe(args []string) error { @@ -1396,7 +1396,7 @@ func (c *config) clusterDescribe(args []string) error { if err != nil { return err } - return output.New().Print(shoot.Payload) + return c.describePrinter.Print(shoot.Payload) } func (c *config) clusterIssues(args []string) error { @@ -1435,7 +1435,7 @@ func (c *config) clusterIssues(args []string) error { if err != nil { return err } - return output.New().Print(output.ShootIssuesResponses(response.Payload)) + return c.listPrinter.Print(output.ShootIssuesResponses(response.Payload)) } request := cluster.NewListClustersParams().WithReturnMachines(&boolTrue) @@ -1443,7 +1443,7 @@ func (c *config) clusterIssues(args []string) error { if err != nil { return err } - return output.New().Print(output.ShootIssuesResponses(shoots.Payload)) + return c.listPrinter.Print(output.ShootIssuesResponses(shoots.Payload)) } ci, err := c.clusterID("issues", args) @@ -1456,7 +1456,7 @@ func (c *config) clusterIssues(args []string) error { if err != nil { return err } - return output.New().Print(output.ShootIssuesResponse(shoot.Payload)) + return c.listPrinter.Print(output.ShootIssuesResponse(shoot.Payload)) } func (c *config) clusterMachines(args []string) error { @@ -1471,17 +1471,17 @@ func (c *config) clusterMachines(args []string) error { return err } - if output.New().Type() != "table" { - return output.New().Print(shoot.Payload) + if viper.GetString("output-format") != "table" { + return c.describePrinter.Print(shoot.Payload) } fmt.Println("Cluster:") - genericcli.Must(output.New().Print(shoot.Payload)) + genericcli.Must(c.listPrinter.Print(shoot.Payload)) ms := shoot.Payload.Machines ms = append(ms, shoot.Payload.Firewalls...) fmt.Println("\nMachines:") - return output.New().Print(ms) + return c.listPrinter.Print(ms) } func (c *config) clusterLogs(args []string) error { @@ -1504,13 +1504,13 @@ func (c *config) clusterLogs(args []string) error { lastErrors = shoot.Payload.Status.LastErrors } - if output.New().Type() != "table" { + if viper.GetString("output-format") != "table" { type s struct { Conditions []*models.V1beta1Condition LastOperation *models.V1beta1LastOperation LastErrors []*models.V1beta1LastError } - return output.New().Print(s{ + return c.describePrinter.Print(s{ Conditions: conditions, LastOperation: lastOperation, LastErrors: lastErrors, @@ -1518,19 +1518,19 @@ func (c *config) clusterLogs(args []string) error { } fmt.Println("Conditions:") - err = output.New().Print(conditions) + err = c.listPrinter.Print(conditions) if err != nil { return err } fmt.Println("\nLast Errors:") - err = output.New().Print(lastErrors) + err = c.listPrinter.Print(lastErrors) if err != nil { return err } fmt.Println("\nLast Operation:") - return output.New().Print(lastOperation) + return c.listPrinter.Print(lastOperation) } func (c *config) clusterInputs() error { @@ -1544,7 +1544,7 @@ func (c *config) clusterInputs() error { return err } - return output.New().Print(sc) + return c.describePrinter.Print(sc.Payload) } func (c *config) clusterDNSManifest(args []string) error { @@ -1688,7 +1688,7 @@ func (c *config) clusterMachineReset(args []string) error { ms := shoot.Payload.Machines ms = append(ms, shoot.Payload.Firewalls...) - return output.New().Print(ms) + return c.listPrinter.Print(ms) } func (c *config) clusterMachineCycle(args []string) error { @@ -1710,7 +1710,7 @@ func (c *config) clusterMachineCycle(args []string) error { ms := shoot.Payload.Machines ms = append(ms, shoot.Payload.Firewalls...) - return output.New().Print(ms) + return c.listPrinter.Print(ms) } func (c *config) clusterMachineReinstall(args []string) error { @@ -1736,7 +1736,7 @@ func (c *config) clusterMachineReinstall(args []string) error { ms := shoot.Payload.Machines ms = append(ms, shoot.Payload.Firewalls...) - return output.New().Print(ms) + return c.listPrinter.Print(ms) } func (c *config) clusterMachinePackages(args []string) error { @@ -1800,7 +1800,7 @@ func (c *config) clusterMonitoringSecret(args []string) error { return err } - return output.New().Print(secret.Payload) + return c.describePrinter.Print(secret.Payload) } func (c *config) clusterMachineSSH(args []string, console bool) error { diff --git a/cmd/context.go b/cmd/context.go index 674b2b29..84d80db0 100644 --- a/cmd/context.go +++ b/cmd/context.go @@ -4,7 +4,6 @@ import ( "fmt" "github.com/fatih/color" - "github.com/fi-ts/cloudctl/cmd/output" "github.com/fi-ts/cloudctl/pkg/api" "github.com/spf13/cobra" ) @@ -111,5 +110,5 @@ func (c *config) contextList() error { if err != nil { return err } - return output.New().Print(ctxs) + return c.listPrinter.Print(ctxs) } diff --git a/cmd/health.go b/cmd/health.go index 32cbfa4e..0f07dc34 100644 --- a/cmd/health.go +++ b/cmd/health.go @@ -5,7 +5,6 @@ import ( "fmt" "github.com/fi-ts/cloud-go/api/client/health" - "github.com/fi-ts/cloudctl/cmd/output" "github.com/metal-stack/metal-lib/pkg/genericcli" "github.com/spf13/cobra" ) @@ -26,11 +25,11 @@ func newHealthCmd(c *config) *cobra.Command { } } - genericcli.Must(output.New().Print(resp.Payload)) + genericcli.Must(c.listPrinter.Print(resp.Payload)) fmt.Println() - return output.New().Print(resp.Payload.Services) + return c.listPrinter.Print(resp.Payload.Services) }, } return healthCmd diff --git a/cmd/ip.go b/cmd/ip.go index c1cfb7fd..e0353523 100644 --- a/cmd/ip.go +++ b/cmd/ip.go @@ -9,7 +9,6 @@ import ( "github.com/fi-ts/cloud-go/api/models" "github.com/fi-ts/cloudctl/cmd/helper" - "github.com/fi-ts/cloudctl/cmd/output" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -100,13 +99,13 @@ func (c *config) ipList() error { if err != nil { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } resp, err := c.cloud.IP.ListIPs(nil, nil) if err != nil { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) ipStatic(args []string) error { @@ -140,7 +139,7 @@ func (c *config) ipStatic(args []string) error { if err != nil { return err } - return output.New().Print(resp.Payload) + return c.describePrinter.Print(resp.Payload) } func (c *config) ipAllocate() error { @@ -171,7 +170,7 @@ func (c *config) ipAllocate() error { if err != nil { return err } - return output.New().Print(resp.Payload) + return c.describePrinter.Print(resp.Payload) } func (c *config) ipFree(args []string) error { @@ -187,7 +186,7 @@ func (c *config) ipFree(args []string) error { return err } - return output.New().Print(resp.Payload) + return c.describePrinter.Print(resp.Payload) } func (c *config) getIPFromArgs(args []string) (string, error) { diff --git a/cmd/output/printer.go b/cmd/output/printer.go index 61609dd2..3d326504 100644 --- a/cmd/output/printer.go +++ b/cmd/output/printer.go @@ -10,22 +10,20 @@ import ( "text/template" "github.com/fatih/color" - "github.com/fi-ts/cloud-go/api/client/cluster" "github.com/fi-ts/cloud-go/api/models" "github.com/fi-ts/cloudctl/pkg/api" sprig "github.com/go-task/slim-sprig/v3" + "github.com/metal-stack/metal-lib/pkg/genericcli/printers" "github.com/metal-stack/metal-lib/pkg/pointer" "github.com/spf13/viper" "github.com/olekukonko/tablewriter" - "gopkg.in/yaml.v3" ) type ( // Printer main Interface for implementations which spits out to specified Writer Printer interface { Print(data interface{}) error - Type() string } tablePrinter struct { table *tablewriter.Table @@ -40,14 +38,6 @@ type ( wideData [][]string outWriter io.Writer } - // jsonPrinter returns the model in json format - jsonPrinter struct { - outWriter io.Writer - } - // yamlPrinter returns the model in yaml format - yamlPrinter struct { - outWriter io.Writer - } ) // render the table shortHeader and shortData are always expected. @@ -155,13 +145,9 @@ func newPrinter(format, order, tpl string, noHeaders bool, writer io.Writer) (Pr var printer Printer switch format { case "yaml": - printer = &yamlPrinter{ - outWriter: writer, - } + printer = printers.NewYAMLPrinter().WithOut(writer) case "json": - printer = &jsonPrinter{ - outWriter: writer, - } + printer = printers.NewJSONPrinter().WithOut(writer) case "table", "wide": printer = newTablePrinter(format, order, noHeaders, nil, writer) case "template": @@ -316,54 +302,10 @@ func (t tablePrinter) Print(data interface{}) error { PostgresBackupEntryTablePrinter{t}.Print(d) case []*models.V1S3PartitionResponse: S3PartitionTablePrinter{t}.Print(d) - case *models.V1ClusterMonitoringSecretResponse: - return yamlPrinter{ - outWriter: t.outWriter, - }.Print(d) - case *models.V1S3CredentialsResponse, *models.V1S3Response: - return yamlPrinter{ - outWriter: t.outWriter, - }.Print(d) case *api.Contexts: ContextPrinter{t}.Print(d) - case api.Version: - return yamlPrinter{ - outWriter: t.outWriter, - }.Print(d) - case *cluster.ListConstraintsOK: - return yamlPrinter{ - outWriter: t.outWriter, - }.Print(d) default: return fmt.Errorf("unknown table printer for type: %T", d) } return nil } - -// Print a model in json format -func (j jsonPrinter) Print(data interface{}) error { - json, err := json.MarshalIndent(data, "", " ") - if err != nil { - return fmt.Errorf("unable to marshal to json:%w", err) - } - fmt.Fprintf(j.outWriter, "%s\n", string(json)) - return nil -} - -func (j jsonPrinter) Type() string { - return "json" -} - -// Print a model in yaml format -func (y yamlPrinter) Print(data interface{}) error { - yml, err := yaml.Marshal(data) - if err != nil { - return fmt.Errorf("unable to marshal to yaml:%w", err) - } - fmt.Fprintf(y.outWriter, "%s", string(yml)) - return nil -} - -func (y yamlPrinter) Type() string { - return "yaml" -} diff --git a/cmd/postgres.go b/cmd/postgres.go index db759739..95781099 100644 --- a/cmd/postgres.go +++ b/cmd/postgres.go @@ -8,7 +8,6 @@ import ( "github.com/fi-ts/cloud-go/api/client/database" "github.com/fi-ts/cloud-go/api/models" "github.com/fi-ts/cloudctl/cmd/helper" - "github.com/fi-ts/cloudctl/cmd/output" "github.com/metal-stack/metal-lib/pkg/genericcli" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -452,7 +451,7 @@ func (c *config) postgresCreate() error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) postgresCreateStandby() error { @@ -496,7 +495,7 @@ func (c *config) postgresCreateStandby() error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) postgresPromoteToPrimary(args []string) error { @@ -540,7 +539,7 @@ func (c *config) postgresPromoteToPrimary(args []string) error { if err != nil { return err } - return output.New().Print(uresp.Payload) + return c.listPrinter.Print(uresp.Payload) } func (c *config) postgresDemoteToStandby(args []string) error { @@ -580,7 +579,7 @@ func (c *config) postgresDemoteToStandby(args []string) error { if err != nil { return err } - return output.New().Print(uresp.Payload) + return c.listPrinter.Print(uresp.Payload) } func (c *config) postgresRestore() error { @@ -613,7 +612,7 @@ func (c *config) postgresRestore() error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) postgresApply() error { @@ -685,7 +684,7 @@ func (c *config) postgresApply() error { response = append(response, createdPG.Payload) continue } - return output.New().Print(response) + return c.listPrinter.Print(response) } func (c *config) postgresEdit(args []string) error { @@ -720,7 +719,7 @@ func (c *config) postgresEdit(args []string) error { if err != nil { return err } - return output.New().Print(uresp.Payload) + return c.listPrinter.Print(uresp.Payload) } return helper.Edit(id, getFunc, updateFunc) } @@ -810,7 +809,7 @@ func (c *config) postgresUpdate(args []string) error { if err != nil { return err } - return output.New().Print(uresp.Payload) + return c.listPrinter.Print(uresp.Payload) } func (c *config) postgresAcceptRestore(args []string) error { @@ -819,7 +818,7 @@ func (c *config) postgresAcceptRestore(args []string) error { return err } - genericcli.Must(output.New().Print(pg)) + genericcli.Must(c.listPrinter.Print(pg)) fmt.Println("Has the restore finished successfully?") err = helper.Prompt("(type yes to proceed):", "yes") @@ -833,7 +832,7 @@ func (c *config) postgresAcceptRestore(args []string) error { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func readPostgresUpdateRequests(filename string) ([]models.V1PostgresUpdateRequest, error) { @@ -882,13 +881,13 @@ func (c *config) postgresFind() error { if err != nil { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } resp, err := c.cloud.Database.ListPostgres(nil, nil) if err != nil { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) postgresDelete(args []string) error { @@ -898,7 +897,7 @@ func (c *config) postgresDelete(args []string) error { } if !viper.GetBool("yes-i-really-mean-it") { - genericcli.Must(output.New().Print(pg)) + genericcli.Must(c.listPrinter.Print(pg)) idParts := strings.Split(*pg.ID, "-") firstPartOfPostgresID := idParts[0] @@ -920,7 +919,7 @@ func (c *config) postgresDelete(args []string) error { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) postgresDescribe(args []string) error { @@ -929,7 +928,7 @@ func (c *config) postgresDescribe(args []string) error { return err } - return output.New().Print(postgres) + return c.listPrinter.Print(postgres) } func (c *config) postgresListBackups(args []string) error { @@ -943,7 +942,7 @@ func (c *config) postgresListBackups(args []string) error { if err != nil { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) postgresConnectionString(args []string) error { @@ -1032,7 +1031,7 @@ func (c *config) postgresBackupCreate(autocreate bool) error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) postgresBackupUpdate() error { id := viper.GetString("id") @@ -1067,7 +1066,7 @@ func (c *config) postgresBackupUpdate() error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) postgresBackupList() error { @@ -1077,7 +1076,7 @@ func (c *config) postgresBackupList() error { if err != nil { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) postgresBackupDescribe(args []string) error { @@ -1094,7 +1093,7 @@ func (c *config) postgresBackupDescribe(args []string) error { if err != nil { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) postgresBackupDelete(args []string) error { if len(args) < 1 { @@ -1132,7 +1131,7 @@ func (c *config) postgresBackupDelete(args []string) error { if err != nil { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } @@ -1143,7 +1142,7 @@ func (c *config) postgresVersions() error { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) postgresPartitions() error { params := database.NewGetPostgresPartitionsParams() @@ -1152,7 +1151,7 @@ func (c *config) postgresPartitions() error { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) getPostgresFromArgs(args []string) (*models.V1PostgresResponse, error) { if len(args) < 1 { diff --git a/cmd/printers.go b/cmd/printers.go new file mode 100644 index 00000000..f887f376 --- /dev/null +++ b/cmd/printers.go @@ -0,0 +1,46 @@ +package cmd + +import ( + "io" + "log" + + "github.com/fatih/color" + "github.com/fi-ts/cloudctl/cmd/output" + "github.com/metal-stack/metal-lib/pkg/genericcli/printers" + "github.com/spf13/viper" +) + +func newPrinterFromCLI(out io.Writer) printers.Printer { + var printer printers.Printer + + switch format := viper.GetString("output-format"); format { + case "yaml": + printer = printers.NewYAMLPrinter().WithOut(out) + case "json": + printer = printers.NewJSONPrinter().WithOut(out) + case "table", "wide", "markdown": + printer = output.New() + case "template": + printer = printers.NewTemplatePrinter(viper.GetString("template")).WithOut(out) + default: + log.Fatalf("unknown output format: %q", format) + } + + if viper.IsSet("force-color") { + enabled := viper.GetBool("force-color") + if enabled { + color.NoColor = false + } else { + color.NoColor = true + } + } + + return printer +} + +func defaultToYAMLPrinter(out io.Writer) printers.Printer { + if viper.IsSet("output-format") { + return newPrinterFromCLI(out) + } + return printers.NewYAMLPrinter().WithOut(out) +} diff --git a/cmd/project.go b/cmd/project.go index 453ed9eb..a5153ac1 100644 --- a/cmd/project.go +++ b/cmd/project.go @@ -12,7 +12,6 @@ import ( "github.com/fi-ts/cloud-go/api/client/project" "github.com/fi-ts/cloudctl/cmd/helper" - "github.com/fi-ts/cloudctl/cmd/output" "github.com/spf13/cobra" "github.com/spf13/viper" ) @@ -158,7 +157,7 @@ func (c *config) projectCreate() error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) projectDescribe(args []string) error { @@ -174,7 +173,7 @@ func (c *config) projectDescribe(args []string) error { return err } - return output.New().Print(p.Payload) + return c.listPrinter.Print(p.Payload) } func (c *config) projectDelete(args []string) error { @@ -190,7 +189,7 @@ func (c *config) projectDelete(args []string) error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } func (c *config) projectList() error { @@ -209,7 +208,7 @@ func (c *config) projectList() error { return err } - return output.New().Print(response.Payload.Projects) + return c.listPrinter.Print(response.Payload.Projects) } request := project.NewListProjectsParams() @@ -217,7 +216,7 @@ func (c *config) projectList() error { if err != nil { return err } - return output.New().Print(response.Payload.Projects) + return c.listPrinter.Print(response.Payload.Projects) } func (c *config) projectID(verb string, args []string) (string, error) { @@ -294,7 +293,7 @@ func (c *config) projectApply() error { continue } } - return output.New().Print(response) + return c.listPrinter.Print(response) } func (c *config) projectEdit(args []string) error { @@ -330,7 +329,7 @@ func (c *config) projectEdit(args []string) error { if err != nil { return err } - return output.New().Print(uresp.Payload) + return c.listPrinter.Print(uresp.Payload) } return helper.Edit(id, getFunc, updateFunc) diff --git a/cmd/root.go b/cmd/root.go index 7718851d..dbc0baf5 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -15,6 +15,7 @@ import ( "github.com/fi-ts/cloudctl/cmd/helper" "github.com/fi-ts/cloudctl/pkg/api" "github.com/metal-stack/metal-lib/pkg/genericcli" + "github.com/metal-stack/metal-lib/pkg/genericcli/printers" "github.com/spf13/afero" "github.com/spf13/cobra" "github.com/spf13/viper" @@ -25,12 +26,14 @@ const ( ) type config struct { - fs afero.Fs - out io.Writer - cloud *client.CloudAPI - comp *completion.Completion - consoleHost string - log *slog.Logger + fs afero.Fs + out io.Writer + cloud *client.CloudAPI + comp *completion.Completion + consoleHost string + log *slog.Logger + describePrinter printers.Printer + listPrinter printers.Printer } func newRootCmd(cfg *config) *cobra.Command { @@ -151,6 +154,9 @@ func initConfigWithViperCtx(cfg *config) error { ctx := api.MustDefaultContext() + cfg.listPrinter = newPrinterFromCLI(cfg.out) + cfg.describePrinter = defaultToYAMLPrinter(cfg.out) + opts := &slog.HandlerOptions{} if viper.GetBool("debug") { opts.Level = slog.LevelDebug diff --git a/cmd/s3.go b/cmd/s3.go index 6c1eb3eb..e98c74ba 100644 --- a/cmd/s3.go +++ b/cmd/s3.go @@ -6,7 +6,6 @@ import ( "fmt" "github.com/fi-ts/cloud-go/api/models" - "github.com/fi-ts/cloudctl/cmd/output" "github.com/metal-stack/metal-lib/pkg/genericcli" "github.com/fi-ts/cloud-go/api/client/s3" @@ -189,7 +188,7 @@ func (c *config) s3Describe() error { default: return fmt.Errorf("unsupported s3 client configuration:%s", client) } - return output.New().Print(response.Payload) + return c.describePrinter.Print(response.Payload) } func (c *config) s3Create() error { @@ -226,7 +225,7 @@ func (c *config) s3Create() error { return err } - return output.New().Print(response.Payload) + return c.describePrinter.Print(response.Payload) } func (c *config) s3Delete() error { @@ -255,7 +254,7 @@ func (c *config) s3Delete() error { return err } - return output.New().Print(response.Payload) + return c.describePrinter.Print(response.Payload) } func (c *config) s3AddKey() error { @@ -287,7 +286,7 @@ func (c *config) s3AddKey() error { return err } - return output.New().Print(response.Payload) + return c.describePrinter.Print(response.Payload) } func (c *config) s3RemoveKey() error { @@ -315,7 +314,7 @@ func (c *config) s3RemoveKey() error { return err } - return output.New().Print(response.Payload) + return c.describePrinter.Print(response.Payload) } func (c *config) s3List() error { @@ -335,7 +334,7 @@ func (c *config) s3List() error { } if project == "" { - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } var result []*models.V1S3Response @@ -344,7 +343,7 @@ func (c *config) s3List() error { result = append(result, s3) } } - return output.New().Print(result) + return c.listPrinter.Print(result) } func (c *config) s3ListPartitions() error { @@ -354,5 +353,5 @@ func (c *config) s3ListPartitions() error { if err != nil { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } diff --git a/cmd/tenant.go b/cmd/tenant.go index 79fa2ed7..20ca7c64 100644 --- a/cmd/tenant.go +++ b/cmd/tenant.go @@ -7,7 +7,6 @@ import ( "github.com/fi-ts/cloud-go/api/models" "github.com/fi-ts/cloudctl/cmd/helper" - "github.com/fi-ts/cloudctl/cmd/output" "github.com/metal-stack/metal-lib/pkg/genericcli" "gopkg.in/yaml.v3" @@ -97,7 +96,7 @@ func (c *config) tenantDescribe(args []string) error { if err != nil { return fmt.Errorf("tenant describe error:%w", err) } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) tenantList() error { @@ -114,7 +113,7 @@ func (c *config) tenantList() error { return err } - return output.New().Print(response.Payload) + return c.listPrinter.Print(response.Payload) } request := tenant.NewListTenantsParams() @@ -122,7 +121,7 @@ func (c *config) tenantList() error { if err != nil { return fmt.Errorf("tenant list error:%w", err) } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) tenantApply() error { @@ -174,7 +173,7 @@ func (c *config) tenantApply() error { continue } } - return output.New().Print(response) + return c.listPrinter.Print(response) } func (c *config) tenantEdit(args []string) error { @@ -218,7 +217,7 @@ func (c *config) tenantEdit(args []string) error { if err != nil { return err } - return output.New().Print(uresp.Payload) + return c.listPrinter.Print(uresp.Payload) } return helper.Edit(id, getFunc, updateFunc) diff --git a/cmd/version.go b/cmd/version.go index 87be17c0..ff2e439b 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -3,7 +3,6 @@ package cmd import ( "fmt" - "github.com/fi-ts/cloudctl/cmd/output" "github.com/fi-ts/cloudctl/pkg/api" "github.com/metal-stack/v" "github.com/spf13/cobra" @@ -24,7 +23,7 @@ func newVersionCmd(c *config) *cobra.Command { v.Server = resp.Payload } - if err2 := output.New().Print(v); err2 != nil { + if err2 := c.describePrinter.Print(v); err2 != nil { return err2 } if err != nil { diff --git a/cmd/version_test.go b/cmd/version_test.go index 09185514..67f3976e 100644 --- a/cmd/version_test.go +++ b/cmd/version_test.go @@ -1,9 +1,8 @@ package cmd import ( + "bytes" "fmt" - "io" - "os" "runtime" "testing" @@ -18,13 +17,6 @@ import ( ) func Test_newVersionCmd(t *testing.T) { - rescueStdout := os.Stdout - r, w, _ := os.Pipe() - os.Stdout = w - defer func() { - os.Stdout = rescueStdout - }() - v.BuildDate = "1.1.1970" v.GitSHA1 = "abcdef" v.Revision = "v0.0.0" @@ -35,37 +27,31 @@ func Test_newVersionCmd(t *testing.T) { Version: mockVersionService, } + var out bytes.Buffer + mockVersionService.On("Info", mock.Anything, mock.Anything).Return(&version.InfoOK{Payload: &models.RestVersion{Name: pointer.Pointer("cloudctl")}}, nil) cfg := &config{ - cloud: &cloud, + cloud: &cloud, + describePrinter: defaultToYAMLPrinter(&out), } cmd := newVersionCmd(cfg) - //cmd.SetOut(w) - //cmd.SetArgs([]string{"--o", "yaml"}) + err := cmd.Execute() if err != nil { t.Fatal(err) } mockVersionService.AssertExpectations(t) - err = w.Close() - if err != nil { - t.Fatal(err) - } - - out, err := io.ReadAll(r) - if err != nil { - t.Fatal(err) - } - expected := fmt.Sprintf(`client: v0.0.0 (abcdef), v0.0.0, 1.1.1970, %s -server: - builddate: null - gitsha1: null - minclientversion: null - name: cloudctl - revision: null - version: null + expected := fmt.Sprintf(`--- +Client: v0.0.0 (abcdef), v0.0.0, 1.1.1970, %s +Server: + builddate: null + gitsha1: null + min_client_version: null + name: cloudctl + revision: null + version: null `, runtime.Version()) - assert.Equal(t, expected, string(out)) + assert.Equal(t, expected, string(out.String())) } diff --git a/cmd/volume.go b/cmd/volume.go index 718bb5ca..d49b83e6 100644 --- a/cmd/volume.go +++ b/cmd/volume.go @@ -199,7 +199,7 @@ func (c *config) volumeFind() error { if viper.GetBool("only-unbound") { volumes = onlyUnboundVolumes(volumes) } - return output.New().Print(volumes) + return c.listPrinter.Print(volumes) } resp, err := c.cloud.Volume.ListVolumes(nil, nil) if err != nil { @@ -209,7 +209,7 @@ func (c *config) volumeFind() error { if viper.GetBool("only-unbound") { volumes = onlyUnboundVolumes(volumes) } - return output.New().Print(volumes) + return c.listPrinter.Print(volumes) } func onlyUnboundVolumes(volumes []*models.V1VolumeResponse) (result []*models.V1VolumeResponse) { @@ -228,7 +228,7 @@ func (c *config) volumeDescribe(args []string) error { if err != nil { return err } - return output.New().Print(vol) + return c.listPrinter.Print(vol) } func (c *config) volumeDelete(args []string) error { @@ -257,7 +257,7 @@ If used in cronjob for example, volume might not be connected now, but required return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) volumeSetQoS(args []string) error { @@ -286,7 +286,7 @@ func (c *config) volumeSetQoS(args []string) error { if err != nil { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) volumeClusterInfo() error { @@ -295,7 +295,7 @@ func (c *config) volumeClusterInfo() error { if err != nil { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) volumeManifest(args []string) error { @@ -356,7 +356,7 @@ func (c *config) snapshotFind() error { if err != nil { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) snapshotDescribe(args []string) error { @@ -364,7 +364,7 @@ func (c *config) snapshotDescribe(args []string) error { if err != nil { return err } - return output.New().Print(snap) + return c.listPrinter.Print(snap) } func (c *config) snapshotDelete(args []string) error { @@ -389,7 +389,7 @@ delete snapshot: %q, all data will be lost forever. return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) } func (c *config) listQoSPolicies() error { @@ -397,5 +397,5 @@ func (c *config) listQoSPolicies() error { if err != nil { return err } - return output.New().Print(resp.Payload) + return c.listPrinter.Print(resp.Payload) }