Skip to content

Commit

Permalink
Pass the writer using stdout to the functions
Browse files Browse the repository at this point in the history
Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Dec 19, 2024
1 parent 9fb9c76 commit 666bfc7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 47 deletions.
12 changes: 7 additions & 5 deletions pkg/cmd/get/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
23 changes: 12 additions & 11 deletions pkg/cmd/get/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{}

Expand Down Expand Up @@ -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{}

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/cmd/get/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"io"
"testing"

"github.com/cnoe-io/idpbuilder/api/v1alpha1"
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)

Expand Down
37 changes: 9 additions & 28 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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
}

0 comments on commit 666bfc7

Please sign in to comment.