From 60a2ec11ffec562ae4e5e6d1c36fb5ed8e33b68c Mon Sep 17 00:00:00 2001 From: Reuben Miller Date: Mon, 14 Oct 2024 20:31:04 +0200 Subject: [PATCH] feat(extensions): add new booleanValue type booleanValue is a string flag which accepts a value of true or false --- pkg/cmdparser/cmdparser.go | 9 ++++ pkg/flags/getters.go | 23 +++++++++ .../extensions/example/body_basic_tests.yaml | 49 +++++++++++++++++++ .../c8y-kitchensink/api/body_basic.yaml | 9 ++++ tools/schema/extensionCommands.json | 1 + 5 files changed, 91 insertions(+) diff --git a/pkg/cmdparser/cmdparser.go b/pkg/cmdparser/cmdparser.go index 506701ec3..51e9c8626 100644 --- a/pkg/cmdparser/cmdparser.go +++ b/pkg/cmdparser/cmdparser.go @@ -308,6 +308,13 @@ func AddFlag(cmd *CmdOptions, p *models.Parameter, factory *cmdutil.Factory) err } cmd.Command.Flags().Float32P(p.Name, p.ShortName, float32(defaultValue), p.GetDescription()) + case "booleanValue": + cmd.Command.Flags().StringP(p.Name, p.ShortName, p.Default, p.GetDescription()) + cmd.Completion = append( + cmd.Completion, + completion.WithValidateSet(p.Name, "true", "false"), + ) + case "boolean", "booleanDefault", "optional_fragment": defaultValue, err := strconv.ParseBool(p.Default) if err != nil { @@ -372,6 +379,8 @@ func GetOption(cmd *CmdOptions, p *models.Parameter, factory *cmdutil.Factory, a opts = append(opts, flags.WithFileContentsAsString(p.Name, targetProp, p.Value)) case "boolean": opts = append(opts, flags.WithBoolValue(p.Name, targetProp, p.Value)) + case "booleanValue": + opts = append(opts, flags.WithBooleanAsString(p.Name, targetProp, p.Value)) case "booleanDefault": opts = append(opts, flags.WithDefaultBoolValue(p.Name, targetProp, p.Value)) case "optional_fragment": diff --git a/pkg/flags/getters.go b/pkg/flags/getters.go index d4f2f7c2e..72e5a3812 100644 --- a/pkg/flags/getters.go +++ b/pkg/flags/getters.go @@ -10,6 +10,7 @@ import ( "log" "net/http" "os" + "strconv" "strings" "github.com/reubenmiller/go-c8y-cli/v2/pkg/c8yquery" @@ -253,6 +254,28 @@ func WithBoolValue(opts ...string) GetOption { } } +// WithBooleanAsString adds a boolean value from cli arguments where the flag accepts a value +func WithBooleanAsString(opts ...string) GetOption { + return func(cmd *cobra.Command, inputIterators *RequestInputIterators) (string, interface{}, error) { + src, dst, format := UnpackGetterOptions("", opts...) + if cmd.Flags().Changed(src) { + value, err := cmd.Flags().GetString(src) + + if err != nil { + return "", false, err + } + + if format != "" { + boolValue, err := strconv.ParseBool(applyFormatter(format, value)) + return dst, boolValue, err + } + boolValue, err := strconv.ParseBool(value) + return dst, boolValue, err + } + return "", false, nil + } +} + // WithDefaultBoolValue sets a boolean value regardless if the value has been provided by the flag or not func WithDefaultBoolValue(opts ...string) GetOption { return func(cmd *cobra.Command, inputIterators *RequestInputIterators) (string, interface{}, error) { diff --git a/tests/manual/extensions/example/body_basic_tests.yaml b/tests/manual/extensions/example/body_basic_tests.yaml index 57cb9711e..eb2e38809 100644 --- a/tests/manual/extensions/example/body_basic_tests.yaml +++ b/tests/manual/extensions/example/body_basic_tests.yaml @@ -85,6 +85,55 @@ tests: json: body: "{}" + booleanValue set true: + command: | + c8y kitchensink body booleanValue --value true | c8y util show --select body + stdout: + exactly: | + {"body":{"value":true}} + + booleanValue set 1: + command: | + c8y kitchensink body booleanValue --value 1 | c8y util show --select body + stdout: + exactly: | + {"body":{"value":true}} + + booleanValue set TRUE: + command: | + c8y kitchensink body booleanValue --value TRUE | c8y util show --select body + stdout: + exactly: | + {"body":{"value":true}} + + booleanValue set false: + command: | + c8y kitchensink body booleanValue --value false --select body | c8y util show --select body + stdout: + exactly: | + {"body":{"value":false}} + + booleanValue set 0: + command: | + c8y kitchensink body booleanValue --value 0 --select body | c8y util show --select body + stdout: + exactly: | + {"body":{"value":false}} + + booleanValue set FALSE: + command: | + c8y kitchensink body booleanValue --value FALSE --select body | c8y util show --select body + stdout: + exactly: | + {"body":{"value":false}} + + booleanValue set no value: + command: | + c8y kitchensink body booleanValue --select body | c8y util show --select body + stdout: + exactly: | + {"body":{}} + # # Date / Time # diff --git a/tests/testdata/extensions/c8y-kitchensink/api/body_basic.yaml b/tests/testdata/extensions/c8y-kitchensink/api/body_basic.yaml index e700fb14e..e3b911906 100644 --- a/tests/testdata/extensions/c8y-kitchensink/api/body_basic.yaml +++ b/tests/testdata/extensions/c8y-kitchensink/api/body_basic.yaml @@ -41,6 +41,15 @@ commands: type: optional_fragment description: enable + - name: booleanValue + path: inventory/managedObjects + description: Create object + method: POST + body: + - name: value + type: booleanValue + description: value + # # Date / Time # diff --git a/tools/schema/extensionCommands.json b/tools/schema/extensionCommands.json index 08c6dafa3..90234043c 100644 --- a/tools/schema/extensionCommands.json +++ b/tools/schema/extensionCommands.json @@ -243,6 +243,7 @@ "attachment", "binaryUploadURL", "boolean", + "booleanValue", "booleanDefault", "certificate[]", "certificatefile",