Skip to content

Commit fc9d053

Browse files
jon-stewartafiune
andauthored
fix: component args (#1512)
Signed-off-by: Salim Afiune Maya <[email protected]> Co-authored-by: Salim Afiune Maya <[email protected]>
1 parent 9ba45aa commit fc9d053

File tree

2 files changed

+100
-7
lines changed

2 files changed

+100
-7
lines changed

cli/cmd/component_args.go

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ func (p *componentArgParser) parseLongArg(flags *pflag.FlagSet, s string, args [
8383
// We're actually a bit stuck here as we don't know if this flag
8484
// takes an argument or not, so we don't know whether or not to consume
8585
// the next arg. What we'll do is peek ahead, and if the next arg does
86-
// not start with - then we'll take it.
87-
if len(args) > 0 && len(args[0]) > 0 && args[0][0] == '-' {
86+
// not start with '--' then we'll take it.
87+
if len(args) > 0 && len(args[0]) > 0 && (args[0][0] == '-' && args[0][1] == '-') {
8888
// This component flag does not take an arg
8989
return args
9090
}
@@ -126,14 +126,31 @@ func (p *componentArgParser) parseShortArg(flags *pflag.FlagSet, s string, args
126126
if flag == nil {
127127
// Not our flag, pass to the component. Like the long form above we
128128
// don't know whether to consume an extra arg, so we'll do the same
129-
// thing: if the next arg does not start with - then pass it along
130-
p.componentArgs = append(p.componentArgs, fmt.Sprintf("-%s", shorthand))
131-
if len(shorthands) == 1 && (len(args) > 0 && len(args[0]) > 0 && args[0][0] == '-') {
129+
// thing: if the next arg does not start with `--`` then pass it along
130+
131+
// handles case
132+
// -n
133+
if len(shorthands) == 1 {
134+
p.componentArgs = append(p.componentArgs, fmt.Sprintf("-%s", shorthand))
135+
return args
136+
}
137+
138+
// handles cases
139+
// -n=1234
140+
// -n1234
141+
if len(shorthands) > 2 {
142+
p.componentArgs = append(p.componentArgs, fmt.Sprintf("-%s", shorthands))
143+
return args
144+
}
145+
146+
// handles cases
147+
// -n 24h
148+
// -n -24h
149+
if len(shorthands) == 1 && len(args) > 0 && len(args[0]) > 0 && args[0][0] == '-' && args[0][1] != '-' {
150+
p.componentArgs = append(p.componentArgs, fmt.Sprintf("-%s", shorthand))
132151
p.componentArgs = append(p.componentArgs, args[0])
133152
return args[2:]
134153
}
135-
shorthands = shorthands[1:]
136-
continue
137154
}
138155

139156
if len(shorthands) > 2 && shorthands[1] == '=' {

cli/cmd/component_args_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,82 @@ func TestComponentArgs(t *testing.T) {
7575
"--debug",
7676
},
7777
},
78+
{
79+
[]string{
80+
"iac", "negative-values", "--time", "-24h",
81+
},
82+
[]string{
83+
"iac", "negative-values", "--time", "-24h",
84+
},
85+
[]string{},
86+
},
87+
{
88+
// We do not have access to the component flag configuration and therefore cannot
89+
// support the shorthand expansion of `-xyz` -> `-x -y -z`. We have to treat it as
90+
// `x=yz` and pass through to the component.
91+
[]string{
92+
"iac", "shorthands", "-xyz",
93+
},
94+
[]string{
95+
"iac", "shorthands", "-xyz",
96+
},
97+
[]string{},
98+
},
99+
{
100+
[]string{
101+
"iac", "long-assign", "--xyz=value",
102+
},
103+
[]string{
104+
"iac", "long-assign", "--xyz=value",
105+
},
106+
[]string{},
107+
},
108+
{
109+
[]string{
110+
"iac", "short-assign", "-x=value", "-y=true", "-t -24h", "-n=1234", "-n1234",
111+
},
112+
[]string{
113+
"iac", "short-assign", "-x=value", "-y=true", "-t -24h", "-n=1234", "-n1234",
114+
},
115+
[]string{},
116+
},
117+
{
118+
[]string{
119+
"sca", "scan", "--key=projectId=${CI_PROJECT_ID}",
120+
"--link", "https://github.com/lacework-dev/project-abc/blob/foo/$FILENAME#L$LINENUMBER",
121+
"--tool-paths=checkov=/app/checkov,opal=/app/lacework-opal-releases/latest/opal",
122+
"--fail", "High=2",
123+
"--foo=",
124+
},
125+
[]string{
126+
"sca", "scan", "--key=projectId=${CI_PROJECT_ID}",
127+
"--link", "https://github.com/lacework-dev/project-abc/blob/foo/$FILENAME#L$LINENUMBER",
128+
"--tool-paths=checkov=/app/checkov,opal=/app/lacework-opal-releases/latest/opal",
129+
"--fail", "High=2",
130+
"--foo=",
131+
},
132+
[]string{},
133+
},
134+
{
135+
// -x=true -y=true -z="hello"
136+
[]string{
137+
"iac", "bool-str-assign", "-xyz", "hello", "-x",
138+
},
139+
[]string{
140+
"iac", "bool-str-assign", "-xyz", "hello", "-x",
141+
},
142+
[]string{},
143+
},
144+
{
145+
// -x=true -y=true -z="hello"
146+
[]string{
147+
"iac", "bool-str-assign", "-xyz=hello",
148+
},
149+
[]string{
150+
"iac", "bool-str-assign", "-xyz=hello",
151+
},
152+
[]string{},
153+
},
78154
} {
79155
p := componentArgParser{}
80156
p.parseArgs(flags, k.args)

0 commit comments

Comments
 (0)