Skip to content

Commit

Permalink
Added avail for listing available commands
Browse files Browse the repository at this point in the history
  • Loading branch information
hurngchunlee committed Nov 4, 2019
1 parent 4edc889 commit 64e6fd7
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"

"github.com/olekukonko/tablewriter"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -61,7 +62,7 @@ __custom_func() {
func init() {
rootCmd.PersistentFlags().BoolVarP(&Verbose, "verbose", "v", false, "verbose output")
rootCmd.PersistentFlags().StringVarP(&NetDomain, "domain", "d", "dccn.nl", "default network domain")
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(versionCmd, availCmd)
}

var rootCmd = &cobra.Command{
Expand All @@ -86,6 +87,36 @@ var versionCmd = &cobra.Command{
},
}

var availCmd = &cobra.Command{
Use: "avail",
Short: "Print availab sub-commands.",
Long: ``,
Args: cobra.NoArgs,
Run: func(cmd *cobra.Command, args []string) {
table := tablewriter.NewWriter(os.Stdout)
table.SetRowLine(true)
// TODO: SetColMinWidth doesn't automatically wrap the long string accordingly.
//table.SetColMinWidth(1,40)
table.SetHeader([]string{"Command", "Description"})
table.SetRowSeparator("-")
addCommandUseToTable(rootCmd, "", table)
table.SetColWidth(40)
table.Render()
},
}

// addCommandUseToTable addes recursively command's and sub-commands' `Use` and `Short` to a
// `tablewritter.Table`.
func addCommandUseToTable(cmd *cobra.Command, parentUse string, table *tablewriter.Table) {
for _, c := range cmd.Commands() {
table.Append([]string{
fmt.Sprintf("%s %s %s", parentUse, cmd.Use, c.Use),
c.Short,
})
addCommandUseToTable(c, fmt.Sprintf("%s %s", parentUse, cmd.Use), table)
}
}

// Execute is the main entry point of the cluster command.
func Execute() {
if err := rootCmd.Execute(); err != nil {
Expand Down

0 comments on commit 64e6fd7

Please sign in to comment.