Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
### Bug fixes
- fix(instance): create instance with an IPv6 #788

## Features
- feat(cli scaffold): allow deprecated flags #789

### Breaking changes
- instance create: merge --ipv6 and --private-instance into --public-ip #788
- nodepool: replace --ipv6 and --nodepool-ipv6 with --public-ip and --nodepool-public-ip #788
Expand Down
16 changes: 15 additions & 1 deletion cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,9 @@ func MustCLICommandFlagName(c cliCommand, field interface{}) string {
// - cli-usage:"<usage help>": an optional string to use as flag usage
// help message. For positional arguments, this field is used as argument
// label for the "use" command help.
// - cli-hidden:"": mark the corresponding flag "hidden".
// - cli-hidden:"": mark the corresponding flag "hidden"
// - cli-deprecated:"<deprecation message>": mark the corresponding flag as hidden
// and display the deprecation message when used.
func cliCommandFlagSet(c cliCommand) (*pflag.FlagSet, error) {
fs := pflag.NewFlagSet("", pflag.ExitOnError)
cv := reflect.ValueOf(c)
Expand Down Expand Up @@ -247,6 +249,11 @@ func cliCommandFlagSet(c cliCommand) (*pflag.FlagSet, error) {
flagUsage = v
}

var deprecatedMsg *string
if v, ok := cTypeField.Tag.Lookup("cli-deprecated"); ok {
deprecatedMsg = &v
}

flagDefaultValue := cv.Field(i).Interface()

switch t := cTypeField.Type.Kind(); t {
Expand Down Expand Up @@ -285,6 +292,13 @@ func cliCommandFlagSet(c cliCommand) (*pflag.FlagSet, error) {
default:
return nil, cliCommandImplemError{fmt.Sprintf("unsupported type %s", t)}
}
if deprecatedMsg != nil {
if err := fs.MarkDeprecated(flagName, *deprecatedMsg); err != nil {
return nil, cliCommandImplemError{
reason: fmt.Sprintf("unable to deprecate flag: %s, err: %s", flagName, err.Error()),
}
}
}

if _, ok := cTypeField.Tag.Lookup("cli-hidden"); ok {
if err := fs.MarkHidden(flagName); err != nil {
Expand Down
9 changes: 9 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type testCLICmd struct {
Bool bool
MultiStrings []string `cli-flag:"multi-string-value" cli-usage:"multiple strings"`
StringsMap map[string]string
Deprecated string `cli-flags:"deprecated" cli-deprecated:"use another flag instead"`

aliases []string `cli:"-"`
short string `cli:"-"`
Expand Down Expand Up @@ -49,6 +50,7 @@ func Test_cliCommandFlagName(t *testing.T) {
func Test_cliCommandFlagSet(t *testing.T) {
var (
testSingleStringValue = "test"
testDeprecatedValue = "deprecated"
testInt64Value int64 = 42
testBoolValue = true
testMultiStringsValue = []string{"a", "b", "c"}
Expand All @@ -61,14 +63,17 @@ func Test_cliCommandFlagSet(t *testing.T) {
Bool: testBoolValue,
MultiStrings: testMultiStringsValue,
StringsMap: testStringsMap,
Deprecated: testDeprecatedValue,
}

expected := pflag.NewFlagSet("", pflag.ExitOnError)
expected.StringP("single-string", "s", testSingleStringValue, "")
expected.StringP("deprecated", "", testDeprecatedValue, "")
expected.Int64P("int64", "i", testInt64Value, "")
expected.BoolP("bool", "", testBoolValue, "")
expected.StringSliceP("multi-string-value", "", testMultiStringsValue, "multiple strings")
expected.StringToStringP("strings-map", "", testStringsMap, "")
require.NoError(t, expected.MarkDeprecated("deprecated", "use another flag instead"))

actual, err := cliCommandFlagSet(cmd)
require.NoError(t, err)
Expand Down Expand Up @@ -100,6 +105,7 @@ func Test_cliCommandUse(t *testing.T) {
func Test_cliCommandDefaultPreRun(t *testing.T) {
var (
testRequiredArg = "required-arg"
testDeprecatedValue = "deprecated"
testOptionalArgs = []string{"optional-arg1", "optional-arg2"}
testSingleStringValue = "test"
testInt64Value int64 = 42
Expand All @@ -114,6 +120,7 @@ func Test_cliCommandDefaultPreRun(t *testing.T) {
testFlags.BoolP("bool", "", false, "")
testFlags.StringSliceP("multi-string-value", "", nil, "multiple strings")
testFlags.StringToStringP("strings-map", "", nil, "")
testFlags.StringP("deprecated", "", "", "")

type args struct {
cmd *cobra.Command
Expand Down Expand Up @@ -182,6 +189,7 @@ func Test_cliCommandDefaultPreRun(t *testing.T) {
flags.BoolP("bool", "", testBoolValue, "")
flags.StringSliceP("multi-string-value", "", testMultiStringsValue, "")
flags.StringToStringP("strings-map", "", testStringsMap, "")
flags.StringP("deprecated", "", testDeprecatedValue, "")

testCmd := new(cobra.Command)
flags.VisitAll(func(flag *pflag.Flag) { testCmd.Flags().AddFlag(flag) })
Expand All @@ -192,6 +200,7 @@ func Test_cliCommandDefaultPreRun(t *testing.T) {
expected: &testCLICmd{
RequiredArg: testRequiredArg,
SingleString: testSingleStringValue,
Deprecated: testDeprecatedValue,
Int64: testInt64Value,
Bool: testBoolValue,
MultiStrings: testMultiStringsValue,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/exoscale/cli

go 1.24.12
go 1.24.13

require (
github.com/aws/aws-sdk-go-v2 v1.2.0
Expand Down