Skip to content

Commit

Permalink
chore(cmd): Add missing flag completions
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 25, 2024
1 parent c3afbe9 commit ecd786d
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
33 changes: 32 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 2 additions & 6 deletions cmd/flag_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions internal/config/flags/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package flags

const (
ValueFlag = "value"
ValueFlagShort = "v"
)
7 changes: 7 additions & 0 deletions internal/util/completion.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 5 additions & 5 deletions internal/util/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bufio"
"strings"

"github.com/clevyr/yampl/cmd"
"github.com/clevyr/yampl/internal/config/flags"
)

func FixStringToStringNewlines(s []string) []string {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ecd786d

Please sign in to comment.