Skip to content

Commit

Permalink
fix: conflict in parsing of -h flag for help and hostname in containe…
Browse files Browse the repository at this point in the history
…r run

Signed-off-by: Shubharanshu Mahapatra <[email protected]>
  • Loading branch information
Shubhranshu153 committed Jan 21, 2025
1 parent 779e638 commit 1ef973f
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 24 deletions.
3 changes: 2 additions & 1 deletion cmd/finch/nerdctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ func (nc *nerdctlCommand) shouldReplaceForHelp(cmdName string, args []string) bo
}
}

// this only needs to handle `container run`.
for _, arg := range args {
if arg == "--help" || arg == "-h" {
if arg == "--help" || (arg == "-h" && !strings.Contains(cmdName, "run")) {
return true
}
}
Expand Down
76 changes: 53 additions & 23 deletions cmd/finch/nerdctl_remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,17 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error {
nerdctlArgs = append(nerdctlArgs, arg)
case arg == "--add-host":
// exact match to --add-host
args[i+1], err = resolveIP(args[i+1], nc.logger, nc.ecc)
if err != nil {
return err
if len(args) > i+1 {
args[i+1], err = resolveIP(args[i+1], nc.logger, nc.ecc)
if err != nil {
return err
}
nerdctlArgs = append(nerdctlArgs, arg)
} else {
// no value found for arg flag
// pass the arg as a nerdctl command argument
nerdctlArgs = append(nerdctlArgs, arg)
}
nerdctlArgs = append(nerdctlArgs, arg)
case strings.HasPrefix(arg, "--add-host"):
// arg begins with --add-host
resolvedIP, err := resolveIP(arg[11:], nc.logger, nc.ecc)
Expand All @@ -158,21 +164,33 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error {
case strings.HasPrefix(arg, "--env-file"):
// exact match to --env-file
// or arg begins with --env-file
shouldSkip, addEnvs, err := handleEnvFile(nc.fs, nc.systemDeps, arg, args[i+1])
if err != nil {
return err
if len(args) > i+1 {
shouldSkip, addEnvs, err := handleEnvFile(nc.fs, nc.systemDeps, arg, args[i+1])
if err != nil {
return err
}
skip = shouldSkip
fileEnvs = append(fileEnvs, addEnvs...)
} else {
// no value found for arg flag
// pass the arg as a nerdctl command argument
nerdctlArgs = append(nerdctlArgs, arg)
}
skip = shouldSkip
fileEnvs = append(fileEnvs, addEnvs...)
case argIsEnv(arg):
// exact match to either -e or --env
// or arg begins with -e or --env
// -e="<value>", -e"<value>"
// --env="<key>=<value>", --env"<key>=<value>"
shouldSkip, addEnv := handleEnv(nc.systemDeps, arg, args[i+1])
skip = shouldSkip
if addEnv != "" {
envs = append(envs, addEnv)
if len(args) > i+1 {
shouldSkip, addEnv := handleEnv(nc.systemDeps, arg, args[i+1])
skip = shouldSkip
if addEnv != "" {
envs = append(envs, addEnv)
}
} else {
// no value found for arg flag
// pass the arg as a nerdctl command argument
nerdctlArgs = append(nerdctlArgs, arg)
}
case shortFlagBoolSet.Has(arg) || longFlagBoolSet.Has(arg):
// exact match to a short no argument flag: -?
Expand All @@ -195,23 +213,35 @@ func (nc *nerdctlCommand) run(cmdName string, args []string) error {
// or begins with a short arg flag:
// short arg flag concatenated to value: -?"<value>"
// short arg flag equated to value: -?="<value>" or -?=<value>
shouldSkip, addKey, addVal := nc.handleFlagArg(arg, args[i+1])
skip = shouldSkip
if addKey != "" {
nerdctlArgs = append(nerdctlArgs, addKey)
nerdctlArgs = append(nerdctlArgs, addVal)
if len(args) > i+1 {
shouldSkip, addKey, addVal := nc.handleFlagArg(arg, args[i+1])
skip = shouldSkip
if addKey != "" {
nerdctlArgs = append(nerdctlArgs, addKey)
nerdctlArgs = append(nerdctlArgs, addVal)
}
} else {
// no value found for short arg flag
// pass the arg as a nerdctl command argument
nerdctlArgs = append(nerdctlArgs, arg)
}
case strings.HasPrefix(arg, "--"):
// exact match to a long arg flag: -<long_flag>
// next arg must be the <value>
// or begins with a long arg flag:
// long arg flag concatenated to value: --<long_flag>"<value>"
// long arg flag equated to value: --<long_flag>="<value>" or --<long_flag>=<value>
shouldSkip, addKey, addVal := nc.handleFlagArg(arg, args[i+1])
skip = shouldSkip
if addKey != "" {
nerdctlArgs = append(nerdctlArgs, addKey)
nerdctlArgs = append(nerdctlArgs, addVal)
if len(args) > i+1 {
shouldSkip, addKey, addVal := nc.handleFlagArg(arg, args[i+1])
skip = shouldSkip
if addKey != "" {
nerdctlArgs = append(nerdctlArgs, addKey)
nerdctlArgs = append(nerdctlArgs, addVal)
}
} else {
// no value found for arg flag
// pass the arg as a nerdctl command argument
nerdctlArgs = append(nerdctlArgs, arg)
}
default:
// arg other than a flag ("-?","--<long_flag>") or a skipped <flag_value>
Expand Down

0 comments on commit 1ef973f

Please sign in to comment.