~hduran-8/juju-core/trunk

« back to all changes in this revision

Viewing changes to cmd/juju/constraints.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:
70
70
}
71
71
 
72
72
func (c *GetConstraintsCommand) SetFlags(f *gnuflag.FlagSet) {
73
 
        c.EnvCommandBase.SetFlags(f)
74
73
        c.out.AddFlags(f, "constraints", map[string]cmd.Formatter{
75
74
                "constraints": formatConstraints,
76
75
                "yaml":        cmd.FormatYaml,
79
78
}
80
79
 
81
80
func (c *GetConstraintsCommand) Init(args []string) error {
82
 
        if err := c.EnvCommandBase.EnsureEnvName(); err != nil {
83
 
                return err
84
 
        }
85
81
        if len(args) > 0 {
86
82
                if !names.IsService(args[0]) {
87
83
                        return fmt.Errorf("invalid service name %q", args[0])
127
123
}
128
124
 
129
125
func (c *SetConstraintsCommand) SetFlags(f *gnuflag.FlagSet) {
130
 
        c.EnvCommandBase.SetFlags(f)
131
126
        f.StringVar(&c.ServiceName, "s", "", "set service constraints")
132
127
        f.StringVar(&c.ServiceName, "service", "", "")
133
128
}
134
129
 
135
130
func (c *SetConstraintsCommand) Init(args []string) (err error) {
136
 
        if err := c.EnvCommandBase.EnsureEnvName(); err != nil {
137
 
                return err
138
 
        }
139
131
        if c.ServiceName != "" && !names.IsService(c.ServiceName) {
140
132
                return fmt.Errorf("invalid service name %q", c.ServiceName)
141
133
        }