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