github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/cmd/juju/commands/helptool.go (about) 1 // Copyright 2013, 2014 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package commands 5 6 import ( 7 "fmt" 8 "time" 9 10 "github.com/juju/cmd" 11 "github.com/juju/errors" 12 "gopkg.in/juju/charm.v6-unstable" 13 "launchpad.net/gnuflag" 14 15 "github.com/juju/juju/network" 16 "github.com/juju/juju/storage" 17 "github.com/juju/juju/worker/uniter/runner/jujuc" 18 ) 19 20 // dummyHookContext implements jujuc.Context, 21 // as expected by jujuc.NewCommand. 22 type dummyHookContext struct{ jujuc.Context } 23 24 func (dummyHookContext) AddMetrics(_, _ string, _ time.Time) error { 25 return nil 26 } 27 func (dummyHookContext) UnitName() string { 28 return "" 29 } 30 func (dummyHookContext) PublicAddress() (string, error) { 31 return "", errors.NotFoundf("PublicAddress") 32 } 33 func (dummyHookContext) PrivateAddress() (string, error) { 34 return "", errors.NotFoundf("PrivateAddress") 35 } 36 func (dummyHookContext) AvailabilityZone() (string, error) { 37 return "", errors.NotFoundf("AvailabilityZone") 38 } 39 func (dummyHookContext) OpenPort(protocol string, port int) error { 40 return nil 41 } 42 func (dummyHookContext) ClosePort(protocol string, port int) error { 43 return nil 44 } 45 func (dummyHookContext) OpenedPorts() []network.PortRange { 46 return nil 47 } 48 func (dummyHookContext) ConfigSettings() (charm.Settings, error) { 49 return charm.NewConfig().DefaultSettings(), nil 50 } 51 func (dummyHookContext) HookRelation() (jujuc.ContextRelation, error) { 52 return nil, errors.NotFoundf("HookRelation") 53 } 54 func (dummyHookContext) RemoteUnitName() (string, error) { 55 return "", errors.NotFoundf("RemoteUnitName") 56 } 57 func (dummyHookContext) Relation(id int) (jujuc.ContextRelation, error) { 58 return nil, errors.NotFoundf("Relation") 59 } 60 func (dummyHookContext) RelationIds() ([]int, error) { 61 return []int{}, errors.NotFoundf("RelationIds") 62 } 63 64 func (dummyHookContext) RequestReboot(prio jujuc.RebootPriority) error { 65 return nil 66 } 67 68 func (dummyHookContext) HookStorageInstance() (*storage.StorageInstance, error) { 69 return nil, errors.NotFoundf("HookStorageInstance") 70 } 71 72 func (dummyHookContext) HookStorage() (jujuc.ContextStorageAttachment, error) { 73 return nil, errors.NotFoundf("HookStorage") 74 } 75 76 func (dummyHookContext) StorageInstance(id string) (*storage.StorageInstance, error) { 77 return nil, errors.NotFoundf("StorageInstance") 78 } 79 80 func (dummyHookContext) UnitStatus() (*jujuc.StatusInfo, error) { 81 return &jujuc.StatusInfo{}, nil 82 } 83 84 func (dummyHookContext) SetStatus(jujuc.StatusInfo) error { 85 return nil 86 } 87 88 func newHelpToolCommand() cmd.Command { 89 return &helpToolCommand{} 90 } 91 92 type helpToolCommand struct { 93 cmd.CommandBase 94 tool string 95 } 96 97 func (t *helpToolCommand) Info() *cmd.Info { 98 return &cmd.Info{ 99 Name: "help-tool", 100 Args: "[tool]", 101 Purpose: "show help on a juju charm tool", 102 } 103 } 104 105 func (t *helpToolCommand) Init(args []string) error { 106 tool, err := cmd.ZeroOrOneArgs(args) 107 if err == nil { 108 t.tool = tool 109 } 110 return err 111 } 112 113 func (c *helpToolCommand) Run(ctx *cmd.Context) error { 114 var hookctx dummyHookContext 115 if c.tool == "" { 116 // Ripped from SuperCommand. We could Run() a SuperCommand 117 // with "help commands", but then the implicit "help" command 118 // shows up. 119 names := jujuc.CommandNames() 120 cmds := make([]cmd.Command, 0, len(names)) 121 longest := 0 122 for _, name := range names { 123 if c, err := jujuc.NewCommand(hookctx, name); err == nil { 124 if len(name) > longest { 125 longest = len(name) 126 } 127 cmds = append(cmds, c) 128 } 129 } 130 for _, c := range cmds { 131 info := c.Info() 132 fmt.Fprintf(ctx.Stdout, "%-*s %s\n", longest, info.Name, info.Purpose) 133 } 134 } else { 135 c, err := jujuc.NewCommand(hookctx, c.tool) 136 if err != nil { 137 return err 138 } 139 info := c.Info() 140 f := gnuflag.NewFlagSet(info.Name, gnuflag.ContinueOnError) 141 c.SetFlags(f) 142 ctx.Stdout.Write(info.Help(f)) 143 } 144 return nil 145 }