From 3a2c119455e742d715a63fefb8a6bbaa3bf8333c Mon Sep 17 00:00:00 2001 From: cmoulliard Date: Fri, 20 Dec 2024 14:22:02 +0100 Subject: [PATCH] Move the code to their corresponding go file Signed-off-by: cmoulliard --- pkg/printer/cluster.go | 64 +++++++++++++++++++++ pkg/printer/printer.go | 128 ----------------------------------------- pkg/printer/secret.go | 61 ++++++++++++++++++++ 3 files changed, 125 insertions(+), 128 deletions(-) create mode 100644 pkg/printer/cluster.go create mode 100644 pkg/printer/secret.go diff --git a/pkg/printer/cluster.go b/pkg/printer/cluster.go new file mode 100644 index 00000000..8cdb5a8d --- /dev/null +++ b/pkg/printer/cluster.go @@ -0,0 +1,64 @@ +package printer + +import ( + "fmt" + "github.com/cnoe-io/idpbuilder/pkg/entity" + "io" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +type ClusterPrinter struct { + Clusters []entity.Cluster + OutWriter io.Writer +} + +func (cp ClusterPrinter) PrintOutput(format string) error { + switch format { + case "json": + return PrintDataAsJson(cp.Clusters, cp.OutWriter) + case "yaml": + return PrintDataAsYaml(cp.Clusters, cp.OutWriter) + case "table": + return PrintDataAsTable(cp.generateClusterTable(cp.Clusters), cp.OutWriter) + default: + return fmt.Errorf("output format %s is not supported", format) + } +} + +func (cp ClusterPrinter) generateClusterTable(input []entity.Cluster) metav1.Table { + table := &metav1.Table{} + table.ColumnDefinitions = []metav1.TableColumnDefinition{ + {Name: "Name", Type: "string"}, + {Name: "External-Port", Type: "string"}, + {Name: "Kube-Api", Type: "string"}, + {Name: "TLS", Type: "string"}, + {Name: "Kube-Port", Type: "string"}, + {Name: "Nodes", Type: "string"}, + } + + for _, cluster := range input { + row := metav1.TableRow{ + Cells: []interface{}{ + cluster.Name, + cluster.ExternalPort, + cluster.URLKubeApi, + cluster.TlsCheck, + cluster.KubePort, + generateNodeData(cluster.Nodes), + }, + } + table.Rows = append(table.Rows, row) + } + return *table +} + +func generateNodeData(nodes []entity.Node) string { + var result string + for i, aNode := range nodes { + result += aNode.Name + if i < len(nodes)-1 { + result += "," + } + } + return result +} diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index 265e0fbf..634c314e 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -2,140 +2,12 @@ package printer import ( "encoding/json" - "fmt" - "github.com/cnoe-io/idpbuilder/pkg/entity" "io" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/cli-runtime/pkg/printers" "sigs.k8s.io/yaml" - "strings" ) -type Printer interface { - PrintOutput() -} - -type ClusterPrinter struct { - Clusters []entity.Cluster - OutWriter io.Writer -} - -type SecretPrinter struct { - Secrets []entity.Secret - OutWriter io.Writer -} - -func (sp SecretPrinter) PrintOutput(format string) error { - switch format { - case "json": - return PrintDataAsJson(sp.Secrets, sp.OutWriter) - case "yaml": - return PrintDataAsYaml(sp.Secrets, sp.OutWriter) - case "table": - return PrintDataAsTable(sp.generateSecretTable(sp.Secrets), sp.OutWriter) - default: - return fmt.Errorf("output format %s is not supported", format) - } -} - -func (sp SecretPrinter) generateSecretTable(secretTable []entity.Secret) metav1.Table { - table := &metav1.Table{} - table.ColumnDefinitions = []metav1.TableColumnDefinition{ - {Name: "Name", Type: "string"}, - {Name: "Namespace", Type: "string"}, - {Name: "Username", Type: "string"}, - {Name: "Password", Type: "string"}, - {Name: "Token", Type: "string"}, - {Name: "Data", Type: "string"}, - } - for _, secret := range secretTable { - var dataEntries []string - - if !secret.IsCore { - for key, value := range secret.Data { - dataEntries = append(dataEntries, fmt.Sprintf("%s=%s", key, value)) - } - } - dataString := strings.Join(dataEntries, ", ") - row := metav1.TableRow{ - Cells: []interface{}{ - secret.Name, - secret.Namespace, - secret.Username, - secret.Password, - secret.Token, - dataString, - }, - } - table.Rows = append(table.Rows, row) - } - return *table -} - -func (cp ClusterPrinter) PrintOutput(format string) error { - switch format { - case "json": - return PrintDataAsJson(cp.Clusters, cp.OutWriter) - case "yaml": - return PrintDataAsYaml(cp.Clusters, cp.OutWriter) - case "table": - return PrintDataAsTable(cp.generateClusterTable(cp.Clusters), cp.OutWriter) - default: - return fmt.Errorf("output format %s is not supported", format) - } -} - -func (cp ClusterPrinter) generateClusterTable(input []entity.Cluster) metav1.Table { - table := &metav1.Table{} - table.ColumnDefinitions = []metav1.TableColumnDefinition{ - {Name: "Name", Type: "string"}, - {Name: "External-Port", Type: "string"}, - {Name: "Kube-Api", Type: "string"}, - {Name: "TLS", Type: "string"}, - {Name: "Kube-Port", Type: "string"}, - {Name: "Nodes", Type: "string"}, - } - - for _, cluster := range input { - row := metav1.TableRow{ - Cells: []interface{}{ - cluster.Name, - cluster.ExternalPort, - cluster.URLKubeApi, - cluster.TlsCheck, - cluster.KubePort, - generateNodeData(cluster.Nodes), - }, - } - table.Rows = append(table.Rows, row) - } - return *table -} - -func generateNodeData(nodes []entity.Node) string { - var result string - for i, aNode := range nodes { - result += aNode.Name - if i < len(nodes)-1 { - result += "," - } - } - return result -} - -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) diff --git a/pkg/printer/secret.go b/pkg/printer/secret.go new file mode 100644 index 00000000..206aa7cb --- /dev/null +++ b/pkg/printer/secret.go @@ -0,0 +1,61 @@ +package printer + +import ( + "fmt" + "github.com/cnoe-io/idpbuilder/pkg/entity" + "io" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "strings" +) + +type SecretPrinter struct { + Secrets []entity.Secret + OutWriter io.Writer +} + +func (sp SecretPrinter) PrintOutput(format string) error { + switch format { + case "json": + return PrintDataAsJson(sp.Secrets, sp.OutWriter) + case "yaml": + return PrintDataAsYaml(sp.Secrets, sp.OutWriter) + case "table": + return PrintDataAsTable(sp.generateSecretTable(sp.Secrets), sp.OutWriter) + default: + return fmt.Errorf("output format %s is not supported", format) + } +} + +func (sp SecretPrinter) generateSecretTable(secretTable []entity.Secret) metav1.Table { + table := &metav1.Table{} + table.ColumnDefinitions = []metav1.TableColumnDefinition{ + {Name: "Name", Type: "string"}, + {Name: "Namespace", Type: "string"}, + {Name: "Username", Type: "string"}, + {Name: "Password", Type: "string"}, + {Name: "Token", Type: "string"}, + {Name: "Data", Type: "string"}, + } + for _, secret := range secretTable { + var dataEntries []string + + if !secret.IsCore { + for key, value := range secret.Data { + dataEntries = append(dataEntries, fmt.Sprintf("%s=%s", key, value)) + } + } + dataString := strings.Join(dataEntries, ", ") + row := metav1.TableRow{ + Cells: []interface{}{ + secret.Name, + secret.Namespace, + secret.Username, + secret.Password, + secret.Token, + dataString, + }, + } + table.Rows = append(table.Rows, row) + } + return *table +}