diff --git a/cmd/agent.go b/cmd/agent.go index e75ac8cb1..d2a7b4bfa 100644 --- a/cmd/agent.go +++ b/cmd/agent.go @@ -4,6 +4,7 @@ import ( "fmt" "os" "os/signal" + "strings" "syscall" "time" @@ -138,3 +139,17 @@ func handleReload() { } //Config reloading will also reload Notification settings } + +// UnmarshalTags is a utility function which takes a slice of strings in +// key=value format and returns them as a tag mapping. +func UnmarshalTags(tags []string) (map[string]string, error) { + result := make(map[string]string) + for _, tag := range tags { + parts := strings.SplitN(tag, "=", 2) + if len(parts) != 2 || len(parts[0]) == 0 { + return nil, fmt.Errorf("Invalid tag: '%s'", tag) + } + result[parts[0]] = parts[1] + } + return result, nil +} diff --git a/cmd/agent_test.go b/cmd/agent_test.go index ccaad54bd..0c98b902b 100644 --- a/cmd/agent_test.go +++ b/cmd/agent_test.go @@ -23,13 +23,13 @@ func getEnvWithDefault() string { return ea } -func Test_unmarshalTags(t *testing.T) { +func TestUnmarshalTags(t *testing.T) { tagPairs := []string{ "tag1=val1", "tag2=val2", } - tags, err := unmarshalTags(tagPairs) + tags, err := UnmarshalTags(tagPairs) if err != nil { t.Fatalf("err: %s", err)