~hduran-8/juju-core/trunk

« back to all changes in this revision

Viewing changes to cmd/juju/environment.go

  • Committer: Tarmac
  • Author(s): Andrew Wilkins
  • Date: 2014-05-13 01:48:02 UTC
  • mfrom: (2713.1.5 envcmd-inversion)
  • Revision ID: tarmac-20140513014802-d1n958os00gw27zf
[r=axwalk] Invert envcmd relationship

Previously we embedded EnvCommandBase in all commands
requring an environment, and EnvCommandBase included
everything (SetFlags, and Init). The problem with this
is that it was easy to miss initialisation of the
EnvCommandBase type (this happened a few times), which
leads to bad things happening like sync-tools destroying
environments.

I have inverted the relationship so that we now have
envcmd.EnvironCommand, an interface that extends Command
with a SetEnvName method, and EnvCommandBase, which
implements EnvironCommand. A new method, envcmd.Wrap takes
an EnvironCommand and creates a Command that calls
SetEnvName prior to the wrapped method's Init method. If
the environment name cannot be determined, the wrapping
command will error out early.

https://codereview.appspot.com/94350045/

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
}
46
46
 
47
47
func (c *GetEnvironmentCommand) SetFlags(f *gnuflag.FlagSet) {
48
 
        c.EnvCommandBase.SetFlags(f)
49
48
        c.out.AddFlags(f, "smart", cmd.DefaultFormatters)
50
49
}
51
50
 
52
51
func (c *GetEnvironmentCommand) Init(args []string) (err error) {
53
 
        if err := c.EnvCommandBase.EnsureEnvName(); err != nil {
54
 
                return err
55
 
        }
56
52
        c.key, err = cmd.ZeroOrOneArgs(args)
57
53
        return
58
54
}
102
98
        }
103
99
}
104
100
 
105
 
// SetFlags handled entirely by envcmd.EnvCommandBase
106
 
 
107
101
func (c *SetEnvironmentCommand) Init(args []string) (err error) {
108
 
        if err := c.EnsureEnvName(); err != nil {
109
 
                return err
110
 
        }
111
102
        if len(args) == 0 {
112
103
                return fmt.Errorf("No key, value pairs specified")
113
104
        }
166
157
}
167
158
 
168
159
func (c *UnsetEnvironmentCommand) Init(args []string) (err error) {
169
 
        if err := c.EnsureEnvName(); err != nil {
170
 
                return err
171
 
        }
172
160
        if len(args) == 0 {
173
161
                return fmt.Errorf("No keys specified")
174
162
        }