Skip to content

Commit

Permalink
fix: swapped the name and namespace columns and also kept a distance …
Browse files Browse the repository at this point in the history
…of 2 extra spaces after the end of the length for better visibility

Signed-off-by: Abinand P <[email protected]>
  • Loading branch information
Abiji-2020 committed Oct 6, 2024
1 parent db662a5 commit 98c389a
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions cyctl/internal/get/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -39,14 +38,37 @@ func listResources(clientset *k8sclient.KubernetesClient, moduleNames []string)
return
}

headerSpacing := 20
output := "KIND" + strings.Repeat(" ", 16) + " NAME" + strings.Repeat(" ", 16) + " NAMESPACE\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()))
namespaceSpacing := max(0, headerSpacing-len(resource.GetName()))
fmt.Printf("%s"+strings.Repeat(" ", nameSpacing)+" %s"+strings.Repeat(" ", namespaceSpacing)+"%s\n",
resource.GetKind(), resource.GetName(), resource.GetNamespace())
fmt.Printf("%-*s %-*s %-*s\n",
maxKindLen, resource.GetKind(),
maxNamespaceLen, resource.GetNamespace(), maxNameLen, resource.GetName(),
)
}

}
Expand Down

0 comments on commit 98c389a

Please sign in to comment.