From 666bfc71490eb24e0a47c21c08e69978bd4ebfda Mon Sep 17 00:00:00 2001 From: cmoulliard Date: Thu, 19 Dec 2024 15:33:06 +0100 Subject: [PATCH] Pass the writer using stdout to the functions Signed-off-by: cmoulliard --- pkg/cmd/get/clusters.go | 12 +++++++----- pkg/cmd/get/secrets.go | 23 ++++++++++++----------- pkg/cmd/get/secrets_test.go | 7 ++++--- pkg/util/util.go | 37 +++++++++---------------------------- 4 files changed, 32 insertions(+), 47 deletions(-) diff --git a/pkg/cmd/get/clusters.go b/pkg/cmd/get/clusters.go index 5c29d628..8d880765 100644 --- a/pkg/cmd/get/clusters.go +++ b/pkg/cmd/get/clusters.go @@ -9,12 +9,14 @@ import ( "github.com/cnoe-io/idpbuilder/pkg/kind" "github.com/cnoe-io/idpbuilder/pkg/util" "github.com/spf13/cobra" + "io" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd/api" + "os" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/kind/pkg/cluster" "slices" @@ -73,7 +75,7 @@ func list(cmd *cobra.Command, args []string) error { return err } else { // Convert the list of the clusters to a Table of clusters and print the table using the format selected - err := printClustersOutput(clusters, outputFormat) + err := printClustersOutput(os.Stdout, clusters, outputFormat) if err != nil { return err } else { @@ -82,14 +84,14 @@ func list(cmd *cobra.Command, args []string) error { } } -func printClustersOutput(clusters []Cluster, format string) error { +func printClustersOutput(outWriter io.Writer, clusters []Cluster, format string) error { switch format { case "json": - return util.PrintDataAsJson(clusters) + return util.PrintDataAsJson(clusters, outWriter) case "yaml": - return util.PrintDataAsYaml(clusters) + return util.PrintDataAsYaml(clusters, outWriter) case "": - return util.PrintTable(generateClusterTable(clusters)) + return util.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 f9f2edab..b09a24f9 100644 --- a/pkg/cmd/get/secrets.go +++ b/pkg/cmd/get/secrets.go @@ -4,7 +4,9 @@ import ( "context" "fmt" "github.com/cnoe-io/idpbuilder/pkg/util" + "io" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "os" "path/filepath" "github.com/cnoe-io/idpbuilder/api/v1alpha1" @@ -79,13 +81,13 @@ func getSecretsE(cmd *cobra.Command, args []string) error { } if len(packages) == 0 { - return printAllPackageSecrets(ctx, kubeClient, outputFormat) + return printAllPackageSecrets(ctx, os.Stdout, kubeClient, outputFormat) } - return printPackageSecrets(ctx, kubeClient, outputFormat) + return printPackageSecrets(ctx, os.Stdout, kubeClient, outputFormat) } -func printAllPackageSecrets(ctx context.Context, kubeClient client.Client, format string) error { +func printAllPackageSecrets(ctx context.Context, outWriter io.Writer, kubeClient client.Client, format string) error { selector := labels.NewSelector() secrets := []Secret{} @@ -115,10 +117,10 @@ func printAllPackageSecrets(ctx context.Context, kubeClient client.Client, forma fmt.Println("no secrets found") return nil } - return printSecretsOutput(secrets, format) + return printSecretsOutput(outWriter, secrets, format) } -func printPackageSecrets(ctx context.Context, kubeClient client.Client, format string) error { +func printPackageSecrets(ctx context.Context, outWriter io.Writer, kubeClient client.Client, format string) error { selector := labels.NewSelector() secrets := []Secret{} @@ -161,7 +163,7 @@ func printPackageSecrets(ctx context.Context, kubeClient client.Client, format s } } - return printSecretsOutput(secrets, format) + return printSecretsOutput(outWriter, secrets, format) } func generateSecretTable(secretTable []Secret) metav1.Table { @@ -188,16 +190,15 @@ func generateSecretTable(secretTable []Secret) metav1.Table { return *table } -func printSecretsOutput(secrets []Secret, format string) error { +func printSecretsOutput(outWriter io.Writer, secrets []Secret, format string) error { switch format { case "json": - return util.PrintDataAsJson(secrets) + return util.PrintDataAsJson(secrets, outWriter) case "yaml": - return util.PrintDataAsYaml(secrets) + return util.PrintDataAsYaml(secrets, outWriter) case "": - return util.PrintTable(generateSecretTable(secrets)) + return util.PrintTable(generateSecretTable(secrets), outWriter) default: - return fmt.Errorf("output format %s is not supported", format) } } diff --git a/pkg/cmd/get/secrets_test.go b/pkg/cmd/get/secrets_test.go index 83e7a23a..d55572ef 100644 --- a/pkg/cmd/get/secrets_test.go +++ b/pkg/cmd/get/secrets_test.go @@ -4,6 +4,7 @@ import ( "bytes" "context" "encoding/json" + "io" "testing" "github.com/cnoe-io/idpbuilder/api/v1alpha1" @@ -96,7 +97,7 @@ func TestPrintPackageSecrets(t *testing.T) { fClient.On("Get", ctx, c.getKeys[j], mock.Anything, mock.Anything).Return(c.err) } - err := printPackageSecrets(ctx, fClient, "") + err := printPackageSecrets(ctx, io.Discard, fClient, "") fClient.AssertExpectations(t) assert.Nil(t, err) } @@ -133,7 +134,7 @@ func TestPrintAllPackageSecrets(t *testing.T) { for j := range c.getKeys { fClient.On("Get", ctx, c.getKeys[j], mock.Anything, mock.Anything).Return(c.err) } - err := printAllPackageSecrets(ctx, fClient, "") + err := printAllPackageSecrets(ctx, io.Discard, fClient, "") fClient.AssertExpectations(t) assert.Nil(t, err) } @@ -211,7 +212,7 @@ func TestOutput(t *testing.T) { var b []byte buffer := bytes.NewBuffer(b) - err := printAllPackageSecrets(ctx, fClient, "json") + err := printAllPackageSecrets(ctx, io.Discard, fClient, "json") fClient.AssertExpectations(t) assert.Nil(t, err) diff --git a/pkg/util/util.go b/pkg/util/util.go index a89839af..4a21b8c1 100644 --- a/pkg/util/util.go +++ b/pkg/util/util.go @@ -1,12 +1,12 @@ package util import ( - "bytes" "context" "crypto/rand" "crypto/tls" "encoding/json" "fmt" + "io" "k8s.io/cli-runtime/pkg/printers" "math" "math/big" @@ -189,42 +189,23 @@ func SetPackageLabels(obj client.Object) { } } -func PrintTable(table metav1.Table) error { - out := bytes.NewBuffer([]byte{}) +func PrintTable(table metav1.Table, outWriter io.Writer) error { printer := printers.NewTablePrinter(printers.PrintOptions{}) - err := printer.PrintObj(&table, out) - if err != nil { - return fmt.Errorf("failed to print the table %w", err) - } - fmt.Println(out) - return nil + return printer.PrintObj(&table, outWriter) } -func PrintDataAsJson(data any) error { - out := bytes.NewBuffer([]byte{}) - enc := json.NewEncoder(out) +func PrintDataAsJson(data any, outWriter io.Writer) error { + enc := json.NewEncoder(outWriter) enc.SetEscapeHTML(false) enc.SetIndent("", " ") - if err := enc.Encode(data); err != nil { - return err - } else { - fmt.Println(out) - return nil - } - + return enc.Encode(data) } -func PrintDataAsYaml(data any) error { - out := bytes.NewBuffer([]byte{}) +func PrintDataAsYaml(data any, outWriter io.Writer) error { b, err := yaml.Marshal(data) if err != nil { return err } - _, err = out.Write(b) - if err != nil { - return err - } else { - fmt.Println(out) - return nil - } + _, err = outWriter.Write(b) + return err }