Skip to content

Commit

Permalink
fix: default flag parser for apps (#4483)
Browse files Browse the repository at this point in the history
* fix default flag parse for apps

* remove bug condition

* fix. unit tests

* add changelog

---------

Co-authored-by: Julien Robert <[email protected]>
(cherry picked from commit 7f91423)

# Conflicts:
#	ignite/services/plugin/flag_test.go
#	ignite/services/plugin/grpc/v1/interface_flag.go
  • Loading branch information
Pantani authored and mergify[bot] committed Feb 1, 2025
1 parent 538870f commit 48f3702
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 21 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
### Bug Fixes

- [#4474](https://github.com/ignite/cli/pull/4474) Fix issue in `build --release` command
- [#4479](https://github.com/ignite/cli/pull/4479) Scaffold an `uint64 type crashs Ignite
- [#4483](https://github.com/ignite/cli/pull/4483) Fix default flag parser for apps

## [`v28.7.0`](https://github.com/ignite/cli/releases/tag/v28.7.0)

Expand Down
12 changes: 1 addition & 11 deletions ignite/services/plugin/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,5 @@ func flagValue(flag *Flag) string {
if flag.Value != "" {
return flag.Value
}
if flag.DefaultValue != "" {
return flag.DefaultValue
}
if flag.Type == FlagTypeBool ||
flag.Type == FlagTypeInt ||
flag.Type == FlagTypeInt64 ||
flag.Type == FlagTypeUint ||
flag.Type == FlagTypeUint64 {
return "0"
}
return ""
return flag.DefaultValue
}
20 changes: 14 additions & 6 deletions ignite/services/plugin/flag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestFlags_GetBool(t *testing.T) {
name: "flag without value and default value",
key: flagBool3,
f: testFlags,
want: false,
err: errors.New("strconv.ParseBool: parsing \"\": invalid syntax"),
},
{
name: "invalid flag type",
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestFlags_GetInt(t *testing.T) {
name: "flag without value and default value",
key: flagInt3,
f: testFlags,
want: 0,
err: errors.New("strconv.Atoi: parsing \"\": invalid syntax"),
},
{
name: "invalid flag type",
Expand All @@ -182,7 +182,7 @@ func TestFlags_GetInt(t *testing.T) {
name: "wrong flag value without default or value",
key: flagWrongType3,
f: testFlags,
want: 0,
err: errors.New("strconv.Atoi: parsing \"\": invalid syntax"),
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -223,7 +223,7 @@ func TestFlags_GetInt64(t *testing.T) {
name: "flag without value and default value",
key: flagInt643,
f: testFlags,
want: 0,
err: errors.New("strconv.ParseInt: parsing \"\": invalid syntax"),
},
{
name: "invalid flag type",
Expand Down Expand Up @@ -400,7 +400,7 @@ func TestFlags_GetUint(t *testing.T) {
name: "flag without value and default value",
key: flagUint3,
f: testFlags,
want: 0,
err: errors.New("strconv.ParseUint: parsing \"\": invalid syntax"),
},
{
name: "invalid flag type",
Expand Down Expand Up @@ -459,7 +459,7 @@ func TestFlags_GetUint64(t *testing.T) {
name: "flag without value and default value",
key: flagUint643,
f: testFlags,
want: 0,
err: errors.New("strconv.ParseUint: parsing \"\": invalid syntax"),
},
{
name: "invalid flag type",
Expand Down Expand Up @@ -570,6 +570,14 @@ func Test_flagValue(t *testing.T) {
flag: &Flag{Name: flagString1},
want: "",
},
<<<<<<< HEAD

Check failure on line 573 in ignite/services/plugin/flag_test.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

expected operand, found '<<'

Check failure on line 573 in ignite/services/plugin/flag_test.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

expected operand, found '<<'
=======
{
name: "number without value and default value",
flag: &Flag{Name: flagUint642, Type: FlagTypeUint64},
want: "",
},
>>>>>>> 7f914230 (fix: default flag parser for apps (#4483))
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
60 changes: 56 additions & 4 deletions ignite/services/plugin/grpc/v1/interface_flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ func (f *Flag) ExportToFlagSet(fs *pflag.FlagSet) error {
if f.DefaultValue == "" {
f.DefaultValue = "0"
}
if f.Value == "" {
f.Value = "0"
}
}

switch f.Type {
Expand All @@ -56,38 +53,71 @@ func (f *Flag) ExportToFlagSet(fs *pflag.FlagSet) error {
}

fs.BoolP(f.Name, f.Shorthand, v, f.Usage)
<<<<<<< HEAD

Check failure on line 56 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on ubuntu-latest

syntax error: unexpected <<, expected case or default or }

Check failure on line 56 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on macos-latest

syntax error: unexpected <<, expected case or default or }

Check failure on line 56 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on macos-latest

syntax error: unexpected <<, expected case or default or }

Check failure on line 56 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on ubuntu-latest

syntax error: unexpected <<, expected case or default or }

Check failure on line 56 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

syntax error: unexpected <<, expected case or default or }

Check failure on line 56 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

syntax error: unexpected <<, expected case or default or }

Check failure on line 56 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / Lint Go code

syntax error: unexpected <<, expected case or default or }

Check failure on line 56 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / Lint Go code

expected statement, found '<<' (typecheck)
fs.Set(f.Name, f.Value)
=======
if f.Value != "" {

Check failure on line 59 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on ubuntu-latest

syntax error: unexpected if, expected :

Check failure on line 59 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on macos-latest

syntax error: unexpected if, expected :

Check failure on line 59 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on macos-latest

syntax error: unexpected if, expected :

Check failure on line 59 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on ubuntu-latest

syntax error: unexpected if, expected :

Check failure on line 59 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

syntax error: unexpected if, expected :

Check failure on line 59 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

syntax error: unexpected if, expected :

Check failure on line 59 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / Lint Go code

syntax error: unexpected if, expected :
if err := fs.Set(f.Name, f.Value); err != nil {
return newDefaultFlagValueError(cobraFlagTypeBool, f.Value)
}
}
>>>>>>> 7f914230 (fix: default flag parser for apps (#4483))

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on ubuntu-latest

syntax error: unexpected >>, expected }

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on macos-latest

syntax error: unexpected >>, expected }

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on macos-latest

invalid character U+0023 '#'

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on macos-latest

syntax error: unexpected >>, expected }

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on macos-latest

invalid character U+0023 '#'

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on ubuntu-latest

syntax error: unexpected >>, expected }

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

syntax error: unexpected >>, expected }

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

invalid character U+0023 '#'

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

syntax error: unexpected >>, expected }

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

invalid character U+0023 '#'

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / Lint Go code

syntax error: unexpected >>, expected }

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / Lint Go code

invalid character U+0023 '#'

Check failure on line 64 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / Lint Go code

expected statement, found '>>' (typecheck)
case Flag_TYPE_FLAG_INT:
v, err := strconv.Atoi(f.DefaultValue)
if err != nil {
return newDefaultFlagValueError(cobraFlagTypeInt, f.DefaultValue)
}

fs.IntP(f.Name, f.Shorthand, v, f.Usage)
<<<<<<< HEAD
fs.Set(f.Name, f.Value)
=======
if f.Value != "" {
if err := fs.Set(f.Name, f.Value); err != nil {
return newDefaultFlagValueError(cobraFlagTypeInt, f.Value)
}
}
>>>>>>> 7f914230 (fix: default flag parser for apps (#4483))

Check failure on line 80 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 80 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on macos-latest

invalid character U+0023 '#'

Check failure on line 80 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on macos-latest

invalid character U+0023 '#'

Check failure on line 80 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 80 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

invalid character U+0023 '#'

Check failure on line 80 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

invalid character U+0023 '#'

Check failure on line 80 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / Lint Go code

invalid character U+0023 '#'
case Flag_TYPE_FLAG_UINT:
v, err := strconv.ParseUint(f.DefaultValue, 10, 64)
if err != nil {
return newDefaultFlagValueError(cobraFlagTypeUint, f.DefaultValue)
}

fs.UintP(f.Name, f.Shorthand, uint(v), f.Usage)
<<<<<<< HEAD
fs.Set(f.Name, f.Value)
=======
if f.Value != "" {
if err := fs.Set(f.Name, f.Value); err != nil {
return newDefaultFlagValueError(cobraFlagTypeUint, f.Value)
}
}
>>>>>>> 7f914230 (fix: default flag parser for apps (#4483))

Check failure on line 96 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 96 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on macos-latest

invalid character U+0023 '#'

Check failure on line 96 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on macos-latest

invalid character U+0023 '#'

Check failure on line 96 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 96 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

invalid character U+0023 '#'

Check failure on line 96 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

invalid character U+0023 '#'

Check failure on line 96 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / Lint Go code

invalid character U+0023 '#'
case Flag_TYPE_FLAG_INT64:
v, err := strconv.ParseInt(f.DefaultValue, 10, 64)
if err != nil {
return newDefaultFlagValueError(cobraFlagTypeInt64, f.DefaultValue)
}

fs.Int64P(f.Name, f.Shorthand, v, f.Usage)
<<<<<<< HEAD
fs.Set(f.Name, f.Value)
=======
if f.Value != "" {
if err := fs.Set(f.Name, f.Value); err != nil {
return newDefaultFlagValueError(cobraFlagTypeInt64, f.Value)
}
}
>>>>>>> 7f914230 (fix: default flag parser for apps (#4483))

Check failure on line 112 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 112 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on macos-latest

invalid character U+0023 '#'

Check failure on line 112 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on macos-latest

invalid character U+0023 '#'

Check failure on line 112 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 112 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

invalid character U+0023 '#'

Check failure on line 112 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

invalid character U+0023 '#'

Check failure on line 112 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / Lint Go code

invalid character U+0023 '#'
case Flag_TYPE_FLAG_UINT64:
v, err := strconv.ParseUint(f.DefaultValue, 10, 64)
if err != nil {
return newDefaultFlagValueError(cobraFlagTypeInt64, f.DefaultValue)
return newDefaultFlagValueError(cobraFlagTypeUint64, f.DefaultValue)
}

fs.Uint64P(f.Name, f.Shorthand, v, f.Usage)
<<<<<<< HEAD
fs.Set(f.Name, f.Value)
case Flag_TYPE_FLAG_STRING_SLICE:
s := strings.Trim(f.DefaultValue, "[]")
Expand All @@ -96,6 +126,28 @@ func (f *Flag) ExportToFlagSet(fs *pflag.FlagSet) error {
case Flag_TYPE_FLAG_STRING_UNSPECIFIED:
fs.StringP(f.Name, f.Shorthand, f.DefaultValue, f.Usage)
fs.Set(f.Name, f.Value)
=======
if f.Value != "" {
if err := fs.Set(f.Name, f.Value); err != nil {
return newDefaultFlagValueError(cobraFlagTypeUint64, f.Value)
}
}
case Flag_TYPE_FLAG_STRING_SLICE:
s := strings.Trim(f.DefaultValue, "[]")
fs.StringSliceP(f.Name, f.Shorthand, strings.Fields(s), f.Usage)
if f.Value != "" {
if err := fs.Set(f.Name, strings.Trim(f.Value, "[]")); err != nil {
return newDefaultFlagValueError(cobraFlagTypeStringSlice, f.Value)
}
}
case Flag_TYPE_FLAG_STRING_UNSPECIFIED:
fs.StringP(f.Name, f.Shorthand, f.DefaultValue, f.Usage)
if f.Value != "" {
if err := fs.Set(f.Name, f.Value); err != nil {
return newDefaultFlagValueError(cobraFlagTypeString, f.Value)
}
}
>>>>>>> 7f914230 (fix: default flag parser for apps (#4483))

Check failure on line 150 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 150 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on macos-latest

invalid character U+0023 '#'

Check failure on line 150 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test plugin on macos-latest

invalid character U+0023 '#'

Check failure on line 150 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test network on ubuntu-latest

invalid character U+0023 '#'

Check failure on line 150 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

invalid character U+0023 '#'

Check failure on line 150 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

invalid character U+0023 '#'

Check failure on line 150 in ignite/services/plugin/grpc/v1/interface_flag.go

View workflow job for this annotation

GitHub Actions / Lint Go code

invalid character U+0023 '#' (typecheck)
}
return nil
}
Expand Down

0 comments on commit 48f3702

Please sign in to comment.