Skip to content

Commit de26e8d

Browse files
fix handling of flags that can have negative integer values
Signed-off-by: Swapnanil-Gupta <swpnlg@amazon.com>
1 parent d3fddae commit de26e8d

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

cmd/finch/nerdctl_remote.go

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"maps"
1313
"path/filepath"
1414
"runtime"
15+
"strconv"
1516
"strings"
1617

1718
"github.com/aws/aws-sdk-go-v2/aws"
@@ -507,8 +508,10 @@ func (nc *nerdctlCommand) handleFlagArg(arg string, nextArg string) (bool, strin
507508
// long flag concatenated to value by '=': --<long_flag>="<value>"
508509
skip = false
509510
flagKey, flagVal, _ = strings.Cut(arg, "=")
510-
case strings.HasPrefix(arg, "--") && !strings.HasPrefix(nextArg, "-"):
511-
// long flag followed by a value: --<long_flag> "<value>"
511+
case strings.HasPrefix(arg, "--") && (isNumeric(nextArg) || !strings.HasPrefix(nextArg, "-")):
512+
// long flag followed by a value (including a negative number): --<long_flag> "<value>".
513+
// the isNumeric check is needed because the value can be a negative number.
514+
// for example, in our health check tests where we pass --health-retries -5 or --health-timeout -5s
512515
skip = true
513516
flagKey = arg
514517
flagVal = nextArg
@@ -522,8 +525,8 @@ func (nc *nerdctlCommand) handleFlagArg(arg string, nextArg string) (bool, strin
522525
skip = false
523526
flagKey = arg[:2]
524527
flagVal = arg[2:]
525-
case strings.HasPrefix(arg, "-") && len(arg) == 2 && !strings.HasPrefix(nextArg, "-"):
526-
// short flag followed by a value: -? "<value>" or -? <value>
528+
case strings.HasPrefix(arg, "-") && len(arg) == 2 && (isNumeric(nextArg) || !strings.HasPrefix(nextArg, "-")):
529+
// short flag followed by a value (including a negative number): -? "<value>" or -? <value>
527530
skip = true
528531
flagKey = arg
529532
flagVal = nextArg
@@ -613,3 +616,21 @@ func handleEnvFile(fs afero.Fs, systemDeps NerdctlCommandSystemDeps, arg, arg2 s
613616
}
614617
return skip, envs, nil
615618
}
619+
620+
func isNumeric(arg string) bool {
621+
if arg == "" {
622+
return false
623+
}
624+
// handle the case where the arg can be a negative number followed by a char
625+
// for example: --health-timeout -5s
626+
if arg[0] == '-' && len(arg) > 1 {
627+
for i := 1; i < len(arg); i++ {
628+
if arg[i] < '0' || arg[i] > '9' {
629+
return i > 1
630+
}
631+
}
632+
return true
633+
}
634+
_, err := strconv.ParseInt(arg, 10, 64)
635+
return err == nil
636+
}

0 commit comments

Comments
 (0)