Skip to content

Commit

Permalink
fix(cdk): Pass all global flags via env variables (#993)
Browse files Browse the repository at this point in the history
We were not passing a number of global flags via environment variables,
this change adds them all.

* fix(cdk): Pass LW_ORGANIZATION to components
* fix(cdk): -h, --help flags should be consider args
* test(lint): fix linting issues

Signed-off-by: Salim Afiune Maya <[email protected]>
  • Loading branch information
afiune authored Nov 2, 2022
1 parent 441fc00 commit 1dc9fad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
6 changes: 6 additions & 0 deletions cli/cmd/cli_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,12 @@ func (c *cliState) envs() []string {
fmt.Sprintf("LW_API_KEY=%s", c.KeyID),
fmt.Sprintf("LW_API_SECRET=%s", c.Secret),
fmt.Sprintf("LW_API_TOKEN=%s", c.Token),
fmt.Sprintf("LW_ORGANIZATION=%v", c.OrgLevel),
fmt.Sprintf("LW_NONINTERACTIVE=%v", c.nonInteractive),
fmt.Sprintf("LW_NOCACHE=%v", c.noCache),
fmt.Sprintf("LW_NOCOLOR=%s", os.Getenv("NO_COLOR")),
fmt.Sprintf("LW_LOG=%s", c.LogLevel),
fmt.Sprintf("LW_JSON=%v", c.jsonOutput),
}
}

Expand Down
4 changes: 4 additions & 0 deletions cli/cmd/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,10 @@ func filterCLIFlagsFromComponentArgs(args []string, globalFlags []*pflag.Flag) (
continue
}

if flag.Name == "help" || flag.Shorthand == "h" { // pass -h, --help flags as component arguments
continue
}

if flag.Name == argFlag || flag.Shorthand == argFlag {
// the argument is indeed a flag

Expand Down
11 changes: 9 additions & 2 deletions cli/cmd/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
var mockedGlobalFlags = []*pflag.Flag{
&pflag.Flag{Name: "profile", Shorthand: "p", Value: &mockStringFlagValue{}},
&pflag.Flag{Name: "debug", Shorthand: "", Value: &mockBoolFlagValue{}},
&pflag.Flag{Name: "help", Shorthand: "-h", Value: &mockBoolFlagValue{}},
&pflag.Flag{Name: "nocolor", Shorthand: "", Value: &mockBoolFlagValue{}},
&pflag.Flag{Name: "nocache", Shorthand: "", Value: &mockBoolFlagValue{}},
&pflag.Flag{Name: "noninteractive", Shorthand: "", Value: &mockBoolFlagValue{}},
Expand Down Expand Up @@ -61,14 +62,20 @@ func TestComponentFilterCLIFlagsFromComponentArgs(t *testing.T) {
[]string(nil), []string{"--profile", "p2", "--debug"}},

{"args that have both arguments and global flags should split them correctly",
[]string{"--profile", "p2", "iac", "terraform-scan", "--verbose", "--debug"}, mockedGlobalFlags,
[]string{"--profile", "p2", "iac", "terraform-scan", "--verbose", "--debug"},
mockedGlobalFlags,
[]string{"iac", "terraform-scan", "--verbose"}, []string{"--profile", "p2", "--debug"}},

{"complex args and flags with component commands and flags should split them correctly",
[]string{"comp-cmd", "--nocolor", "--comp-flag", "comp-subcommand", "--comp-flag2", "-c", "-p", "foo"},
[]string{"comp-cmd", "--nocolor", "--comp-flag",
"comp-subcommand", "--comp-flag2", "-c", "-p", "foo"},
mockedGlobalFlags,
[]string{"comp-cmd", "--comp-flag", "comp-subcommand", "--comp-flag2", "-c"},
[]string{"--nocolor", "-p", "foo"}},

{"-h or --help should not be consider a flag but instead a component argument",
[]string{"iac", "terraform-scan", "--help"}, mockedGlobalFlags,
[]string{"iac", "terraform-scan", "--help"}, []string(nil)},
}

for _, kase := range cases {
Expand Down

0 comments on commit 1dc9fad

Please sign in to comment.