| 1 |
package cobracompletefig |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strings" |
| 6 |
|
| 7 |
"github.com/spf13/cobra" |
| 8 |
"github.com/spf13/pflag" |
| 9 |
) |
| 10 |
|
| 11 |
var includeHidden bool |
| 12 |
var figGenCmdUse string = "" |
| 13 |
|
| 14 |
var generateCommandArgs func(*cobra.Command) Args |
| 15 |
|
| 16 |
type Opts struct { |
| 17 |
Use string |
| 18 |
Short string |
| 19 |
Visible bool |
| 20 |
Long string |
| 21 |
commandArgGenerator func(*cobra.Command) Args |
| 22 |
} |
| 23 |
|
| 24 |
func CreateCompletionSpecCommand(options ...Opts) * cobra.Command { |
| 25 |
Use := "generate-fig-spec" |
| 26 |
Aliases := []string{"generateFigSpec", "genFigSpec"} |
| 27 |
Short := "Generate a fig spec" |
| 28 |
Hidden := true |
| 29 |
Long := ` |
| 30 |
Fig is a tool for your command line that adds autocomplete. |
| 31 |
This command generates a TypeScript file with the skeleton |
| 32 |
Fig autocomplete spec for your Cobra CLI. |
| 33 |
` |
| 34 |
if len(options) > 0 { |
| 35 |
if options[0].Use != "" { |
| 36 |
Use = options[0].Use |
| 37 |
} |
| 38 |
if options[0].Short != "" { |
| 39 |
Short = options[0].Short |
| 40 |
} |
| 41 |
if options[0].Long != "" { |
| 42 |
Long = options[0].Long |
| 43 |
} |
| 44 |
if options[0].Visible { |
| 45 |
Hidden = false |
| 46 |
} |
| 47 |
if options[0].commandArgGenerator != nil { |
| 48 |
generateCommandArgs = options[0].commandArgGenerator |
| 49 |
} |
| 50 |
} |
| 51 |
figGenCmdUse = Use |
| 52 |
var cmd = &cobra.Command{ |
| 53 |
Use: Use, |
| 54 |
Aliases: Aliases, |
| 55 |
Short: Short, |
| 56 |
Hidden: Hidden, |
| 57 |
Long: Long, |
| 58 |
Run: func(cmd *cobra.Command, args []string) { |
| 59 |
root := cmd.Root() |
| 60 |
spec := GenerateCompletionSpec(root) |
| 61 |
fmt.Println(spec.ToTypescript()) |
| 62 |
}, |
| 63 |
} |
| 64 |
cmd.Flags().BoolVar( |
| 65 |
&includeHidden, "include-hidden", false, |
| 66 |
"Include hidden commands in generated Fig autocomplete spec") |
| 67 |
return cmd |
| 68 |
} |
| 69 |
|
| 70 |
func GenerateCompletionSpec(root *cobra.Command) Spec { |
| 71 |
opts := append(options(root.LocalNonPersistentFlags(), false), options(root.PersistentFlags(), true)...) |
| 72 |
opts = append(opts, makeHelpOption()) |
| 73 |
spec := Spec{ |
| 74 |
Subcommand: &Subcommand{ |
| 75 |
BaseSuggestion: &BaseSuggestion{ |
| 76 |
description: root.Short, |
| 77 |
}, |
| 78 |
options: opts, |
| 79 |
subcommands: append(subcommands(root, false, Options{}), makeHelpCommand(root)), |
| 80 |
args: commandArguments(root), |
| 81 |
}, |
| 82 |
name: root.Name(), |
| 83 |
} |
| 84 |
return spec |
| 85 |
} |
| 86 |
|
| 87 |
func subcommands(cmd *cobra.Command, overrideOptions bool, overrides Options) Subcommands { |
| 88 |
var subs []Subcommand |
| 89 |
for _, sub := range cmd.Commands() { |
| 90 |
if sub.Name() == "help" || (!includeHidden && sub.Hidden) || (sub.Use == figGenCmdUse) { |
| 91 |
continue |
| 92 |
} |
| 93 |
var opts Options |
| 94 |
if overrideOptions { |
| 95 |
opts = overrides |
| 96 |
} else { |
| 97 |
opts = append(options(sub.LocalNonPersistentFlags(), false), options(sub.PersistentFlags(), true)...) |
| 98 |
} |
| 99 |
subs = append(subs, Subcommand{ |
| 100 |
BaseSuggestion: &BaseSuggestion{ |
| 101 |
description: sub.Short, |
| 102 |
hidden: sub.Hidden, |
| 103 |
}, |
| 104 |
name: append(sub.Aliases, sub.Name()), |
| 105 |
options: opts, |
| 106 |
subcommands: subcommands(sub, overrideOptions, overrides), |
| 107 |
args: commandArguments(sub), |
| 108 |
}) |
| 109 |
} |
| 110 |
return subs |
| 111 |
} |
| 112 |
|
| 113 |
func isFlagRepeatable(flag *pflag.Flag) bool { |
| 114 |
return strings.Contains(flag.Value.Type(), "Slice") || strings.Contains(flag.Value.Type(), "Array") |
| 115 |
} |
| 116 |
|
| 117 |
func options(flagSet *pflag.FlagSet, persistent bool) []Option { |
| 118 |
var opts []Option |
| 119 |
attachFlags := func(flag *pflag.Flag) { |
| 120 |
|
| 121 |
option := Option{ |
| 122 |
BaseSuggestion: &BaseSuggestion{ |
| 123 |
description: flag.Usage, |
| 124 |
hidden: flag.Hidden, |
| 125 |
}, |
| 126 |
name: []string{fmt.Sprintf("--%v", flag.Name)}, |
| 127 |
isRepeatable: isFlagRepeatable(flag), |
| 128 |
} |
| 129 |
if flag.Shorthand != "" { |
| 130 |
option.name = append(option.name, fmt.Sprintf("-%v", flag.Shorthand)) |
| 131 |
} |
| 132 |
if persistent != false { |
| 133 |
option.isPersistent = true |
| 134 |
} |
| 135 |
requiredAnnotation, found := flag.Annotations[cobra.BashCompOneRequiredFlag] |
| 136 |
if found && requiredAnnotation[0] == "true" { |
| 137 |
option.isRequired = true |
| 138 |
} |
| 139 |
option.args = flagArguments(flag) |
| 140 |
opts = append(opts, option) |
| 141 |
} |
| 142 |
|
| 143 |
flagSet.VisitAll(attachFlags) |
| 144 |
return opts |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
func commandArguments(cmd *cobra.Command) []Arg { |
| 153 |
if generateCommandArgs != nil { |
| 154 |
return generateCommandArgs(cmd) |
| 155 |
} |
| 156 |
return []Arg{} |
| 157 |
} |
| 158 |
|
| 159 |
func flagArguments(flag *pflag.Flag) []Arg { |
| 160 |
var args []Arg |
| 161 |
defaultVal := flag.DefValue |
| 162 |
if defaultVal == "[]" { |
| 163 |
defaultVal = "" |
| 164 |
} |
| 165 |
if flag.Value.Type() != "bool" { |
| 166 |
arg := Arg{ |
| 167 |
name: flag.Name, |
| 168 |
defaultVal: defaultVal, |
| 169 |
} |
| 170 |
_, foundFilenameAnnotation := flag.Annotations[cobra.BashCompFilenameExt] |
| 171 |
if foundFilenameAnnotation { |
| 172 |
arg.template = append(arg.template, FILEPATHS) |
| 173 |
} |
| 174 |
_, foundDirectoryAnnotation := flag.Annotations[cobra.BashCompSubdirsInDir] |
| 175 |
if foundDirectoryAnnotation { |
| 176 |
arg.template = append(arg.template, FOLDERS) |
| 177 |
} |
| 178 |
args = append(args, arg) |
| 179 |
} |
| 180 |
return args |
| 181 |
} |
| 182 |
|
| 183 |
func makeHelpCommand(root *cobra.Command) Subcommand { |
| 184 |
return Subcommand{ |
| 185 |
BaseSuggestion: &BaseSuggestion{ |
| 186 |
description: "Help about any command", |
| 187 |
}, |
| 188 |
name: []string{"help"}, |
| 189 |
subcommands: subcommands(root, true, []Option{}), |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
func makeHelpOption() Option { |
| 194 |
return Option{ |
| 195 |
BaseSuggestion: &BaseSuggestion{ |
| 196 |
description: fmt.Sprintf("Display help"), |
| 197 |
}, |
| 198 |
isPersistent: true, |
| 199 |
name: []string{"--help", "-h"}, |
| 200 |
} |
| 201 |
} |