~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/commands/flags.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Copyright 2016 Canonical Ltd.
2
 
// Licensed under the AGPLv3, see LICENCE file for details.
3
 
 
4
 
package commands
5
 
 
6
 
import (
7
 
        "fmt"
8
 
        "io/ioutil"
9
 
        "strings"
10
 
 
11
 
        "github.com/juju/cmd"
12
 
        "github.com/juju/errors"
13
 
        "github.com/juju/utils"
14
 
        "gopkg.in/yaml.v2"
15
 
)
16
 
 
17
 
type configFlag struct {
18
 
        files []string
19
 
        attrs map[string]interface{}
20
 
}
21
 
 
22
 
// Set implements gnuflag.Value.Set.
23
 
func (f *configFlag) Set(s string) error {
24
 
        if s == "" {
25
 
                return errors.NotValidf("empty string")
26
 
        }
27
 
        fields := strings.SplitN(s, "=", 2)
28
 
        if len(fields) == 1 {
29
 
                f.files = append(f.files, fields[0])
30
 
                return nil
31
 
        }
32
 
        var value interface{}
33
 
        if err := yaml.Unmarshal([]byte(fields[1]), &value); err != nil {
34
 
                return errors.Trace(err)
35
 
        }
36
 
        if f.attrs == nil {
37
 
                f.attrs = make(map[string]interface{})
38
 
        }
39
 
        f.attrs[fields[0]] = value
40
 
        return nil
41
 
}
42
 
 
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)
49
 
                if err != nil {
50
 
                        return nil, errors.Trace(err)
51
 
                }
52
 
                data, err := ioutil.ReadFile(ctx.AbsPath(path))
53
 
                if err != nil {
54
 
                        return nil, errors.Trace(err)
55
 
                }
56
 
                if err := yaml.Unmarshal(data, &attrs); err != nil {
57
 
                        return nil, err
58
 
                }
59
 
        }
60
 
        for k, v := range f.attrs {
61
 
                attrs[k] = v
62
 
        }
63
 
        return attrs, nil
64
 
}
65
 
 
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)
71
 
        }
72
 
        for k, v := range f.attrs {
73
 
                strs = append(strs, fmt.Sprintf("%s=%v", k, v))
74
 
        }
75
 
        return strings.Join(strs, " ")
76
 
}