diff --git a/cmd/get/getCluster.go b/cmd/get/getCluster.go index 5a975eda..1bba03e2 100644 --- a/cmd/get/getCluster.go +++ b/cmd/get/getCluster.go @@ -23,6 +23,9 @@ package get import ( "fmt" + "os" + "sort" + "strings" "github.com/rancher/k3d/pkg/cluster" "github.com/rancher/k3d/pkg/runtimes" @@ -30,6 +33,8 @@ import ( "github.com/spf13/cobra" log "github.com/sirupsen/logrus" + + "github.com/liggitt/tabwriter" ) // NewCmdGetCluster returns a new cobra command @@ -92,11 +97,30 @@ func parseGetClusterCmd(cmd *cobra.Command, args []string) (*k3d.Cluster, runtim // TODO: improve (tabular output or output similar to kubectl) func printClusters(clusters []*k3d.Cluster) { + + tabwriter := tabwriter.NewWriter(os.Stdout, 6, 4, 3, ' ', tabwriter.RememberWidths) + defer tabwriter.Flush() + + headers := []string{"NAME", "MASTERS", "WORKERS"} // TODO: add status + _, err := fmt.Fprintf(tabwriter, "%s\n", strings.Join(headers, "\t")) + if err != nil { + log.Fatalln("Failed to print headers") + } + + sort.Slice(clusters, func(i, j int) bool { + return clusters[i].Name < clusters[j].Name + }) + for _, cluster := range clusters { - fmt.Printf("@@@ Cluster '%s' @@@\n", cluster.Name) + masterCount := 0 + workerCount := 0 for _, node := range cluster.Nodes { - fmt.Printf("Node '%s' - Role '%s' - Labels '%v'\n", node.Name, node.Role, node.Labels) + if node.Role == k3d.MasterRole { + masterCount++ + } else if node.Role == k3d.WorkerRole { + workerCount++ + } } - fmt.Println("====================") + fmt.Fprintf(tabwriter, "%s\t%d\t%d\n", cluster.Name, masterCount, workerCount) } } diff --git a/go.mod b/go.mod index 72beae16..18e0005b 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/google/go-cmp v0.3.0 // indirect github.com/gorilla/mux v1.7.3 // indirect github.com/imdario/mergo v0.3.7 // indirect + github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de github.com/morikuni/aec v0.0.0-20170113033406-39771216ff4c // indirect github.com/opencontainers/go-digest v1.0.0-rc1 // indirect github.com/opencontainers/image-spec v1.0.1 // indirect diff --git a/go.sum b/go.sum index 17c54cbb..e336b364 100644 --- a/go.sum +++ b/go.sum @@ -106,6 +106,8 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de h1:9TO3cAIGXtEhnIaL+V+BEER86oLrvS+kWobKpbJuye0= +github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de/go.mod h1:zAbeS9B/r2mtpb6U+EI2rYA5OAXxsYw6wTamcNW+zcE= github.com/magiconair/properties v1.8.0 h1:LLgXmsheXeRoUOBOjtwPQCWIYqM/LU1ayDtDePerRcY= github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ= github.com/marstr/guid v1.1.0/go.mod h1:74gB1z2wpxxInTG6yaqA7KrtM0NZ+RbrcqDvYHefzho= diff --git a/vendor/modules.txt b/vendor/modules.txt index 399116c1..5e4ee9da 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -127,6 +127,8 @@ github.com/golang/protobuf/ptypes/timestamp github.com/inconshreveable/mousetrap # github.com/konsorten/go-windows-terminal-sequences v1.0.1 github.com/konsorten/go-windows-terminal-sequences +# github.com/liggitt/tabwriter v0.0.0-20181228230101-89fcab3d43de +github.com/liggitt/tabwriter # github.com/opencontainers/go-digest v1.0.0-rc1 github.com/opencontainers/go-digest # github.com/opencontainers/image-spec v1.0.1