Skip to content
This repository has been archived by the owner on Dec 7, 2020. It is now read-only.

Commit

Permalink
[KEYCLOAK-12433] Ingest --tags, --response-headers from CLI options
Browse files Browse the repository at this point in the history
  • Loading branch information
sampsonj committed Dec 14, 2019
1 parent 42b3e3c commit c178911
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 8 deletions.
13 changes: 10 additions & 3 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
51 changes: 51 additions & 0 deletions cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ limitations under the License.
package main

import (
"fmt"
"reflect"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down

0 comments on commit c178911

Please sign in to comment.