2023-03-28 19:12:30 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-10-11 12:51:19 +00:00
|
|
|
package flags
|
2017-07-14 19:45:08 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Ensure implements
|
|
|
|
var _ flag.Value = (*FlagMapValue)(nil)
|
|
|
|
|
|
|
|
// FlagMapValue is a flag implementation used to provide key=value semantics
|
|
|
|
// multiple times.
|
|
|
|
type FlagMapValue map[string]string
|
|
|
|
|
|
|
|
func (h *FlagMapValue) String() string {
|
|
|
|
return fmt.Sprintf("%v", *h)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *FlagMapValue) Set(value string) error {
|
|
|
|
idx := strings.Index(value, "=")
|
|
|
|
if idx == -1 {
|
|
|
|
return fmt.Errorf("Missing \"=\" value in argument: %s", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
key, value := value[0:idx], value[idx+1:]
|
|
|
|
|
|
|
|
if *h == nil {
|
|
|
|
*h = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
headers := *h
|
|
|
|
headers[key] = value
|
|
|
|
*h = headers
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-10-17 21:57:48 +00:00
|
|
|
|
|
|
|
// Merge will overlay this value if it has been set.
|
|
|
|
func (h *FlagMapValue) Merge(onto map[string]string) {
|
|
|
|
if h == nil || onto == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for k, v := range *h {
|
|
|
|
if _, ok := onto[k]; !ok {
|
|
|
|
onto[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|