From 771fd890bcd9db767508e9a6989db79f0dd2c725 Mon Sep 17 00:00:00 2001 From: cmoulliard Date: Fri, 20 Dec 2024 09:53:59 +0100 Subject: [PATCH] Moving the print functions to a new package: printer Signed-off-by: cmoulliard --- pkg/cmd/get/clusters.go | 7 ++++--- pkg/cmd/get/secrets.go | 8 ++++---- pkg/printer/printer.go | 30 ++++++++++++++++++++++++++++++ pkg/util/util.go | 26 -------------------------- 4 files changed, 38 insertions(+), 33 deletions(-) create mode 100644 pkg/printer/printer.go diff --git a/pkg/cmd/get/clusters.go b/pkg/cmd/get/clusters.go index 3cc3babb..b3c8ed91 100644 --- a/pkg/cmd/get/clusters.go +++ b/pkg/cmd/get/clusters.go @@ -7,6 +7,7 @@ import ( "github.com/cnoe-io/idpbuilder/pkg/cmd/helpers" "github.com/cnoe-io/idpbuilder/pkg/k8s" "github.com/cnoe-io/idpbuilder/pkg/kind" + "github.com/cnoe-io/idpbuilder/pkg/printer" "github.com/cnoe-io/idpbuilder/pkg/util" "github.com/spf13/cobra" "io" @@ -87,11 +88,11 @@ func list(cmd *cobra.Command, args []string) error { func printClustersOutput(outWriter io.Writer, clusters []Cluster, format string) error { switch format { case "json": - return util.PrintDataAsJson(clusters, outWriter) + return printer.PrintDataAsJson(clusters, outWriter) case "yaml": - return util.PrintDataAsYaml(clusters, outWriter) + return printer.PrintDataAsYaml(clusters, outWriter) case "table": - return util.PrintTable(generateClusterTable(clusters), outWriter) + return printer.PrintTable(generateClusterTable(clusters), outWriter) default: return fmt.Errorf("output format %s is not supported", format) diff --git a/pkg/cmd/get/secrets.go b/pkg/cmd/get/secrets.go index 1a688e33..53b3840e 100644 --- a/pkg/cmd/get/secrets.go +++ b/pkg/cmd/get/secrets.go @@ -3,7 +3,7 @@ package get import ( "context" "fmt" - "github.com/cnoe-io/idpbuilder/pkg/util" + "github.com/cnoe-io/idpbuilder/pkg/printer" "io" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "os" @@ -206,11 +206,11 @@ func generateSecretTable(secretTable []Secret) metav1.Table { func printSecretsOutput(outWriter io.Writer, secrets []Secret, format string) error { switch format { case "json": - return util.PrintDataAsJson(secrets, outWriter) + return printer.PrintDataAsJson(secrets, outWriter) case "yaml": - return util.PrintDataAsYaml(secrets, outWriter) + return printer.PrintDataAsYaml(secrets, outWriter) case "table": - return util.PrintTable(generateSecretTable(secrets), outWriter) + return printer.PrintTable(generateSecretTable(secrets), outWriter) default: return fmt.Errorf("output format %s is not supported", format) } diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go new file mode 100644 index 00000000..1c31240b --- /dev/null +++ b/pkg/printer/printer.go @@ -0,0 +1,30 @@ +package printer + +import ( + "encoding/json" + "io" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/cli-runtime/pkg/printers" + "sigs.k8s.io/yaml" +) + +func PrintTable(table metav1.Table, outWriter io.Writer) error { + printer := printers.NewTablePrinter(printers.PrintOptions{}) + return printer.PrintObj(&table, outWriter) +} + +func PrintDataAsJson(data any, outWriter io.Writer) error { + enc := json.NewEncoder(outWriter) + enc.SetEscapeHTML(false) + enc.SetIndent("", " ") + return enc.Encode(data) +} + +func PrintDataAsYaml(data any, outWriter io.Writer) error { + b, err := yaml.Marshal(data) + if err != nil { + return err + } + _, err = outWriter.Write(b) + return err +} diff --git a/pkg/util/util.go b/pkg/util/util.go index 4a21b8c1..dbd606fe 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -4,10 +4,7 @@ import ( "context" "crypto/rand" "crypto/tls" - "encoding/json" "fmt" - "io" - "k8s.io/cli-runtime/pkg/printers" "math" "math/big" mathrand "math/rand" @@ -19,11 +16,9 @@ import ( "time" "github.com/cnoe-io/idpbuilder/api/v1alpha1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/kind/pkg/cluster" - "sigs.k8s.io/yaml" ) const ( @@ -188,24 +183,3 @@ func SetPackageLabels(obj client.Object) { labels[v1alpha1.PackageTypeLabelKey] = v1alpha1.PackageTypeLabelCustom } } - -func PrintTable(table metav1.Table, outWriter io.Writer) error { - printer := printers.NewTablePrinter(printers.PrintOptions{}) - return printer.PrintObj(&table, outWriter) -} - -func PrintDataAsJson(data any, outWriter io.Writer) error { - enc := json.NewEncoder(outWriter) - enc.SetEscapeHTML(false) - enc.SetIndent("", " ") - return enc.Encode(data) -} - -func PrintDataAsYaml(data any, outWriter io.Writer) error { - b, err := yaml.Marshal(data) - if err != nil { - return err - } - _, err = outWriter.Write(b) - return err -}