feat(cli): add/set/remove worker tags (#2266)

* adds CLI commands for add/set/removing worker tags, and respective tests
* adds StringSliceMapVar Flag to convert a string from the CLI to a map[string][]string value
This commit is contained in:
Haotian authored and GitHub committed 2022-08-26 13:37:26 -07:00
1 parent 547e7a5970
commit 17ddc301a8
12 files changed
+825 -4

No files matched your search

+1
View File
@@ -113,6 +113,7 @@ type Command struct {
FlagVersion int
FlagRecursive bool
FlagFilter string
FlagTags map[string][]string
// Attribute values
FlagAttributes string
+115
View File
@@ -716,6 +716,121 @@ func mapToKV(m map[string]string) string {
return strings.Join(list, ",")
}
// StringSliceMapVar maps a key string to a slice of string values.
// This is useful for cases such as modifying Worker tags, which can have
// multiple values associated per key.
// Setting NullCheck to return true enables the string input value "null"
// to be accepted without throwing an error. This is useful for add/set/remove
// patterns where we want to enable "set null".
type StringSliceMapVar struct {
Name string
Aliases []string
Usage string
Default map[string][]string
Hidden bool
Target *map[string][]string
NullCheck func() bool
Completion complete.Predictor
}
func (f *FlagSet) StringSliceMapVar(i *StringSliceMapVar) {
f.VarFlag(&VarFlag{
Name: i.Name,
Aliases: i.Aliases,
Usage: i.Usage,
Value: newStringSliceMapValue(i.Default, i.Target, i.NullCheck, i.Hidden),
Completion: i.Completion,
})
}
type stringSliceMapValue struct {
hidden bool
target *map[string][]string
nullCheck func() bool
isNull bool
}
func newStringSliceMapValue(def map[string][]string, target *map[string][]string, nullCheck func() bool, hidden bool) *stringSliceMapValue {
*target = def
return &stringSliceMapValue{
hidden: hidden,
target: target,
nullCheck: nullCheck,
isNull: false,
}
}
func (s *stringSliceMapValue) Set(val string) error {
kv := strings.TrimSpace(val)
// Don't enable this behavior if nullCheck is not defined.
if s.nullCheck != nil {
if kv == "null" {
if !s.nullCheck() {
return fmt.Errorf(`"null" is not an allowed value`)
}
// Ensure "null" cannot be entered as a consecutive entry in a chain of valid entries
if s.isNull == true || len(*s.target) > 0 {
return fmt.Errorf(`"null" cannot be combined with other values`)
}
// Set target to nil and return
if kv == "null" {
*s.target = map[string][]string{"null": nil}
s.isNull = true
return nil
}
} else if s.isNull == true {
// Ensure consecutive "non-null" entries cannot be entered after a "null"
return fmt.Errorf(`"null" cannot be combined with other values`)
}
}
// Return a better error message if "null" is not enabled
if kv == "null" {
return fmt.Errorf(`"null" is not an allowed value`)
}
if *s.target == nil {
*s.target = make(map[string][]string)
}
split := strings.SplitN(kv, "=", 2)
if len(split) != 2 {
return fmt.Errorf("missing = in KV pair: %q", val)
}
// trim space, check proto, and assign to map
key := strings.TrimSpace(split[0])
if !protoIdentifierRegex.Match([]byte(key)) {
return fmt.Errorf("key %q is invalid", key)
}
vals := strings.Split(split[1], ",")
for i, v := range vals {
vals[i] = strings.TrimSpace(v)
if !protoIdentifierRegex.Match([]byte(vals[i])) {
return fmt.Errorf("value %q is invalid", vals[i])
}
}
for _, v := range vals {
(*s.target)[key] = append((*s.target)[key], v)
}
return nil
}
func sliceMapToKV(m map[string][]string) string {
list := make([]string, 0, len(m))
for k, vals := range m {
vv := strings.Join(vals, ",")
list = append(list, k+"="+vv)
}
return strings.Join(list, ", ")
}
func (c *stringSliceMapValue) Get() interface{} { return *c.target }
func (c *stringSliceMapValue) String() string { return sliceMapToKV(*c.target) }
func (c *stringSliceMapValue) Example() string { return "key1=val-a, key2=val-b,val-c" }
func (c *stringSliceMapValue) Hidden() bool { return c.hidden }
// -- VarFlag
type VarFlag struct {
Name string
+151
View File
@@ -0,0 +1,151 @@
package base
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestFlagSet_StringSliceMapVar(t *testing.T) {
t.Parallel()
tests := []struct {
name string
setString string
wantErrContains string
wantTarget map[string][]string
}{
{
name: "key-value",
setString: "key=value",
wantTarget: map[string][]string{"key": {"value"}},
},
{
name: "many-values",
setString: "key=v1,v2,v3,v4,v5",
wantTarget: map[string][]string{"key": {"v1", "v2", "v3", "v4", "v5"}},
},
{
// this case passes here and errors at worker client
name: "duplicate-values",
setString: "k1=v1,v1",
wantTarget: map[string][]string{"k1": {"v1", "v1"}},
},
{
name: "no-deliminator",
setString: "key",
wantErrContains: "missing = in KV pair",
},
{
name: "multiple-keys",
setString: "k1=v1, k2=v2",
wantErrContains: "value \"k2=v2\" is invalid",
},
{
name: "too-many-commas",
setString: "k1=v1,,v3?,,,,,v8",
wantErrContains: "value \"\" is invalid",
},
{
name: "illegal-characters-key",
setString: "!@#$=%^&*()_",
wantErrContains: "key \"!@#$\" is invalid",
},
{
name: "illegal-characters-value",
setString: "k=%^&*()_",
wantErrContains: "value \"%^&*()_\" is invalid",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
target := make(map[string][]string, 1)
sliceMapVal := &stringSliceMapValue{
hidden: false,
target: &target,
}
err := sliceMapVal.Set(tt.setString)
if tt.wantErrContains != "" {
assert.Error(err)
assert.Contains(err.Error(), tt.wantErrContains)
return
}
require.NoError(err)
assert.Equal(tt.wantTarget, *sliceMapVal.target)
})
}
}
func TestFlagSet_StringSliceMapVar_NullCheck(t *testing.T) {
t.Parallel()
tests := []struct {
name string
setStrings []string
wantErrContains string
wantTarget map[string][]string
}{
{
name: "null",
setStrings: []string{"null"},
wantTarget: map[string][]string{"null": nil},
// this target is then converted to a nil value in workerscmd/func.go
},
{
name: "glass-half-null",
setStrings: []string{"milk=half", "null"},
wantErrContains: `"null" cannot be combined with other values`,
},
{
name: "invalid-entry-before",
setStrings: []string{"the-milk-is-bad", "null"},
wantErrContains: `missing = in KV pair: "the-milk-is-bad"`,
},
{
name: "invalid-entry-after",
setStrings: []string{"null", "the-milk-is-still-bad"},
wantErrContains: `"null" cannot be combined with other values`,
},
{
name: "no-error",
setStrings: []string{"k1=v1,v2,v3", "k2=v4,v5,v6"},
wantTarget: map[string][]string{"k1": {"v1", "v2", "v3"}, "k2": {"v4", "v5", "v6"}},
},
{
name: "too-null",
setStrings: []string{"null", "null", "null"},
wantErrContains: `"null" cannot be combined with other values`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
target := make(map[string][]string, 1)
sliceMapVal := &stringSliceMapValue{
hidden: false,
target: &target,
nullCheck: func() bool { return true },
}
var err error
for _, s := range tt.setStrings {
err = sliceMapVal.Set(s)
if err != nil {
break
}
}
if tt.wantErrContains != "" {
assert.Error(err)
assert.Contains(err.Error(), tt.wantErrContains)
return
}
require.NoError(err)
assert.Equal(tt.wantTarget, *sliceMapVal.target)
})
}
}