Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(extensions): add new booleanValue type #409

Merged
merged 1 commit into from
Oct 14, 2024
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
9 changes: 9 additions & 0 deletions pkg/cmdparser/cmdparser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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":
Expand Down
23 changes: 23 additions & 0 deletions pkg/flags/getters.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"log"
"net/http"
"os"
"strconv"
"strings"

"github.com/reubenmiller/go-c8y-cli/v2/pkg/c8yquery"
Expand Down Expand Up @@ -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) {
Expand Down
49 changes: 49 additions & 0 deletions tests/manual/extensions/example/body_basic_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
9 changes: 9 additions & 0 deletions tests/testdata/extensions/c8y-kitchensink/api/body_basic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
1 change: 1 addition & 0 deletions tools/schema/extensionCommands.json
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@
"attachment",
"binaryUploadURL",
"boolean",
"booleanValue",
"booleanDefault",
"certificate[]",
"certificatefile",
Expand Down