Skip to content

Commit 9313bd7

Browse files
committed
frontend/dockerfile: BFlags.Parse(): use strings.Cut()
Also some minor cleanup in the boolean value parsing; using a switch instead of if/elseif/else. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent a56df1e commit 9313bd7

File tree

1 file changed

+9
-16
lines changed
  • frontend/dockerfile/instructions

1 file changed

+9
-16
lines changed

frontend/dockerfile/instructions/bflag.go

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,7 @@ func (bf *BFlags) Parse() error {
156156
return nil
157157
}
158158

159-
arg = arg[2:]
160-
value := ""
161-
162-
index := strings.Index(arg, "=")
163-
if index >= 0 {
164-
value = arg[index+1:]
165-
arg = arg[:index]
166-
}
159+
arg, value, hasValue := strings.Cut(arg[2:], "=")
167160

168161
flag, ok := bf.flags[arg]
169162
if !ok {
@@ -180,27 +173,27 @@ func (bf *BFlags) Parse() error {
180173
switch flag.flagType {
181174
case boolType:
182175
// value == "" is only ok if no "=" was specified
183-
if index >= 0 && value == "" {
176+
if hasValue && value == "" {
184177
return errors.Errorf("missing a value on flag: %s", arg)
185178
}
186179

187-
lower := strings.ToLower(value)
188-
if lower == "" {
180+
switch strings.ToLower(value) {
181+
case "true", "":
189182
flag.Value = "true"
190-
} else if lower == "true" || lower == "false" {
191-
flag.Value = lower
192-
} else {
183+
case "false":
184+
flag.Value = "false"
185+
default:
193186
return errors.Errorf("expecting boolean value for flag %s, not: %s", arg, value)
194187
}
195188

196189
case stringType:
197-
if index < 0 {
190+
if !hasValue {
198191
return errors.Errorf("missing a value on flag: %s", arg)
199192
}
200193
flag.Value = value
201194

202195
case stringsType:
203-
if index < 0 {
196+
if !hasValue {
204197
return errors.Errorf("missing a value on flag: %s", arg)
205198
}
206199
flag.StringValues = append(flag.StringValues, value)

0 commit comments

Comments
 (0)