-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command "doc flags" which generate a mardown array with all avail…
…able flags.
- Loading branch information
Showing
2 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters