VictoriaMetrics/lib/flagutil/map.go
Morgan 64e96fccd9
Expose OAuth2 Endpoint Parameters to cli (#5427)
The user may which to control the endpoint parameters for instance to
set the audience when requesting an access token. Exposing the
parameters as a map allows for additional use cases without requiring
modification.
2023-12-20 21:38:13 +02:00

50 lines
1.0 KiB
Go

package flagutil
import (
"flag"
"fmt"
"strings"
)
type MapString map[string]string
// String returns a string representation of the map.
func (m *MapString) String() string {
if m == nil {
return ""
}
return fmt.Sprintf("%v", *m)
}
// Set parses the given value into a map.
func (m *MapString) Set(value string) error {
if *m == nil {
*m = make(map[string]string)
}
for _, pair := range parseArrayValues(value) {
key, value, err := parseMapValue(pair)
if err != nil {
return err
}
(*m)[key] = value
}
return nil
}
func parseMapValue(s string) (string, string, error) {
kv := strings.SplitN(s, ":", 2)
if len(kv) != 2 {
return "", "", fmt.Errorf("invalid map value '%s' values must be 'key:value'", s)
}
return kv[0], kv[1], nil
}
// NewMapString returns a new MapString with the given name and description.
func NewMapString(name, description string) *MapString {
description += fmt.Sprintf("\nSupports multiple flags with the following syntax: -%s=key:value", name)
var m MapString
flag.Var(&m, name, description)
return &m
}