github.com/mhilton/juju-juju@v0.0.0-20150901100907-a94dd2c73455/cmd/juju/service/unset.go (about) 1 // Copyright 2012-2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package service 5 6 import ( 7 "errors" 8 9 "github.com/juju/cmd" 10 11 "github.com/juju/juju/cmd/envcmd" 12 "github.com/juju/juju/cmd/juju/block" 13 ) 14 15 // UnsetCommand sets configuration values of a service back 16 // to their default. 17 type UnsetCommand struct { 18 envcmd.EnvCommandBase 19 ServiceName string 20 Options []string 21 api UnsetServiceAPI 22 } 23 24 const unsetDoc = ` 25 Set one or more configuration options for the specified service to their 26 default. See also the set command to set one or more configuration options for 27 a specified service. 28 ` 29 30 func (c *UnsetCommand) Info() *cmd.Info { 31 return &cmd.Info{ 32 Name: "unset", 33 Args: "<service> name ...", 34 Purpose: "set service config options back to their default", 35 Doc: unsetDoc, 36 } 37 } 38 39 func (c *UnsetCommand) Init(args []string) error { 40 if len(args) == 0 { 41 return errors.New("no service name specified") 42 } 43 c.ServiceName = args[0] 44 c.Options = args[1:] 45 if len(c.Options) == 0 { 46 return errors.New("no configuration options specified") 47 } 48 return nil 49 } 50 51 // UnsetServiceAPI defines the methods on the client API 52 // that the service unset command calls. 53 type UnsetServiceAPI interface { 54 Close() error 55 ServiceUnset(service string, options []string) error 56 } 57 58 func (c *UnsetCommand) getAPI() (UnsetServiceAPI, error) { 59 if c.api != nil { 60 return c.api, nil 61 } 62 return c.NewAPIClient() 63 } 64 65 // Run resets the configuration of a service. 66 func (c *UnsetCommand) Run(ctx *cmd.Context) error { 67 apiclient, err := c.getAPI() 68 if err != nil { 69 return err 70 } 71 defer apiclient.Close() 72 return block.ProcessBlockedError(apiclient.ServiceUnset(c.ServiceName, c.Options), block.BlockChange) 73 }