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

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/cmd/juju/model/configcommand.go

  • Committer: Nicholas Skaggs
  • Date: 2016-09-30 14:39:30 UTC
  • mfrom: (1.8.1)
  • Revision ID: nicholas.skaggs@canonical.com-20160930143930-vwwhrefh6ftckccy
import upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
57
57
        modelcmd.ModelCommandBase
58
58
        out cmd.Output
59
59
 
60
 
        action func(*cmd.Context) error // The action which we want to handle, set in cmd.Init.
 
60
        action func(configCommandAPI, *cmd.Context) error // The action which we want to handle, set in cmd.Init.
61
61
        keys   []string
62
62
        reset  bool // Flag denoting whether we are resetting the keys provided.
63
63
        values attributes
154
154
 
155
155
// getAPI returns the API. This allows passing in a test configCommandAPI
156
156
// implementation.
157
 
func (c *configCommand) getAPI() error {
 
157
func (c *configCommand) getAPI() (configCommandAPI, error) {
158
158
        if c.api != nil {
159
 
 
160
 
                return nil
 
159
                return c.api, nil
161
160
        }
162
161
        api, err := c.NewAPIRoot()
163
162
        if err != nil {
164
 
                return errors.Annotate(err, "opening API connection")
 
163
                return nil, errors.Annotate(err, "opening API connection")
165
164
        }
166
 
        c.api = modelconfig.NewClient(api)
167
 
        return nil
 
165
        client := modelconfig.NewClient(api)
 
166
        return client, nil
168
167
}
169
168
 
170
169
// Run implements the meaty part of the cmd.Command interface.
171
170
func (c *configCommand) Run(ctx *cmd.Context) error {
172
 
        err := c.getAPI()
 
171
        client, err := c.getAPI()
173
172
        if err != nil {
174
173
                return err
175
174
        }
176
 
        defer c.api.Close()
 
175
        defer client.Close()
177
176
 
178
 
        return c.action(ctx)
 
177
        return c.action(client, ctx)
179
178
}
180
179
 
181
180
// reset unsets the keys provided to the command.
182
 
func (c *configCommand) resetConfig(ctx *cmd.Context) error {
 
181
func (c *configCommand) resetConfig(client configCommandAPI, ctx *cmd.Context) error {
183
182
        // ctx unused in this method
184
183
 
185
184
        // extra call to the API to retrieve env config
186
 
        envAttrs, err := c.api.ModelGet()
 
185
        envAttrs, err := client.ModelGet()
187
186
        if err != nil {
188
187
                return err
189
188
        }
201
200
                }
202
201
 
203
202
        }
204
 
        return block.ProcessBlockedError(c.api.ModelUnset(c.keys...), block.BlockChange)
 
203
        return block.ProcessBlockedError(client.ModelUnset(c.keys...), block.BlockChange)
205
204
}
206
205
 
207
206
// set sets the provided key/value pairs on the model.
208
 
func (c *configCommand) setConfig(ctx *cmd.Context) error {
 
207
func (c *configCommand) setConfig(client configCommandAPI, ctx *cmd.Context) error {
209
208
        // ctx unused in this method.
210
 
        envAttrs, err := c.api.ModelGet()
 
209
        envAttrs, err := client.ModelGet()
211
210
        if err != nil {
212
211
                return err
213
212
        }
217
216
                }
218
217
 
219
218
        }
220
 
        return block.ProcessBlockedError(c.api.ModelSet(c.values), block.BlockChange)
 
219
        return block.ProcessBlockedError(client.ModelSet(c.values), block.BlockChange)
221
220
}
222
221
 
223
222
// get writes the value of a single key or the full output for the model to the cmd.Context.
224
 
func (c *configCommand) getConfig(ctx *cmd.Context) error {
225
 
        attrs, err := c.api.ModelGetWithMetadata()
 
223
func (c *configCommand) getConfig(client configCommandAPI, ctx *cmd.Context) error {
 
224
        attrs, err := client.ModelGetWithMetadata()
226
225
        if err != nil {
227
226
                return err
228
227
        }