Skip to content

Commit c8ec00c

Browse files
authored
JGC-434 - Look for undeclared flags in arguments (#1511)
1 parent 7215c53 commit c8ec00c

File tree

2 files changed

+361
-7
lines changed

2 files changed

+361
-7
lines changed

plugins/components/conversionlayer.go

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/jfrog/gofrog/datastructures"
99
"github.com/jfrog/jfrog-cli-core/v2/common/commands"
1010
"github.com/jfrog/jfrog-cli-core/v2/docs/common"
11+
"github.com/jfrog/jfrog-cli-core/v2/utils/coreutils"
1112
"github.com/jfrog/jfrog-client-go/utils/errorutils"
1213
"github.com/urfave/cli"
1314
)
@@ -454,6 +455,11 @@ func getValueForStringFlag(f StringFlag, baseContext *cli.Context) (string, erro
454455
if value != "" {
455456
return value, nil
456457
}
458+
// We try to find the flag value in the command arguments.
459+
flagIndex, flagValue := findFlag(f.Name, baseContext.Args())
460+
if flagIndex != -1 {
461+
return flagValue, nil
462+
}
457463
if f.DefaultValue != "" {
458464
// Empty but has a default value defined.
459465
return f.DefaultValue, nil
@@ -466,8 +472,37 @@ func getValueForStringFlag(f StringFlag, baseContext *cli.Context) (string, erro
466472
}
467473

468474
func getValueForBoolFlag(f BoolFlag, baseContext *cli.Context) bool {
469-
if f.DefaultValue {
470-
return baseContext.BoolT(f.Name)
475+
if baseContext.IsSet(f.Name) {
476+
return baseContext.Bool(f.Name)
477+
}
478+
479+
// We try to find the flag value in the command arguments.
480+
flagIndex, flagValue := findFlag(f.Name, baseContext.Args())
481+
482+
if flagIndex != -1 {
483+
switch strings.ToLower(flagValue) {
484+
case "true":
485+
return true
486+
case "false":
487+
return false
488+
case "":
489+
return true
490+
default:
491+
return false
492+
}
493+
}
494+
495+
return f.DefaultValue
496+
}
497+
498+
func findFlag(flagName string, args []string) (flagIndex int, flagValue string) {
499+
var err error
500+
flagIndex, _, flagValue, err = coreutils.FindFlag(flagName, args)
501+
if err != nil {
502+
return
503+
}
504+
if flagIndex == -1 {
505+
flagIndex, _, flagValue, _ = coreutils.FindFlag("--"+flagName, args)
471506
}
472-
return baseContext.Bool(f.Name)
507+
return flagIndex, flagValue
473508
}

0 commit comments

Comments
 (0)