1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
12
"github.com/juju/errors"
13
"github.com/juju/utils"
17
type configFlag struct {
19
attrs map[string]interface{}
22
// Set implements gnuflag.Value.Set.
23
func (f *configFlag) Set(s string) error {
25
return errors.NotValidf("empty string")
27
fields := strings.SplitN(s, "=", 2)
29
f.files = append(f.files, fields[0])
33
if err := yaml.Unmarshal([]byte(fields[1]), &value); err != nil {
34
return errors.Trace(err)
37
f.attrs = make(map[string]interface{})
39
f.attrs[fields[0]] = value
43
// ReadAttrs reads attributes from the specified files, and then overlays
44
// the results with the k=v attributes.
45
func (f *configFlag) ReadAttrs(ctx *cmd.Context) (map[string]interface{}, error) {
46
attrs := make(map[string]interface{})
47
for _, f := range f.files {
48
path, err := utils.NormalizePath(f)
50
return nil, errors.Trace(err)
52
data, err := ioutil.ReadFile(ctx.AbsPath(path))
54
return nil, errors.Trace(err)
56
if err := yaml.Unmarshal(data, &attrs); err != nil {
60
for k, v := range f.attrs {
66
// String implements gnuflag.Value.String.
67
func (f *configFlag) String() string {
68
strs := make([]string, 0, len(f.attrs)+len(f.files))
69
for _, f := range f.files {
70
strs = append(strs, f)
72
for k, v := range f.attrs {
73
strs = append(strs, fmt.Sprintf("%s=%v", k, v))
75
return strings.Join(strs, " ")