launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/cmd/juju/helptool.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package main 5 6 import ( 7 "fmt" 8 9 "launchpad.net/gnuflag" 10 11 "launchpad.net/juju-core/charm" 12 "launchpad.net/juju-core/cmd" 13 "launchpad.net/juju-core/worker/uniter/jujuc" 14 ) 15 16 // dummyHookContext implements jujuc.Context, 17 // as expected by jujuc.NewCommand. 18 type dummyHookContext struct{} 19 20 func (dummyHookContext) UnitName() string { 21 return "" 22 } 23 func (dummyHookContext) PublicAddress() (string, bool) { 24 return "", false 25 } 26 func (dummyHookContext) PrivateAddress() (string, bool) { 27 return "", false 28 } 29 func (dummyHookContext) OpenPort(protocol string, port int) error { 30 return nil 31 } 32 func (dummyHookContext) ClosePort(protocol string, port int) error { 33 return nil 34 } 35 func (dummyHookContext) ConfigSettings() (charm.Settings, error) { 36 return charm.NewConfig().DefaultSettings(), nil 37 } 38 func (dummyHookContext) HookRelation() (jujuc.ContextRelation, bool) { 39 return nil, false 40 } 41 func (dummyHookContext) RemoteUnitName() (string, bool) { 42 return "", false 43 } 44 func (dummyHookContext) Relation(id int) (jujuc.ContextRelation, bool) { 45 return nil, false 46 } 47 func (dummyHookContext) RelationIds() []int { 48 return []int{} 49 } 50 51 func (dummyHookContext) OwnerTag() string { 52 return "" 53 } 54 55 type HelpToolCommand struct { 56 cmd.CommandBase 57 tool string 58 } 59 60 func (t *HelpToolCommand) Info() *cmd.Info { 61 return &cmd.Info{ 62 Name: "help-tool", 63 Args: "[tool]", 64 Purpose: "show help on a juju charm tool", 65 } 66 } 67 68 func (t *HelpToolCommand) Init(args []string) error { 69 tool, err := cmd.ZeroOrOneArgs(args) 70 if err == nil { 71 t.tool = tool 72 } 73 return err 74 } 75 76 func (c *HelpToolCommand) Run(ctx *cmd.Context) error { 77 var hookctx dummyHookContext 78 if c.tool == "" { 79 // Ripped from SuperCommand. We could Run() a SuperCommand 80 // with "help commands", but then the implicit "help" command 81 // shows up. 82 names := jujuc.CommandNames() 83 cmds := make([]cmd.Command, 0, len(names)) 84 longest := 0 85 for _, name := range names { 86 if c, err := jujuc.NewCommand(hookctx, name); err == nil { 87 if len(name) > longest { 88 longest = len(name) 89 } 90 cmds = append(cmds, c) 91 } 92 } 93 for _, c := range cmds { 94 info := c.Info() 95 fmt.Fprintf(ctx.Stdout, "%-*s %s\n", longest, info.Name, info.Purpose) 96 } 97 } else { 98 c, err := jujuc.NewCommand(hookctx, c.tool) 99 if err != nil { 100 return err 101 } 102 info := c.Info() 103 f := gnuflag.NewFlagSet(info.Name, gnuflag.ContinueOnError) 104 c.SetFlags(f) 105 ctx.Stdout.Write(info.Help(f)) 106 } 107 return nil 108 }