hashicorp/hcp

Flagvalue: Support string maps

Closed this issue · 1 comments

With the current flagvalue package, there's no way to define a map of string key/values as flags:

$ hcp waypoint action-config create -header=key=value -header=content-type=json

In the meantime you can easily emulate this:

// Define the flag
{
  Name:         "source-header",
  DisplayValue: "KEY=VALUE",
  Description:  "Headers to send to the URL when obtaining the credential.",
  Value:        flagvalue.SimpleSlice(nil, &opts.SourceURLHeaders),
  Repeatable:   true,
},

// Capture the slice
type Options struct {
  ...
  SourceURLHeaders []string
}

// Parse the flags
cf.Workload.URL.Headers = make(map[string]string, len(opts.SourceURLHeaders))
for _, h := range opts.SourceURLHeaders {
	kv := strings.SplitN(h, "=", 2)
	if len(kv) != 2 {
		return fmt.Errorf("invalid header %q, expected KEY=VALUE", h)
	}

	cf.Workload.URL.Headers[kv[0]] = kv[1]
}