Skip to content

Commit

Permalink
Moving the print functions to a new package: printer
Browse files Browse the repository at this point in the history
Signed-off-by: cmoulliard <[email protected]>
  • Loading branch information
cmoulliard committed Dec 20, 2024
1 parent 288284e commit 771fd89
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
7 changes: 4 additions & 3 deletions pkg/cmd/get/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pkg/cmd/get/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/printer/printer.go
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 0 additions & 26 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -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
}

0 comments on commit 771fd89

Please sign in to comment.