From 580042fa19690c663ccdf77de4aa90a9d6c05a60 Mon Sep 17 00:00:00 2001 From: cmoulliard Date: Fri, 20 Dec 2024 12:16:51 +0100 Subject: [PATCH] Moving the function PrintOutput to the printer package. WIP Signed-off-by: cmoulliard --- pkg/cmd/get/clusters.go | 46 +++++++++++++++++++++++++++-------------- pkg/cmd/get/secrets.go | 17 ++------------- pkg/printer/printer.go | 16 +++++++++++++- 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/pkg/cmd/get/clusters.go b/pkg/cmd/get/clusters.go index b3c8ed91..ac3c6b47 100644 --- a/pkg/cmd/get/clusters.go +++ b/pkg/cmd/get/clusters.go @@ -10,7 +10,6 @@ import ( "github.com/cnoe-io/idpbuilder/pkg/printer" "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" @@ -18,6 +17,7 @@ import ( "k8s.io/client-go/tools/clientcmd" "k8s.io/client-go/tools/clientcmd/api" "os" + "reflect" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/kind/pkg/cluster" "slices" @@ -76,7 +76,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(os.Stdout, clusters, outputFormat) + err := printer.PrintOutput(os.Stdout, clusters, generateClusterTable(clusters), outputFormat) if err != nil { return err } else { @@ -85,18 +85,22 @@ func list(cmd *cobra.Command, args []string) error { } } -func printClustersOutput(outWriter io.Writer, clusters []Cluster, format string) error { - switch format { - case "json": - return printer.PrintDataAsJson(clusters, outWriter) - case "yaml": - return printer.PrintDataAsYaml(clusters, outWriter) - case "table": - return printer.PrintTable(generateClusterTable(clusters), outWriter) - default: - - return fmt.Errorf("output format %s is not supported", format) +func ConvertTypeToAnySlice[T any](input []T) ([]any, error) { + var results []any + + for _, item := range input { + itemValue := reflect.ValueOf(item) + itemType := reflect.TypeOf(item) + + if itemValue.Type().ConvertibleTo(itemType) { + convertedValue := itemValue.Convert(itemType) + results = append(results, convertedValue.Interface()) + } else { + return nil, fmt.Errorf("item of type %s is not convertible to %s", itemValue.Type(), itemType) + } } + + return results, nil } func populateClusterList() ([]Cluster, error) { @@ -225,7 +229,7 @@ func populateClusterList() ([]Cluster, error) { return clusterList, nil } -func generateClusterTable(clusterTable []Cluster) metav1.Table { +func generateClusterTable(input []Cluster) metav1.Table { table := &metav1.Table{} table.ColumnDefinitions = []metav1.TableColumnDefinition{ {Name: "Name", Type: "string"}, @@ -235,7 +239,8 @@ func generateClusterTable(clusterTable []Cluster) metav1.Table { {Name: "Kube-Port", Type: "string"}, {Name: "Nodes", Type: "string"}, } - for _, cluster := range clusterTable { + + for _, cluster := range input { row := metav1.TableRow{ Cells: []interface{}{ cluster.Name, @@ -251,6 +256,17 @@ func generateClusterTable(clusterTable []Cluster) metav1.Table { return *table } +func convertArrayAnyToClusters(input []any) []Cluster { + var clusters []Cluster + for _, item := range input { + cluster, ok := item.(Cluster) + if ok { + clusters = append(clusters, cluster) + } + } + return clusters +} + func generateNodeData(nodes []Node) string { var result string for i, aNode := range nodes { diff --git a/pkg/cmd/get/secrets.go b/pkg/cmd/get/secrets.go index 5e885dd6..2ac6130c 100644 --- a/pkg/cmd/get/secrets.go +++ b/pkg/cmd/get/secrets.go @@ -120,7 +120,7 @@ func printAllPackageSecrets(ctx context.Context, outWriter io.Writer, kubeClient fmt.Println("no secrets found") return nil } - return printSecretsOutput(outWriter, secrets, format) + return printer.PrintOutput(outWriter, secrets, generateSecretTable(secrets), format) } func printPackageSecrets(ctx context.Context, outWriter io.Writer, kubeClient client.Client, format string) error { @@ -166,7 +166,7 @@ func printPackageSecrets(ctx context.Context, outWriter io.Writer, kubeClient cl } } - return printSecretsOutput(outWriter, secrets, format) + return printer.PrintOutput(outWriter, secrets, generateSecretTable(secrets), format) } func generateSecretTable(secretTable []Secret) metav1.Table { @@ -203,19 +203,6 @@ func generateSecretTable(secretTable []Secret) metav1.Table { return *table } -func printSecretsOutput(outWriter io.Writer, secrets []Secret, format string) error { - switch format { - case "json": - return printer.PrintDataAsJson(secrets, outWriter) - case "yaml": - return printer.PrintDataAsYaml(secrets, outWriter) - case "table": - return printer.PrintTable(generateSecretTable(secrets), outWriter) - default: - return fmt.Errorf("output format %s is not supported", format) - } -} - func populateSecret(s v1.Secret, isCoreSecret bool) Secret { secret := Secret{ Name: s.Name, diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index 1c31240b..ae54f3a6 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -2,13 +2,27 @@ package printer import ( "encoding/json" + "fmt" "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 { +func PrintOutput[T any](outWriter io.Writer, input []T, inputTable metav1.Table, format string) error { + switch format { + case "json": + return PrintDataAsJson(input, outWriter) + case "yaml": + return PrintDataAsYaml(input, outWriter) + case "table": + return PrintDataAsTable(inputTable, outWriter) + default: + return fmt.Errorf("output format %s is not supported", format) + } +} + +func PrintDataAsTable(table metav1.Table, outWriter io.Writer) error { printer := printers.NewTablePrinter(printers.PrintOptions{}) return printer.PrintObj(&table, outWriter) }