From c17891146c4df13e38e10eb4d1873c5d8153b4a1 Mon Sep 17 00:00:00 2001 From: jsampson Date: Sat, 14 Dec 2019 10:22:27 -0700 Subject: [PATCH] [KEYCLOAK-12433] Ingest --tags, --response-headers from CLI options --- cli.go | 13 ++++++++++--- cli_test.go | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ utils.go | 8 ++++---- utils_test.go | 3 ++- 4 files changed, 67 insertions(+), 8 deletions(-) diff --git a/cli.go b/cli.go index b3c1aa75..827a5c7b 100644 --- a/cli.go +++ b/cli.go @@ -160,7 +160,7 @@ func getCommandLineOptions() []cli.Flag { // parseCLIOptions parses the command line options and constructs a config object func parseCLIOptions(cx *cli.Context, config *Config) (err error) { // step: we can ignore these options in the Config struct - ignoredOptions := []string{"tag-data", "match-claims", "resources", "headers"} + ignoredOptions := []string{"tags", "match-claims", "resources", "headers", "response-headers"} // step: iterate the Config and grab command line options via reflection count := reflect.TypeOf(config).Elem().NumField() for i := 0; i < count; i++ { @@ -190,8 +190,8 @@ func parseCLIOptions(cx *cli.Context, config *Config) (err error) { } } } - if cx.IsSet("tag") { - tags, err := decodeKeyPairs(cx.StringSlice("tag")) + if cx.IsSet("tags") { + tags, err := decodeKeyPairs(cx.StringSlice("tags")) if err != nil { return err } @@ -211,6 +211,13 @@ func parseCLIOptions(cx *cli.Context, config *Config) (err error) { } mergeMaps(config.Headers, headers) } + if cx.IsSet("response-headers") { + responseHeaders, err := decodeKeyPairs(cx.StringSlice("response-headers")) + if err != nil { + return err + } + mergeMaps(config.ResponseHeaders, responseHeaders) + } if cx.IsSet("resources") { for _, x := range cx.StringSlice("resources") { resource, err := newResource().parse(x) diff --git a/cli_test.go b/cli_test.go index a7aade1e..6ebdd441 100644 --- a/cli_test.go +++ b/cli_test.go @@ -16,6 +16,8 @@ limitations under the License. package main import ( + "fmt" + "reflect" "testing" "github.com/stretchr/testify/assert" @@ -33,6 +35,55 @@ func TestGetCLIOptions(t *testing.T) { } } +type cliOption struct { + OptionName string + FieldValue reflect.Value +} + +func TestParseCLIMapOptions(t *testing.T) { + config := newDefaultConfig() + c := cli.NewApp() + c.Flags = getCommandLineOptions() + c.Action = func(cx *cli.Context) error { + ero := parseCLIOptions(cx, config) + assert.NoError(t, ero) + return nil + } + mapOptions := []cliOption{} + command := []string{"test-cmd"} + resultMap := make(map[string]string) + configPropCount := reflect.TypeOf(config).Elem().NumField() + for i := 0; i < configPropCount; i++ { + field := reflect.TypeOf(config).Elem().Field(i) + if field.Type.Kind() == reflect.Map { + name := field.Tag.Get("yaml") + option := cliOption{ + OptionName: name, + FieldValue: reflect.ValueOf(config).Elem().FieldByName(field.Name), + } + mapOptions = append(mapOptions, option) + resultMap[fmt.Sprintf("%s:%s", name, "k1")] = "v1" + resultMap[fmt.Sprintf("%s:%s", name, "k2")] = "v2=testEqualChar" + command = append(command, fmt.Sprintf("--%s=k1=v1", name)) + command = append(command, fmt.Sprintf("--%s=k2=v2=testEqualChar", name)) + } + } + err := c.Run(command) + assert.NoError(t, err) + errFmt := "the parsed %s cli option is not correct" + for i := 0; i < len(mapOptions); i++ { + name := mapOptions[i].OptionName + fieldValue := mapOptions[i].FieldValue + keys := fieldValue.MapKeys() + assert.True(t, len(keys) > 0, "we should have received flags for all map options") + for j := 0; j < len(keys); j++ { + expected := resultMap[fmt.Sprintf("%s:%s", name, keys[j].String())] + actual := fieldValue.MapIndex(keys[j]).String() + assert.Equal(t, expected, actual, fmt.Sprintf(errFmt, name)) + } + } +} + func TestReadOptions(t *testing.T) { c := cli.NewApp() c.Flags = getCommandLineOptions() diff --git a/utils.go b/utils.go index f90147d0..7ed2ee18 100644 --- a/utils.go +++ b/utils.go @@ -212,11 +212,11 @@ func decodeKeyPairs(list []string) (map[string]string, error) { kp := make(map[string]string) for _, x := range list { - items := strings.Split(x, "=") - if len(items) != 2 { - return kp, fmt.Errorf("invalid tag '%s' should be key=pair", x) + splitIdx := strings.Index(x, "=") + if splitIdx < 0 { + return kp, fmt.Errorf("invalid tag '%s', should be key=pair", x) } - kp[items[0]] = items[1] + kp[x[:splitIdx]] = x[splitIdx+1:] } return kp, nil diff --git a/utils_test.go b/utils_test.go index 70aac396..caa6fbc8 100644 --- a/utils_test.go +++ b/utils_test.go @@ -39,10 +39,11 @@ func TestDecodeKeyPairs(t *testing.T) { Ok bool }{ { - List: []string{"a=b", "b=3"}, + List: []string{"a=b", "b=3", "c=d=e"}, KeyPairs: map[string]string{ "a": "b", "b": "3", + "c": "d=e", }, Ok: true, },