Skip to content

Commit

Permalink
Add command "doc flags" which generate a mardown array with all avail…
Browse files Browse the repository at this point in the history
…able flags.
  • Loading branch information
gfyrag committed Mar 22, 2022
1 parent 90fd81c commit 0f3d32d
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
45 changes: 45 additions & 0 deletions cmd/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package cmd

import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"sort"
"strings"
"text/tabwriter"
)

func NewDocCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "doc",
}
cmd.AddCommand(NewDocFlagCommand())
return cmd
}

func NewDocFlagCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "flags",
Run: func(cmd *cobra.Command, args []string) {

w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.Debug)
defer w.Flush()

allKeys := viper.GetViper().AllKeys()
sort.Strings(allKeys)

fmt.Fprintf(w, "\tFlag\tEnv var\tDefault value\tDescription\t\r\n")
fmt.Fprintf(w, "\t-\t-\t-\t-\t\r\n")
for _, key := range allKeys {
asEnvVar := strings.ToUpper(replacer.Replace(key))
flag := cmd.Parent().Parent().PersistentFlags().Lookup(key)
if flag == nil {
continue
}
fmt.Fprintf(w, "\t--%s\t%s\t%s\t%s\t\r\n", key, asEnvVar, flag.DefValue, flag.Usage)
}
},
}
return cmd
}
6 changes: 5 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ var (
Version = "develop"
BuildDate = "-"
Commit = "-"

replacer = strings.NewReplacer(".", "_", "-", "_")
)

func NewRootCommand() *cobra.Command {
Expand Down Expand Up @@ -98,6 +100,8 @@ func NewRootCommand() *cobra.Command {
root.AddCommand(version)
root.AddCommand(stickersCmd)

root.AddCommand(NewDocCommand())

home, err := os.UserHomeDir()
if err != nil {
home = "/root"
Expand Down Expand Up @@ -147,7 +151,7 @@ func NewRootCommand() *cobra.Command {
viper.ReadInConfig()

viper.SetEnvPrefix("numary")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_", "-", "_"))
viper.SetEnvKeyReplacer(replacer)
viper.AutomaticEnv()

return root
Expand Down

0 comments on commit 0f3d32d

Please sign in to comment.