launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/cmd/juju/unset.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "errors" 8 9 "launchpad.net/gnuflag" 10 11 "launchpad.net/juju-core/charm" 12 "launchpad.net/juju-core/cmd" 13 "launchpad.net/juju-core/juju" 14 "launchpad.net/juju-core/state/api/params" 15 ) 16 17 // UnsetCommand sets configuration values of a service back 18 // to their default. 19 type UnsetCommand struct { 20 cmd.EnvCommandBase 21 ServiceName string 22 Options []string 23 } 24 25 const unsetDoc = ` 26 Set one or more configuration options for the specified service to their 27 default. See also the set commmand to set one or more configuration options for 28 a specified service. 29 ` 30 31 func (c *UnsetCommand) Info() *cmd.Info { 32 return &cmd.Info{ 33 Name: "unset", 34 Args: "<service> name ...", 35 Purpose: "set service config options back to their default", 36 Doc: unsetDoc, 37 } 38 } 39 40 func (c *UnsetCommand) SetFlags(f *gnuflag.FlagSet) { 41 c.EnvCommandBase.SetFlags(f) 42 } 43 44 func (c *UnsetCommand) Init(args []string) error { 45 if len(args) == 0 { 46 return errors.New("no service name specified") 47 } 48 c.ServiceName = args[0] 49 c.Options = args[1:] 50 if len(c.Options) == 0 { 51 return errors.New("no configuration options specified") 52 } 53 return nil 54 } 55 56 // run1dot16 runs 'juju unset' using a direct DB connection to maintain 57 // compatibility with an API server running 1.16 or older (when ServiceUnset 58 // was not available). This fallback can be removed when we no longer maintain 59 // 1.16 compatibility. 60 // This was copied directly from the code in UnsetCommand.Run in 1.16 61 func (c *UnsetCommand) run1dot16() error { 62 conn, err := juju.NewConnFromName(c.EnvName) 63 if err != nil { 64 return err 65 } 66 defer conn.Close() 67 service, err := conn.State.Service(c.ServiceName) 68 if err != nil { 69 return err 70 } 71 if len(c.Options) > 0 { 72 settings := make(charm.Settings) 73 for _, option := range c.Options { 74 settings[option] = nil 75 } 76 return service.UpdateConfigSettings(settings) 77 } else { 78 return nil 79 } 80 } 81 82 // Run resets the configuration of a service. 83 func (c *UnsetCommand) Run(ctx *cmd.Context) error { 84 apiclient, err := juju.NewAPIClientFromName(c.EnvName) 85 if err != nil { 86 return err 87 } 88 defer apiclient.Close() 89 err = apiclient.ServiceUnset(c.ServiceName, c.Options) 90 if params.IsCodeNotImplemented(err) { 91 logger.Infof("ServiceUnset not supported by the API server, " + 92 "falling back to 1.16 compatibility mode (direct DB access)") 93 err = c.run1dot16() 94 } 95 return err 96 }