github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/charmcmd/sub.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package charmcmd 5 6 import ( 7 "io" 8 9 "github.com/juju/cmd" 10 "github.com/juju/errors" 11 12 "github.com/juju/juju/charmstore" 13 ) 14 15 var registeredSubCommands []func(CharmstoreSpec) cmd.Command 16 17 // RegisterSubCommand adds the provided func to the set of those that 18 // will be called when the juju command runs. Each returned command will 19 // be registered with the identified "juju" sub-supercommand. 20 func RegisterSubCommand(newCommand func(CharmstoreSpec) cmd.Command) { 21 registeredSubCommands = append(registeredSubCommands, newCommand) 22 } 23 24 // NewCommandBase returns a new CommandBase. 25 func NewCommandBase(spec CharmstoreSpec) *CommandBase { 26 return &CommandBase{ 27 spec: newCharmstoreSpec(), 28 } 29 } 30 31 // CommandBase is the type that should be embedded in "juju charm" 32 // sub-commands. 33 type CommandBase struct { 34 // TODO(ericsnow) This should be a modelcmd.ModelCommandBase. 35 cmd.CommandBase 36 spec CharmstoreSpec 37 } 38 39 // Connect implements CommandBase. 40 func (c *CommandBase) Connect(ctx *cmd.Context) (charmstore.Client, io.Closer, error) { 41 if c.spec == nil { 42 return charmstore.Client{}, nil, errors.Errorf("missing charm store spec") 43 } 44 client, closer, err := c.spec.Connect(ctx) 45 if err != nil { 46 return charmstore.Client{}, nil, errors.Trace(err) 47 } 48 return client, closer, nil 49 }