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
24 changes: 21 additions & 3 deletions cmd/multiple_flags.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd
Comment thread
emupton marked this conversation as resolved.

import (
"encoding/json"
"fmt"
"strconv"

"github.com/conduktor/ctl/internal/utils"
Expand Down Expand Up @@ -31,6 +33,10 @@ func NewMultipleFlags(command *cobra.Command, flagParams map[string]schema.FlagP
isFlagSet = true
defaultValue := 0
result[key] = command.Flags().Int(flag.FlagName, defaultValue, usage)
} else if flag.Type == "json" {
isFlagSet = true
defaultValue := ""
result[key] = command.Flags().String(flag.FlagName, defaultValue, "JSON value")
} else if utils.CdkDebug() {
println("Unknown flag type: " + flag.Type)
}
Expand All @@ -46,14 +52,26 @@ func NewMultipleFlags(command *cobra.Command, flagParams map[string]schema.FlagP
}
}

func (m *MultipleFlags) ExtractFlagValueForBodyParam() map[string]interface{} {
func (m *MultipleFlags) ExtractFlagValueForBodyParam() (map[string]interface{}, error) {
bodyParams := make(map[string]interface{})
for key, value := range m.result {
if value != nil && m.flagSetByUser(key) {
bodyParams[key] = value
if m.flagParams[key].Type == "json" {
str, ok := value.(*string)
if !ok {
panic("Expected json flag " + key + " to be a *string")
}
var decoded interface{}
if err := json.Unmarshal([]byte(*str), &decoded); err != nil {
return nil, fmt.Errorf("invalid JSON for flag --%s: %w", m.flagParams[key].FlagName, err)
}
bodyParams[key] = decoded
} else {
bodyParams[key] = value
}
}
}
return bodyParams
return bodyParams, nil
}

func (m *MultipleFlags) ExtractFlagValueForQueryParam() map[string]string {
Expand Down
31 changes: 30 additions & 1 deletion cmd/multiple_flags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ func TestExtractFlagValueForBodyParam(t *testing.T) {
"intParam": {FlagName: "intParam", Type: "integer"},
"notSetInt": {FlagName: "notSetInt", Type: "integer"},
"zeroParam": {FlagName: "zeroParam", Type: "integer"},
"jsonArray": {FlagName: "jsonArray", Type: "json"},
"jsonObject": {FlagName: "jsonObject", Type: "json"},
"notSetJson": {FlagName: "notSetJson", Type: "json"},
})
multipleFlags.result = map[string]interface{}{
"stringParam": func() *string { s := "test"; return &s }(),
Expand All @@ -79,6 +82,9 @@ func TestExtractFlagValueForBodyParam(t *testing.T) {
"intParam": func() *int { i := 123; return &i }(),
"notSetInt": func() *int { i := 0; return &i }(),
"zeroParam": func() *int { i := 0; return &i }(),
"jsonArray": func() *string { s := `[{"partition":1,"offset":42}]`; return &s }(),
"jsonObject": func() *string { s := `{"key":"value"}`; return &s }(),
"notSetJson": func() *string { s := ""; return &s }(),
}

expected := map[string]interface{}{
Expand All @@ -88,6 +94,8 @@ func TestExtractFlagValueForBodyParam(t *testing.T) {
"boolParamFalse": func() *bool { b := false; return &b }(),
"intParam": func() *int { i := 123; return &i }(),
"zeroParam": func() *int { i := 0; return &i }(),
"jsonArray": []interface{}{map[string]interface{}{"partition": float64(1), "offset": float64(42)}},
"jsonObject": map[string]interface{}{"key": "value"},
}

command.Flags().Lookup("stringParam").Changed = true
Expand All @@ -96,9 +104,30 @@ func TestExtractFlagValueForBodyParam(t *testing.T) {
command.Flags().Lookup("intParam").Changed = true
command.Flags().Lookup("zeroParam").Changed = true
command.Flags().Lookup("emptyString").Changed = true
result := multipleFlags.ExtractFlagValueForBodyParam()
command.Flags().Lookup("jsonArray").Changed = true
command.Flags().Lookup("jsonObject").Changed = true
result, err := multipleFlags.ExtractFlagValueForBodyParam()
if err != nil {
t.Fatalf("unexpected error: %s", err)
}

if !reflect.DeepEqual(result, expected) {
t.Error(spew.Printf("got %v, want %v", result, expected))
}
}

func TestExtractFlagValueForBodyParamInvalidJSON(t *testing.T) {
command := &cobra.Command{}
multipleFlags := NewMultipleFlags(command, map[string]schema.FlagParameterOption{
"jsonParam": {FlagName: "jsonParam", Type: "json"},
})
multipleFlags.result = map[string]interface{}{
"jsonParam": func() *string { s := "not-json"; return &s }(),
}
command.Flags().Lookup("jsonParam").Changed = true

_, err := multipleFlags.ExtractFlagValueForBodyParam()
if err == nil {
t.Error("expected an error for invalid JSON, got nil")
}
}
8 changes: 5 additions & 3 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ func initRun(runs schema.RunCatalog) {
Run: func(cmd *cobra.Command, args []string) {
pathValues := make([]string, len(pathFlagValues))
queryParams := multipleFlagsForQuery.ExtractFlagValueForQueryParam()
body := multipleFlagsForBody.ExtractFlagValueForBodyParam()
body, err := multipleFlagsForBody.ExtractFlagValueForBodyParam()
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
return
}
for i, v := range pathFlagValues {
pathValues[i] = *v
}

var err error

if len(bodyFlags) == 0 {
body = nil
}
Expand Down
171 changes: 171 additions & 0 deletions pkg/client/console_client_run_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions pkg/client/console_client_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading