This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.go
197 lines (168 loc) · 4.33 KB
/
main.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
188
189
190
191
192
193
194
195
196
197
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package relay
import (
"errors"
"regexp"
"sort"
"strconv"
"github.com/jawher/mow.cli"
"github.com/joyent/conch-shell/pkg/conch"
"github.com/joyent/conch-shell/pkg/util"
)
func register(app *cli.Cmd) {
var (
versionOpt = app.StringOpt("version", "", "The version of the relay")
sshPortOpt = app.IntOpt("ssh_port port", 22, "The SSH port for the relay")
ipAddrOpt = app.StringOpt("ipaddr ip", "", "The IP address for the relay")
aliasOpt = app.StringOpt("alias name", "", "The alias for the relay")
)
app.Spec = "--version [OPTIONS]"
app.Action = func() {
r := conch.WorkspaceRelay{
ID: RelaySerial,
Version: *versionOpt,
IPAddr: *ipAddrOpt,
Alias: *aliasOpt,
SSHPort: *sshPortOpt,
}
if err := util.API.RegisterRelay(r); err != nil {
util.Bail(err)
}
}
}
func getAllRelays(app *cli.Cmd) {
app.Before = util.BuildAPIAndVerifyLogin
app.Action = func() {
relays, err := util.API.GetAllRelays()
if err != nil {
util.Bail(err)
}
if util.JSON {
util.JSONOut(relays)
return
}
table := util.GetMarkdownTable()
table.SetHeader([]string{
"ID",
"Alias",
"IP Addr",
"SSH Port",
"Version",
"Created",
"Updated",
})
sort.Sort(sortRelaysByUpdated(relays))
for _, r := range relays {
table.Append([]string{
r.ID,
r.Alias,
r.IPAddr,
strconv.Itoa(r.SSHPort),
r.Version,
util.TimeStr(r.Created),
util.TimeStr(r.Updated),
})
}
table.Render()
}
}
type sortRelaysByUpdated []conch.WorkspaceRelay
func (s sortRelaysByUpdated) Len() int {
return len(s)
}
func (s sortRelaysByUpdated) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s sortRelaysByUpdated) Less(i, j int) bool {
return s[i].Updated.Before(s[j].Updated)
}
func findRelaysByName(app *cli.Cmd) {
var (
relays = app.StringsArg("RELAYS", nil, "List of regular expressions to match against relay IDs")
andOpt = app.BoolOpt("and", false, "Match the list as a logical AND")
)
app.Spec = "[OPTIONS] RELAYS..."
app.LongDesc = `
Takes a list of regular expressions and matches those against the IDs of all known relays.
The default behavior is to match as a logical OR but this behavior can be changed by providing the --and flag
For instance:
* "conch relay find drd" will find all relays with 'drd' in their ID. For perl folks, this is essentially 'm/drd/'
* "conch relay find '^ams-'" will find all relays with IDs that begin with 'ams-'
* "conch relay find drd '^ams-' will find all relays with IDs that contain 'drd' OR begin with 'ams-'
* "conch relay find --and drd '^ams-' will find all relays with IDs that contain 'drd' AND begin with '^ams-'`
app.Action = func() {
if *relays == nil {
util.Bail(errors.New("please provide a list of regular expressions"))
}
// If a user for some strange reason gives us a relay name of "", the
// cli lib will pass it on to us. That name is obviously useless so
// let's filter it out.
relayREs := make([]*regexp.Regexp, 0)
for _, matcher := range *relays {
if matcher == "" {
continue
}
re, err := regexp.Compile(matcher)
if err != nil {
util.Bail(err)
}
relayREs = append(relayREs, re)
}
if len(relayREs) == 0 {
util.Bail(errors.New("please provide a list of regular expressions"))
}
relays, err := util.API.GetAllRelays()
if err != nil {
util.Bail(err)
}
keepers := make(sortRelaysByUpdated, 0)
for _, relay := range relays {
matched := 0
for _, re := range relayREs {
if re.MatchString(relay.ID) {
if *andOpt {
matched++
} else {
keepers = append(keepers, relay)
continue
}
}
}
if *andOpt {
if matched == len(relayREs) {
keepers = append(keepers, relay)
}
}
}
sort.Sort(keepers)
if util.JSON {
util.JSONOut(keepers)
return
}
table := util.GetMarkdownTable()
table.SetHeader([]string{
"ID",
"Alias",
"IP Addr",
"SSH Port",
"Version",
"Created",
"Updated",
})
for _, r := range keepers {
table.Append([]string{
r.ID,
r.Alias,
r.IPAddr,
strconv.Itoa(r.SSHPort),
r.Version,
util.TimeStr(r.Created),
util.TimeStr(r.Updated),
})
}
table.Render()
}
}