Skip to content

Commit

Permalink
add node ls
Browse files Browse the repository at this point in the history
  • Loading branch information
birdayz committed Oct 2, 2018
1 parent 1295526 commit a4d445b
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions cmd/kaf/node.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"fmt"
"os"
"text/tabwriter"

"sort"

"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(nodeCommand)
nodeCommand.AddCommand(nodeLsCommand)
nodeLsCommand.Flags().BoolVar(&noHeaderFlag, "no-headers", false, "Hide table headers")
}

var nodeCommand = &cobra.Command{
Use: "node",
Short: "Describe and List nodes",
}

var nodeLsCommand = &cobra.Command{
Use: "ls",
Short: "List nodes in a cluster",
Run: func(cmd *cobra.Command, args []string) {
admin, err := getClusterAdmin()
if err != nil {
panic(err)
}

brokers, _, err := admin.DescribeCluster()
if err != nil {
panic(err)
}

sort.Slice(brokers, func(i, j int) bool {
return brokers[i].ID() < brokers[j].ID()
})

w := tabwriter.NewWriter(os.Stdout, tabwriterMinWidth, tabwriterWidth, tabwriterPadding, tabwriterPadChar, tabwriterFlags)
if !noHeaderFlag {
fmt.Fprintf(w, "ID\tADDRESS\t\n")
}

for _, broker := range brokers {
fmt.Fprintf(w, "%v\t%v\t\n", broker.ID(), broker.Addr())
}

w.Flush()
},
}

0 comments on commit a4d445b

Please sign in to comment.