From a56454de7d966bb4d5a5c7d8403ff0cb2048e849 Mon Sep 17 00:00:00 2001 From: Abinand P Date: Tue, 8 Oct 2024 21:30:54 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=8F=98=EF=B8=8F=20updated=20the=20display?= =?UTF-8?q?ing=20of=20the=20namespace=20=20(#606)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix: updates -the command will now print the NAMESPACE of the deployed resources Signed-off-by: Abinand P * fix: swapped the name and namespace columns and also kept a distance of 2 extra spaces after the end of the length for better visibility Signed-off-by: Abinand P --------- Signed-off-by: Abinand P --- cyctl/internal/get/resources.go | 36 +++++++++++++++++++++++++++------ 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/cyctl/internal/get/resources.go b/cyctl/internal/get/resources.go index 49761d1d..7eed8d53 100644 --- a/cyctl/internal/get/resources.go +++ b/cyctl/internal/get/resources.go @@ -2,7 +2,6 @@ package get import ( "fmt" - "strings" "github.com/cyclops-ui/cyclops/cyclops-ctrl/pkg/cluster/k8sclient" "github.com/cyclops-ui/cycops-cyctl/internal/kubeconfig" @@ -39,12 +38,37 @@ func listResources(clientset *k8sclient.KubernetesClient, moduleNames []string) return } - headerSpacing := 20 - output := "KIND" + strings.Repeat(" ", 16) + " NAME\n" - fmt.Print(output) + maxKindLen := len("KIND") + maxNameLen := len("NAME") + maxNamespaceLen := len("NAMESPACE") + + for _, resource := range resources { + if len(resource.GetKind()) > maxKindLen { + maxKindLen = len(resource.GetKind()) + } + if len(resource.GetName()) > maxNameLen { + maxNameLen = len(resource.GetName()) + } + if len(resource.GetNamespace()) > maxNamespaceLen { + maxNamespaceLen = len(resource.GetNamespace()) + } + } + maxKindLen = maxKindLen + 2 + maxNameLen = maxNameLen + 2 + maxNamespaceLen = maxNamespaceLen + 2 + + // Step 2: Print the header with proper spacing + header := fmt.Sprintf("%-*s %-*s %-*s\n", + maxKindLen, "KIND", + maxNamespaceLen, "NAMESPACE", maxNameLen, "NAME") + fmt.Print(header) + + // Step 3: Print each resource with calculated spacing for _, resource := range resources { - nameSpacing := max(0, headerSpacing-len(resource.GetKind())) - fmt.Printf("%s"+strings.Repeat(" ", nameSpacing)+" %s\n", resource.GetKind(), resource.GetName()) + fmt.Printf("%-*s %-*s %-*s\n", + maxKindLen, resource.GetKind(), + maxNamespaceLen, resource.GetNamespace(), maxNameLen, resource.GetName(), + ) } }