-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
199 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/conduktor/ctl/schema" | ||
"github.com/conduktor/ctl/utils" | ||
"github.com/spf13/cobra" | ||
"strconv" | ||
) | ||
|
||
type MultipleFlags struct { | ||
result map[string]interface{} | ||
flagParams map[string]schema.FlagParameterOption | ||
command *cobra.Command | ||
} | ||
|
||
func NewMultipleFlags(command *cobra.Command, flagParams map[string]schema.FlagParameterOption) *MultipleFlags { | ||
result := make(map[string]interface{}, len(flagParams)) | ||
usage := "" | ||
for key, flag := range flagParams { | ||
var isFlagSet bool | ||
if flag.Type == "string" { | ||
isFlagSet = true | ||
defaultValue := "" | ||
result[key] = command.Flags().String(flag.FlagName, defaultValue, usage) | ||
} else if flag.Type == "boolean" { | ||
isFlagSet = true | ||
defaultValue := false | ||
result[key] = command.Flags().Bool(flag.FlagName, defaultValue, usage) | ||
} else if flag.Type == "integer" { | ||
isFlagSet = true | ||
defaultValue := 0 | ||
result[key] = command.Flags().Int(flag.FlagName, defaultValue, usage) | ||
} else if utils.CdkDebug() { | ||
println("Unknown flag type: " + flag.Type) | ||
} | ||
if isFlagSet && flag.Required { | ||
command.MarkFlagRequired(flag.FlagName) | ||
} | ||
} | ||
|
||
return &MultipleFlags{ | ||
result, | ||
flagParams, | ||
command, | ||
} | ||
} | ||
|
||
func (m *MultipleFlags) ExtractFlagValueForBodyParam() map[string]interface{} { | ||
bodyParams := make(map[string]interface{}) | ||
for key, value := range m.result { | ||
if value != nil && m.flagSetByUser(key) { | ||
bodyParams[key] = value | ||
} | ||
} | ||
return bodyParams | ||
} | ||
|
||
func (m *MultipleFlags) ExtractFlagValueForQueryParam() map[string]string { | ||
queryParams := make(map[string]string) | ||
for key, value := range m.result { | ||
if value != nil && m.flagSetByUser(key) { | ||
str, strOk := value.(*string) | ||
boolValue, boolOk := value.(*bool) | ||
intValue, intOk := value.(*int) | ||
|
||
if strOk { | ||
queryParams[key] = *str | ||
} else if boolOk { | ||
queryParams[key] = strconv.FormatBool(*boolValue) | ||
} else if intOk { | ||
queryParams[key] = strconv.Itoa(*intValue) | ||
} else { | ||
panic("Unknown query flag type") | ||
} | ||
} | ||
} | ||
return queryParams | ||
} | ||
|
||
func (m *MultipleFlags) flagSetByUser(flagKey string) bool { | ||
flag, present := m.flagParams[flagKey] | ||
if !present { | ||
panic("Flag " + flagKey + " not found in flagParams") | ||
} | ||
return m.command.Flags().Changed(flag.FlagName) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package cmd | ||
|
||
import ( | ||
"github.com/conduktor/ctl/schema" | ||
"github.com/davecgh/go-spew/spew" | ||
"github.com/spf13/cobra" | ||
"reflect" | ||
"strconv" | ||
"testing" | ||
) | ||
|
||
func TestExtractFlagValueForQueryParam(t *testing.T) { | ||
command := &cobra.Command{} | ||
multipleFlags := NewMultipleFlags(command, map[string]schema.FlagParameterOption{ | ||
"stringParam": {FlagName: "stringParam", Type: "string"}, | ||
"notSetString": {FlagName: "notSetStrign", Type: "string"}, | ||
"emptyString": {FlagName: "emptyString", Type: "string"}, | ||
"boolParam": {FlagName: "boolParam", Type: "boolean"}, | ||
"boolParamFalse": {FlagName: "boolParamFalse", Type: "boolean"}, | ||
"notSetBoolean": {FlagName: "notSetBoolean", Type: "boolean"}, | ||
"intParam": {FlagName: "intParam", Type: "integer"}, | ||
"notSetInt": {FlagName: "notSetInt", Type: "integer"}, | ||
"zeroParam": {FlagName: "zeroParam", Type: "integer"}, | ||
}) | ||
multipleFlags.result = map[string]interface{}{ | ||
"stringParam": func() *string { s := "test"; return &s }(), | ||
"notSetString": func() *string { s := ""; return &s }(), | ||
"emptyString": func() *string { s := ""; return &s }(), | ||
"boolParam": func() *bool { b := true; return &b }(), | ||
"boolParamFalse": func() *bool { b := false; return &b }(), | ||
"notSetBoolean": func() *bool { b := false; return &b }(), | ||
"intParam": func() *int { i := 123; return &i }(), | ||
"notSetInt": func() *int { i := 0; return &i }(), | ||
"zeroParam": func() *int { i := 0; return &i }(), | ||
} | ||
|
||
expected := map[string]string{ | ||
"stringParam": "test", | ||
"boolParam": strconv.FormatBool(true), | ||
"intParam": strconv.Itoa(123), | ||
"zeroParam": strconv.Itoa(0), | ||
"emptyString": "", | ||
"boolParamFalse": strconv.FormatBool(false), | ||
} | ||
|
||
command.Flags().Lookup("stringParam").Changed = true | ||
command.Flags().Lookup("boolParam").Changed = true | ||
command.Flags().Lookup("boolParamFalse").Changed = true | ||
command.Flags().Lookup("intParam").Changed = true | ||
command.Flags().Lookup("zeroParam").Changed = true | ||
command.Flags().Lookup("emptyString").Changed = true | ||
result := multipleFlags.ExtractFlagValueForQueryParam() | ||
|
||
if !reflect.DeepEqual(result, expected) { | ||
t.Error(spew.Printf("got %v, want %v", result, expected)) | ||
} | ||
} | ||
|
||
func TestExtractFlagValueForBodyParam(t *testing.T) { | ||
command := &cobra.Command{} | ||
multipleFlags := NewMultipleFlags(command, map[string]schema.FlagParameterOption{ | ||
"stringParam": {FlagName: "stringParam", Type: "string"}, | ||
"notSetString": {FlagName: "notSetStrign", Type: "string"}, | ||
"emptyString": {FlagName: "emptyString", Type: "string"}, | ||
"boolParam": {FlagName: "boolParam", Type: "boolean"}, | ||
"boolParamFalse": {FlagName: "boolParamFalse", Type: "boolean"}, | ||
"notSetBoolean": {FlagName: "notSetBoolean", Type: "boolean"}, | ||
"intParam": {FlagName: "intParam", Type: "integer"}, | ||
"notSetInt": {FlagName: "notSetInt", Type: "integer"}, | ||
"zeroParam": {FlagName: "zeroParam", Type: "integer"}, | ||
}) | ||
multipleFlags.result = map[string]interface{}{ | ||
"stringParam": func() *string { s := "test"; return &s }(), | ||
"notSetString": func() *string { s := ""; return &s }(), | ||
"emptyString": func() *string { s := ""; return &s }(), | ||
"boolParam": func() *bool { b := true; return &b }(), | ||
"boolParamFalse": func() *bool { b := false; return &b }(), | ||
"notSetBoolean": func() *bool { b := false; return &b }(), | ||
"intParam": func() *int { i := 123; return &i }(), | ||
"notSetInt": func() *int { i := 0; return &i }(), | ||
"zeroParam": func() *int { i := 0; return &i }(), | ||
} | ||
|
||
expected := map[string]interface{}{ | ||
"stringParam": func() *string { s := "test"; return &s }(), | ||
"emptyString": func() *string { s := ""; return &s }(), | ||
"boolParam": func() *bool { b := true; return &b }(), | ||
"boolParamFalse": func() *bool { b := false; return &b }(), | ||
"intParam": func() *int { i := 123; return &i }(), | ||
"zeroParam": func() *int { i := 0; return &i }(), | ||
} | ||
|
||
command.Flags().Lookup("stringParam").Changed = true | ||
command.Flags().Lookup("boolParam").Changed = true | ||
command.Flags().Lookup("boolParamFalse").Changed = true | ||
command.Flags().Lookup("intParam").Changed = true | ||
command.Flags().Lookup("zeroParam").Changed = true | ||
command.Flags().Lookup("emptyString").Changed = true | ||
result := multipleFlags.ExtractFlagValueForBodyParam() | ||
|
||
if !reflect.DeepEqual(result, expected) { | ||
t.Error(spew.Printf("got %v, want %v", result, expected)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.