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

Ingest --tags, --response-headers from CLI options #497

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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") {
sampsonj marked this conversation as resolved.
Show resolved Hide resolved
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") {
sampsonj marked this conversation as resolved.
Show resolved Hide resolved
responseHeaders, err := decodeKeyPairs(cx.StringSlice("response-headers"))
if err != nil {
return err
}
mergeMaps(config.ResponseHeaders, responseHeaders)
sampsonj marked this conversation as resolved.
Show resolved Hide resolved
}
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
sampsonj marked this conversation as resolved.
Show resolved Hide resolved
}
mapOptions := []cliOption{}
command := []string{"test-cmd"}
resultMap := make(map[string]string)
configPropCount := reflect.TypeOf(config).Elem().NumField()
for i := 0; i < configPropCount; i++ {
sampsonj marked this conversation as resolved.
Show resolved Hide resolved
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))
sampsonj marked this conversation as resolved.
Show resolved Hide resolved
command = append(command, fmt.Sprintf("--%s=k2=v2=testEqualChar", name))
}
}
err := c.Run(command)
sampsonj marked this conversation as resolved.
Show resolved Hide resolved
assert.NoError(t, err)
errFmt := "the parsed %s cli option is not correct"
sampsonj marked this conversation as resolved.
Show resolved Hide resolved
for i := 0; i < len(mapOptions); i++ {
sampsonj marked this conversation as resolved.
Show resolved Hide resolved
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++ {
sampsonj marked this conversation as resolved.
Show resolved Hide resolved
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:]
sampsonj marked this conversation as resolved.
Show resolved Hide resolved
}

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