Skip to content

Commit

Permalink
Fixes the watch flag output (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdewinne authored Sep 8, 2023
1 parent e208c25 commit 850ba71
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
17 changes: 9 additions & 8 deletions cli/cmd/cluster_ls.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"fmt"
"reflect"
"time"

Expand All @@ -15,10 +14,10 @@ import (

func (r *runners) InitClusterList(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "ls",
Short: "list test clusters",
Long: `list test clusters`,
RunE: r.listClusters,
Use: "ls",
Short: "list test clusters",
Long: `list test clusters`,
RunE: r.listClusters,
}
parent.AddCommand(cmd)

Expand Down Expand Up @@ -56,6 +55,7 @@ func (r *runners) listClusters(_ *cobra.Command, args []string) error {
return errors.Wrap(err, "list clusters")
}

header := true
if r.args.lsClusterWatch {

// Checks to see if the outputFormat is table
Expand Down Expand Up @@ -118,17 +118,18 @@ func (r *runners) listClusters(_ *cobra.Command, args []string) error {

// Prints the clusters
if len(clustersToPrint) > 0 {
fmt.Print("\033[H\033[2J") // Clears the console
print.Clusters(r.outputFormat, r.w, clustersToPrint)
print.Clusters(r.outputFormat, r.w, clustersToPrint, header)
header = false // only print the header once
}

clusters = newClusters
clustersToPrint = make([]*types.Cluster, 0)
}
}

if len(clusters) == 0 {
return print.NoClusters(r.outputFormat, r.w)
}

return print.Clusters(r.outputFormat, r.w, clusters)
return print.Clusters(r.outputFormat, r.w, clusters, true)
}
8 changes: 4 additions & 4 deletions cli/cmd/release_compatibility.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ import (
func (r *runners) InitReleaseCompatibility(parent *cobra.Command) *cobra.Command {
cmd := &cobra.Command{
Use: "compatibility SEQUENCE",
Short: "report release compatibility",
Long: "report release compatibility for a kubernetes distribution and version",
Short: "Report release compatibility",
Long: "Report release compatibility for a kubernetes distribution and version",
RunE: r.reportReleaseCompatibility,
}
parent.AddCommand(cmd)

cmd.Flags().StringVar(&r.args.compatibilityKubernetesDistribution, "distribution", "kind", "Kubernetes distribution of the cluster to report on.")
cmd.Flags().StringVar(&r.args.compatibilityKubernetesVersion, "version", "1.25.3", "Kubernetes version of the cluster to report on (format is distribution dependent)")
cmd.Flags().StringVar(&r.args.compatibilityKubernetesDistribution, "distribution", "", "Kubernetes distribution of the cluster to report on.")
cmd.Flags().StringVar(&r.args.compatibilityKubernetesVersion, "version", "", "Kubernetes version of the cluster to report on (format is distribution dependent)")
cmd.Flags().BoolVar(&r.args.compatibilitySuccess, "success", false, "If set, the compatibility will be reported as a success.")
cmd.Flags().BoolVar(&r.args.compatibilityFailure, "failure", false, "If set, the compatibility will be reported as a failure.")
cmd.Flags().StringVar(&r.args.compatibilityNotes, "notes", "", "Additional notes to report.")
Expand Down
20 changes: 14 additions & 6 deletions cli/print/clusters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,31 @@ import (
)

// TODO: implement a -o wide, and expose nodecount also?
var clustersTmplSrc = `ID NAME DISTRIBUTION VERSION STATUS CREATED EXPIRES
{{ range . -}}
{{ .ID }} {{ .Name }} {{ .KubernetesDistribution}} {{ .KubernetesVersion }} {{ .Status }} {{ .CreatedAt}} {{if .ExpiresAt.IsZero}}-{{else}}{{ .ExpiresAt }}{{end}}
var clustersTmplHeaderSrc = `ID NAME DISTRIBUTION VERSION STATUS CREATED EXPIRES`
var clustersTmplRowSrc = `{{ range . -}}
{{ .ID }} {{ padding .Name 27 }} {{ padding .KubernetesDistribution 12 }} {{ padding .KubernetesVersion 10 }} {{ padding (printf "%s" .Status) 12 }} {{ .CreatedAt}} {{if .ExpiresAt.IsZero}}-{{else}}{{ .ExpiresAt }}{{end}}
{{ end }}`
var clustersTmplSrc = fmt.Sprintln(clustersTmplHeaderSrc) + clustersTmplRowSrc

var clusterVersionsTmplSrc = `Supported Kubernetes distributions and versions are:
DISTRIBUTION VERSION INSTANCE_TYPES
{{ range $d := . -}}{{ $d.Name }} {{ range $i, $v := $d.Versions -}}{{if $i}}, {{end}}{{ $v }}{{ end }} {{ range $i, $it := $d.InstanceTypes -}}{{if $i}}, {{end}}{{ $it }}{{ end }}
{{ end }}`

var clustersTmpl = template.Must(template.New("clusters").Funcs(funcs).Parse(clustersTmplSrc))
var clustersTmplNoHeader = template.Must(template.New("clusters").Funcs(funcs).Parse(clustersTmplRowSrc))
var clusterVersionsTmpl = template.Must(template.New("clusterVersions").Funcs(funcs).Parse(clusterVersionsTmplSrc))

func Clusters(outputFormat string, w *tabwriter.Writer, clusters []*types.Cluster) error {
func Clusters(outputFormat string, w *tabwriter.Writer, clusters []*types.Cluster, header bool) error {
if outputFormat == "table" {
if err := clustersTmpl.Execute(w, clusters); err != nil {
return err
if header {
if err := clustersTmpl.Execute(w, clusters); err != nil {
return err
}
} else {
if err := clustersTmplNoHeader.Execute(w, clusters); err != nil {
return err
}
}
} else if outputFormat == "json" {
cAsByte, _ := json.MarshalIndent(clusters, "", " ")
Expand Down
6 changes: 6 additions & 0 deletions cli/print/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@ var funcs = template.FuncMap{
"time": func(t time.Time) string {
return t.Format(time.RFC3339)
},
"padding": func(s string, width int) string {
for len(s) < width {
s += " "
}
return s
},
}

0 comments on commit 850ba71

Please sign in to comment.