~dstroppa/juju-core/joyent-provider-storage

1528.3.1 by Andrew Wilkins
cmd/jujud: add help-tool subcommand
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
1528.3.2 by Andrew Wilkins
Move help-tool command to cmd/juju
20
func (dummyHookContext) UnitName() string {
1528.3.1 by Andrew Wilkins
cmd/jujud: add help-tool subcommand
21
	return ""
22
}
1528.3.2 by Andrew Wilkins
Move help-tool command to cmd/juju
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) {
1528.3.1 by Andrew Wilkins
cmd/jujud: add help-tool subcommand
36
	return charm.NewConfig().DefaultSettings(), nil
37
}
1528.3.2 by Andrew Wilkins
Move help-tool command to cmd/juju
38
func (dummyHookContext) HookRelation() (jujuc.ContextRelation, bool) {
1528.3.1 by Andrew Wilkins
cmd/jujud: add help-tool subcommand
39
	return nil, false
40
}
1528.3.2 by Andrew Wilkins
Move help-tool command to cmd/juju
41
func (dummyHookContext) RemoteUnitName() (string, bool) {
1528.3.1 by Andrew Wilkins
cmd/jujud: add help-tool subcommand
42
	return "", false
43
}
1528.3.2 by Andrew Wilkins
Move help-tool command to cmd/juju
44
func (dummyHookContext) Relation(id int) (jujuc.ContextRelation, bool) {
1528.3.1 by Andrew Wilkins
cmd/jujud: add help-tool subcommand
45
	return nil, false
46
}
1528.3.2 by Andrew Wilkins
Move help-tool command to cmd/juju
47
func (dummyHookContext) RelationIds() []int {
1528.3.1 by Andrew Wilkins
cmd/jujud: add help-tool subcommand
48
	return []int{}
49
}
50
1914.2.12 by Matthew Williams
the interface has changed, this didn't get picked up in the tests
51
func (dummyHookContext) OwnerTag() string {
52
	return ""
1914.2.3 by Matthew Williams
get-owner takes a tag parameter and started work of getting data from the right place
53
}
54
1528.3.1 by Andrew Wilkins
cmd/jujud: add help-tool subcommand
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()
1528.3.2 by Andrew Wilkins
Move help-tool command to cmd/juju
83
		cmds := make([]cmd.Command, 0, len(names))
1528.3.1 by Andrew Wilkins
cmd/jujud: add help-tool subcommand
84
		longest := 0
1528.3.2 by Andrew Wilkins
Move help-tool command to cmd/juju
85
		for _, name := range names {
1528.3.1 by Andrew Wilkins
cmd/jujud: add help-tool subcommand
86
			if c, err := jujuc.NewCommand(hookctx, name); err == nil {
87
				if len(name) > longest {
88
					longest = len(name)
89
				}
1528.3.2 by Andrew Wilkins
Move help-tool command to cmd/juju
90
				cmds = append(cmds, c)
1528.3.1 by Andrew Wilkins
cmd/jujud: add help-tool subcommand
91
			}
92
		}
1528.3.2 by Andrew Wilkins
Move help-tool command to cmd/juju
93
		for _, c := range cmds {
94
			info := c.Info()
95
			fmt.Fprintf(ctx.Stdout, "%-*s  %s\n", longest, info.Name, info.Purpose)
1528.3.1 by Andrew Wilkins
cmd/jujud: add help-tool subcommand
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
}