Skip to content

Commit

Permalink
Moving the function PrintOutput to the printer package. WIP
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 813c0cd commit 580042f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 31 deletions.
46 changes: 31 additions & 15 deletions pkg/cmd/get/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ 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"
"k8s.io/apimachinery/pkg/types"
"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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down Expand Up @@ -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"},
Expand All @@ -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,
Expand All @@ -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 {
Expand Down
17 changes: 2 additions & 15 deletions pkg/cmd/get/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down
16 changes: 15 additions & 1 deletion pkg/printer/printer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 580042f

Please sign in to comment.