~fwereade/juju-core/unit-remove-depart-scopes

« back to all changes in this revision

Viewing changes to cmd/juju/environment.go

  • Committer: William Reade
  • Date: 2013-11-07 17:30:38 UTC
  • mfrom: (2032.1.6 juju-core)
  • Revision ID: fwereade@gmail.com-20131107173038-d0a72q96dujotc0z
merge parent

Show diffs side-by-side

added added

removed removed

Lines of Context:
54
54
}
55
55
 
56
56
func (c *GetEnvironmentCommand) Run(ctx *cmd.Context) error {
57
 
        conn, err := juju.NewConnFromName(c.EnvName)
58
 
        if err != nil {
59
 
                return err
60
 
        }
61
 
        defer conn.Close()
62
 
 
63
 
        // Get the existing environment config from the state.
64
 
        config, err := conn.State.EnvironConfig()
65
 
        if err != nil {
66
 
                return err
67
 
        }
68
 
        attrs := config.AllAttrs()
69
 
 
70
 
        // If no key specified, write out the whole lot.
71
 
        if c.key == "" {
72
 
                return c.out.Write(ctx, attrs)
73
 
        }
74
 
 
75
 
        value, found := attrs[c.key]
76
 
        if found {
77
 
                return c.out.Write(ctx, value)
78
 
        }
79
 
 
80
 
        return fmt.Errorf("Key %q not found in %q environment.", c.key, config.Name())
 
57
        client, err := juju.NewAPIClientFromName(c.EnvName)
 
58
        if err != nil {
 
59
                return err
 
60
        }
 
61
        defer client.Close()
 
62
 
 
63
        attrs, err := client.EnvironmentGet()
 
64
        if err != nil {
 
65
                return err
 
66
        }
 
67
 
 
68
        if c.key != "" {
 
69
                if value, found := attrs[c.key]; found {
 
70
                        return c.out.Write(ctx, value)
 
71
                }
 
72
                return fmt.Errorf("Key %q not found in %q environment.", c.key, attrs["name"])
 
73
        }
 
74
        // If key is empty, write out the whole lot.
 
75
        return c.out.Write(ctx, attrs)
81
76
}
82
77
 
83
78
type attributes map[string]interface{}
130
125
}
131
126
 
132
127
func (c *SetEnvironmentCommand) Run(ctx *cmd.Context) error {
133
 
        conn, err := juju.NewConnFromName(c.EnvName)
 
128
        client, err := juju.NewAPIClientFromName(c.EnvName)
134
129
        if err != nil {
135
130
                return err
136
131
        }
137
 
        defer conn.Close()
 
132
        defer client.Close()
138
133
 
139
 
        // Here is the magic around setting the attributes:
140
 
        // TODO(thumper): get this magic under test somewhere, and update other call-sites to use it.
141
 
        // Get the existing environment config from the state.
142
 
        oldConfig, err := conn.State.EnvironConfig()
143
 
        if err != nil {
144
 
                return err
145
 
        }
146
 
        // Apply the attributes specified for the command to the state config.
147
 
        newConfig, err := oldConfig.Apply(c.values)
148
 
        if err != nil {
149
 
                return err
150
 
        }
151
 
        // Now validate this new config against the existing config via the provider.
152
 
        provider := conn.Environ.Provider()
153
 
        newProviderConfig, err := provider.Validate(newConfig, oldConfig)
154
 
        if err != nil {
155
 
                return err
156
 
        }
157
 
        // Now try to apply the new validated config.
158
 
        return conn.State.SetEnvironConfig(newProviderConfig)
 
134
        return client.EnvironmentSet(c.values)
159
135
}