From ecd786d163213487943fdac7ca5c781455e7ec30 Mon Sep 17 00:00:00 2001 From: Gabe Cook Date: Mon, 25 Mar 2024 01:01:22 -0500 Subject: [PATCH] chore(cmd): Add missing flag completions --- cmd/cmd.go | 33 ++++++++++++++++++++++++++++++++- cmd/flag_values.go | 8 ++------ internal/config/flags/flags.go | 6 ++++++ internal/util/completion.go | 7 +++++++ internal/util/flags.go | 10 +++++----- 5 files changed, 52 insertions(+), 12 deletions(-) create mode 100644 internal/config/flags/flags.go create mode 100644 internal/util/completion.go diff --git a/cmd/cmd.go b/cmd/cmd.go index 0e4baf2..1c7c899 100644 --- a/cmd/cmd.go +++ b/cmd/cmd.go @@ -10,6 +10,8 @@ import ( "strings" "github.com/clevyr/yampl/internal/config" + "github.com/clevyr/yampl/internal/config/flags" + "github.com/clevyr/yampl/internal/util" "github.com/clevyr/yampl/internal/visitor" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -35,13 +37,42 @@ func NewCommand(version, commit string) *cobra.Command { registerCompletionFlag(cmd) registerLogFlags(cmd) registerValuesFlag(cmd) + cmd.Flags().BoolVarP(&conf.Inplace, "inplace", "i", conf.Inplace, "Edit files in place") + if err := cmd.RegisterFlagCompletionFunc("inplace", util.BoolCompletion); err != nil { + panic(err) + } + cmd.Flags().StringVarP(&conf.Prefix, "prefix", "p", conf.Prefix, "Template comments must begin with this prefix. The beginning '#' is implied.") + if err := cmd.RegisterFlagCompletionFunc("prefix", cobra.NoFileCompletions); err != nil { + panic(err) + } + cmd.Flags().StringVar(&conf.LeftDelim, "left-delim", conf.LeftDelim, "Override template left delimiter") + if err := cmd.RegisterFlagCompletionFunc("left-delim", cobra.NoFileCompletions); err != nil { + panic(err) + } + cmd.Flags().StringVar(&conf.RightDelim, "right-delim", conf.RightDelim, "Override template right delimiter") + if err := cmd.RegisterFlagCompletionFunc("right-delim", cobra.NoFileCompletions); err != nil { + panic(err) + } + cmd.Flags().IntVarP(&conf.Indent, "indent", "I", conf.Indent, "Override output indentation") + if err := cmd.RegisterFlagCompletionFunc("indent", cobra.NoFileCompletions); err != nil { + panic(err) + } + cmd.Flags().BoolVarP(&conf.Fail, "fail", "f", conf.Fail, `Exit with an error if a template variable is not set`) + if err := cmd.RegisterFlagCompletionFunc("fail", util.BoolCompletion); err != nil { + panic(err) + } + cmd.Flags().BoolVarP(&conf.Strip, "strip", "s", conf.Strip, "Strip template comments from output") + if err := cmd.RegisterFlagCompletionFunc("strip", util.BoolCompletion); err != nil { + panic(err) + } + cmd.InitDefaultVersionFlag() return cmd @@ -77,7 +108,7 @@ func preRun(cmd *cobra.Command, args []string) error { return ErrNoFiles } - rawValues, err := cmd.Flags().GetStringToString(ValueFlag) + rawValues, err := cmd.Flags().GetStringToString(flags.ValueFlag) if err != nil { panic(err) } diff --git a/cmd/flag_values.go b/cmd/flag_values.go index 8f3306d..bc17ddf 100644 --- a/cmd/flag_values.go +++ b/cmd/flag_values.go @@ -6,18 +6,14 @@ import ( "os" "strings" + "github.com/clevyr/yampl/internal/config/flags" "github.com/clevyr/yampl/internal/visitor" "github.com/spf13/cobra" "gopkg.in/yaml.v3" ) -const ( - ValueFlag = "value" - ValueFlagShort = "v" -) - func registerValuesFlag(cmd *cobra.Command) { - cmd.Flags().StringToStringP(ValueFlag, ValueFlagShort, map[string]string{}, "Define a template variable. Can be used more than once.") + cmd.Flags().StringToStringP(flags.ValueFlag, flags.ValueFlagShort, map[string]string{}, "Define a template variable. Can be used more than once.") err := cmd.RegisterFlagCompletionFunc("value", valueCompletion) if err != nil { panic(err) diff --git a/internal/config/flags/flags.go b/internal/config/flags/flags.go new file mode 100644 index 0000000..f5b2d39 --- /dev/null +++ b/internal/config/flags/flags.go @@ -0,0 +1,6 @@ +package flags + +const ( + ValueFlag = "value" + ValueFlagShort = "v" +) diff --git a/internal/util/completion.go b/internal/util/completion.go new file mode 100644 index 0000000..396ff96 --- /dev/null +++ b/internal/util/completion.go @@ -0,0 +1,7 @@ +package util + +import "github.com/spf13/cobra" + +func BoolCompletion(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) { + return []string{"true", "false"}, cobra.ShellCompDirectiveNoFileComp +} diff --git a/internal/util/flags.go b/internal/util/flags.go index 7123e5d..7861b69 100644 --- a/internal/util/flags.go +++ b/internal/util/flags.go @@ -4,7 +4,7 @@ import ( "bufio" "strings" - "github.com/clevyr/yampl/cmd" + "github.com/clevyr/yampl/internal/config/flags" ) func FixStringToStringNewlines(s []string) []string { @@ -35,10 +35,10 @@ func FixStringToStringNewlines(s []string) []string { } func hasValueFlag(s string) bool { - return s == "-"+cmd.ValueFlagShort || - s == "--"+cmd.ValueFlag || - strings.HasPrefix(s, "-"+cmd.ValueFlagShort+"=") || - strings.HasPrefix(s, "--"+cmd.ValueFlag+"=") + return s == "-"+flags.ValueFlagShort || + s == "--"+flags.ValueFlag || + strings.HasPrefix(s, "-"+flags.ValueFlagShort+"=") || + strings.HasPrefix(s, "--"+flags.ValueFlag+"=") } func fixArgNewlines(arg string) []string {