Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cdk): Pass all global flags via env variables #993

Merged
merged 4 commits into from
Nov 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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