Skip to content

Commit

Permalink
fix(cdk): handle -v flag for components (#1170)
Browse files Browse the repository at this point in the history
Two issues here - parsing of -v would loop without returning.  And
executing with -v (which cobra handles and prints the version) didn't
work.
  • Loading branch information
slshen authored Mar 7, 2023
1 parent 9259953 commit acf5a3c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
15 changes: 12 additions & 3 deletions cli/cmd/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func (c *cliState) LoadComponents() {
c.Log.Debugw("loading dynamic cli command",
"component", component.Name, "version", ver,
)
rootCmd.AddCommand(
componentCmd :=
&cobra.Command{
Use: component.Name,
Short: component.Description,
Expand All @@ -194,6 +194,15 @@ func (c *cliState) LoadComponents() {
DisableFlagParsing: true,
DisableFlagsInUseLine: true,
RunE: func(cmd *cobra.Command, args []string) error {
// cobra will automatically add a -v/--version flag to
// the command, but because for components we're not
// parsing the args at the usual point in time, we have
// to repeat the check for -v here
versionVal, _ := cmd.Flags().GetBool("version")
if versionVal {
cmd.Printf("%s version %s\n", cmd.Use, cmd.Version)
return nil
}
go func() {
// Start the gRPC server for components to communicate back
if err := c.Serve(); err != nil {
Expand All @@ -218,8 +227,8 @@ func (c *cliState) LoadComponents() {
// happens, let the user know that we would love to hear their feedback
return errors.New("something went pretty wrong here, contact [email protected]")
},
},
)
}
rootCmd.AddCommand(componentCmd)
}
}
}
Expand Down
1 change: 1 addition & 0 deletions cli/cmd/component_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ func (p *componentArgParser) parseShortArg(flags *pflag.FlagSet, s string, args
} else if flag.NoOptDefVal != "" {
// '-f' (arg was optional)
p.cliArgs = append(p.cliArgs, s)
return args
} else if len(shorthands) > 1 {
// '-farg'
p.cliArgs = append(p.cliArgs, s)
Expand Down
13 changes: 12 additions & 1 deletion cli/cmd/component_args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,24 @@ package cmd
import (
"testing"

"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)

func TestComponentArgs(t *testing.T) {
assert := assert.New(t)
flags := rootCmd.PersistentFlags()
flags := &pflag.FlagSet{}
rootCmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
flags.AddFlag(f)
})
// cobra adds a -v flag very late in the execution flow
flags.BoolP("version", "v", false, "version of command")
for _, k := range []struct{ args, expectedComponent, expectedCLI []string }{
{
[]string{"iac", "-v", "-d", "foo"},
[]string{"iac", "-d", "foo"},
[]string{"-v"},
},
{
[]string{"--profile", "none", "--debug"},
[]string{},
Expand Down

0 comments on commit acf5a3c

Please sign in to comment.