Skip to content

Commit 22c0726

Browse files
authored
fix(core/validate): log deprecated arg only when used (#2509)
1 parent ec0b0d6 commit 22c0726

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

internal/core/validate.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func DefaultCommandValidateFunc() CommandValidateFunc {
3636
return err
3737
}
3838

39-
validateDeprecated(ctx, cmd)
39+
validateDeprecated(ctx, cmd, cmdArgs, rawArgs)
4040
return nil
4141
}
4242
}
@@ -113,11 +113,26 @@ func validateNoConflict(cmd *Command, rawArgs args.RawArgs) error {
113113
}
114114

115115
// validateDeprecated print a warning message if a deprecated argument is used
116-
func validateDeprecated(ctx context.Context, cmd *Command) {
116+
func validateDeprecated(ctx context.Context, cmd *Command, cmdArgs interface{}, rawArgs args.RawArgs) {
117117
deprecatedArgs := cmd.ArgSpecs.GetDeprecated(true)
118-
for _, argSpec := range deprecatedArgs {
119-
helpCmd := cmd.GetCommandLine(extractMeta(ctx).BinaryName) + " --help"
120-
ExtractLogger(ctx).Warningf("The argument '%s' is deprecated, more info with: %s\n", argSpec.Name, helpCmd)
118+
for _, arg := range deprecatedArgs {
119+
fieldName := strcase.ToPublicGoName(arg.Name)
120+
fieldValues, err := getValuesForFieldByName(reflect.ValueOf(cmdArgs), strings.Split(fieldName, "."))
121+
if err != nil {
122+
validationErr := fmt.Errorf("could not validate arg value for '%v': invalid field name '%v': %v", arg.Name, fieldName, err.Error())
123+
if !arg.Required {
124+
logger.Infof(validationErr.Error())
125+
continue
126+
}
127+
panic(validationErr)
128+
}
129+
130+
for i := range fieldValues {
131+
if rawArgs.ExistsArgByName(strings.Replace(arg.Name, "{index}", strconv.Itoa(i), 1)) {
132+
helpCmd := cmd.GetCommandLine(extractMeta(ctx).BinaryName) + " --help"
133+
ExtractLogger(ctx).Warningf("The argument '%s' is deprecated, more info with: %s\n", arg.Name, helpCmd)
134+
}
135+
}
121136
}
122137
}
123138

0 commit comments

Comments
 (0)