-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
ui.go
187 lines (153 loc) · 4.22 KB
/
ui.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package command
import (
"fmt"
"net/url"
"strings"
"github.com/hashicorp/nomad/api/contexts"
"github.com/posener/complete"
"github.com/skratchdot/open-golang/open"
)
var (
// uiContexts is the contexts the ui can open automatically.
uiContexts = []contexts.Context{contexts.Jobs, contexts.Allocs, contexts.Nodes}
)
type UiCommand struct {
Meta
}
func (c *UiCommand) Help() string {
helpText := `
Usage: nomad ui [options] <identifier>
Open the Nomad Web UI in the default browser. An optional identifier may be
provided, in which case the UI will be opened to view the details for that
object. Supported identifiers are jobs, allocations and nodes.
General Options:
` + generalOptionsUsage()
return strings.TrimSpace(helpText)
}
func (c *UiCommand) AutocompleteFlags() complete.Flags {
return c.Meta.AutocompleteFlags(FlagSetClient)
}
func (c *UiCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictFunc(func(a complete.Args) []string {
client, err := c.Meta.Client()
if err != nil {
return nil
}
resp, _, err := client.Search().PrefixSearch(a.Last, contexts.All, nil)
if err != nil {
return []string{}
}
final := make([]string, 0)
for _, allowed := range uiContexts {
matches, ok := resp.Matches[allowed]
if !ok {
continue
}
if len(matches) == 0 {
continue
}
final = append(final, matches...)
}
return final
})
}
func (c *UiCommand) Synopsis() string {
return "Open the Nomad Web UI"
}
func (c *UiCommand) Name() string { return "ui" }
func (c *UiCommand) Run(args []string) int {
flags := c.Meta.FlagSet(c.Name(), FlagSetClient)
flags.Usage = func() { c.Ui.Output(c.Help()) }
if err := flags.Parse(args); err != nil {
return 1
}
// Check that we got no more than one argument
args = flags.Args()
if l := len(args); l > 1 {
c.Ui.Error("This command takes no or one optional argument, [<identifier>]")
c.Ui.Error(commandErrorText(c))
return 1
}
// Get the HTTP client
client, err := c.Meta.Client()
if err != nil {
c.Ui.Error(fmt.Sprintf("Error initializing client: %s", err))
return 1
}
url, err := url.Parse(client.Address())
if err != nil {
c.Ui.Error(fmt.Sprintf("Error parsing Nomad address %q: %s", client.Address(), err))
return 1
}
// We were given an id so look it up
if len(args) == 1 {
id := args[0]
// Query for the context associated with the id
res, _, err := client.Search().PrefixSearch(id, contexts.All, nil)
if err != nil {
c.Ui.Error(fmt.Sprintf("Error querying search with id: %q", err))
return 1
}
if res.Matches == nil {
c.Ui.Error(fmt.Sprintf("No matches returned for query: %q", err))
return 1
}
var match contexts.Context
var fullID string
matchCount := 0
for _, ctx := range uiContexts {
vers, ok := res.Matches[ctx]
if !ok {
continue
}
if l := len(vers); l == 1 {
match = ctx
fullID = vers[0]
matchCount++
} else if l > 0 && vers[0] == id {
// Exact match
match = ctx
fullID = vers[0]
break
}
// Only a single result should return, as this is a match against a full id
if matchCount > 1 || len(vers) > 1 {
c.logMultiMatchError(id, res.Matches)
return 1
}
}
switch match {
case contexts.Nodes:
url.Path = fmt.Sprintf("ui/clients/%s", fullID)
case contexts.Allocs:
url.Path = fmt.Sprintf("ui/allocations/%s", fullID)
case contexts.Jobs:
url.Path = fmt.Sprintf("ui/jobs/%s", fullID)
default:
c.Ui.Error(fmt.Sprintf("Unable to resolve ID: %q", id))
return 1
}
}
c.Ui.Output(fmt.Sprintf("Opening URL %q", url.String()))
if err := open.Start(url.String()); err != nil {
c.Ui.Error(fmt.Sprintf("Error opening URL: %s", err))
return 1
}
return 0
}
// logMultiMatchError is used to log an error message when multiple matches are
// found. The error message logged displays the matched IDs per context.
func (c *UiCommand) logMultiMatchError(id string, matches map[contexts.Context][]string) {
c.Ui.Error(fmt.Sprintf("Multiple matches found for id %q", id))
for _, ctx := range uiContexts {
vers, ok := matches[ctx]
if !ok {
continue
}
if len(vers) == 0 {
continue
}
c.Ui.Error(fmt.Sprintf("\n%s:", strings.Title(string(ctx))))
c.Ui.Error(fmt.Sprintf("%s", strings.Join(vers, ", ")))
}
}