forked from rancher/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.go
267 lines (229 loc) · 5.86 KB
/
server.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package cmd
import (
"bufio"
"fmt"
"io"
"os"
"sort"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/rancher/cli/config"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"golang.org/x/exp/maps"
)
type serverData struct {
Index int
Current string
Name string
URL string
}
// ServerCommand defines the 'rancher server' sub-commands
func ServerCommand() cli.Command {
cfg := &config.Config{}
return cli.Command{
Name: "server",
Usage: "Operations for the server",
Description: `Switch or view the server currently in focus.
`,
Before: loadAndValidateConfig(cfg),
Subcommands: []cli.Command{
{
Name: "current",
Usage: "Display the current server",
Action: func(ctx *cli.Context) error {
return serverCurrent(ctx.App.Writer, cfg)
},
},
{
Name: "delete",
Usage: "Delete a server from the local config",
ArgsUsage: "[SERVER_NAME]",
Description: `
The server arg is optional, if not passed in a list of available servers will
be displayed and one can be selected.
`,
Action: func(ctx *cli.Context) error {
serverName, err := getSelectedServer(ctx, cfg)
if err != nil {
return err
}
return serverDelete(cfg, serverName)
},
},
{
Name: "ls",
Usage: "List all servers",
Action: func(ctx *cli.Context) error {
format := ctx.String("format")
return serverLs(ctx.App.Writer, cfg, format)
},
},
{
Name: "switch",
Usage: "Switch to a new server",
ArgsUsage: "[SERVER_NAME]",
Description: `
The server arg is optional, if not passed in a list of available servers will
be displayed and one can be selected.
`,
Action: func(ctx *cli.Context) error {
serverName, err := getSelectedServer(ctx, cfg)
if err != nil {
return err
}
return serverSwitch(cfg, serverName)
},
},
},
}
}
// serverCurrent command to display the name of the current server in the local config
func serverCurrent(out io.Writer, cfg *config.Config) error {
serverName := cfg.CurrentServer
currentServer, found := cfg.Servers[serverName]
if !found {
return errors.New("Current server not set")
}
fmt.Fprintf(out, "Name: %s URL: %s\n", serverName, currentServer.URL)
return nil
}
// serverDelete command to delete a server from the local config
func serverDelete(cfg *config.Config, serverName string) error {
_, ok := cfg.Servers[serverName]
if !ok {
return errors.New("Server not found")
}
delete(cfg.Servers, serverName)
if cfg.CurrentServer == serverName {
cfg.CurrentServer = ""
}
err := cfg.Write()
if err != nil {
return err
}
logrus.Infof("Server %s deleted", serverName)
return nil
}
// serverLs command to list rancher servers from the local config
func serverLs(out io.Writer, cfg *config.Config, format string) error {
writerConfig := &TableWriterConfig{
Writer: out,
Format: format,
}
writer := NewTableWriterWithConfig([][]string{
{"CURRENT", "Current"},
{"NAME", "Name"},
{"URL", "URL"},
}, writerConfig)
defer writer.Close()
servers := getServers(cfg)
for _, server := range servers {
writer.Write(server)
}
return writer.Err()
}
// serverSwitch will alter and write the config to switch rancher server.
func serverSwitch(cf *config.Config, serverName string) error {
_, ok := cf.Servers[serverName]
if !ok {
return errors.New("Server not found")
}
if len(cf.Servers[serverName].Project) == 0 {
logrus.Warn("No context set; some commands will not work. Run 'rancher context switch'")
}
cf.CurrentServer = serverName
err := cf.Write()
if err != nil {
return err
}
return nil
}
// getSelectedServer will get the selected server if provided as argument,
// or it will prompt the user to select one.
func getSelectedServer(ctx *cli.Context, cfg *config.Config) (string, error) {
serverName := ctx.Args().First()
if serverName != "" {
return serverName, nil
}
return serverFromInput(ctx, cfg)
}
// serverFromInput displays the list of servers from the local config and
// prompt the user to select one.
func serverFromInput(ctx *cli.Context, cf *config.Config) (string, error) {
servers := getServers(cf)
if err := displayListServers(ctx, servers); err != nil {
return "", err
}
fmt.Print("Select a Server:")
reader := bufio.NewReader(os.Stdin)
errMessage := fmt.Sprintf("Invalid input, enter a number between 1 and %v: ", len(servers))
var selection int
for {
input, err := reader.ReadString('\n')
if err != nil {
return "", err
}
input = strings.TrimSpace(input)
if input != "" {
i, err := strconv.Atoi(input)
if err != nil {
fmt.Print(errMessage)
continue
}
if i <= len(servers) && i != 0 {
selection = i - 1
break
}
fmt.Print(errMessage)
continue
}
}
return servers[selection].Name, nil
}
// displayListServers displays the list of rancher servers
func displayListServers(ctx *cli.Context, servers []*serverData) error {
writer := NewTableWriter([][]string{
{"INDEX", "Index"},
{"NAME", "Name"},
{"URL", "URL"},
}, ctx)
defer writer.Close()
for _, server := range servers {
writer.Write(server)
}
return writer.Err()
}
// getServers returns an ordered slice (by name) of serverData
func getServers(cfg *config.Config) []*serverData {
serverNames := maps.Keys(cfg.Servers)
sort.Strings(serverNames)
servers := []*serverData{}
for i, server := range serverNames {
var current string
if server == cfg.CurrentServer {
current = "*"
}
servers = append(servers, &serverData{
Index: i + 1,
Name: server,
Current: current,
URL: cfg.Servers[server].URL,
})
}
return servers
}
func loadAndValidateConfig(cfg *config.Config) cli.BeforeFunc {
return func(ctx *cli.Context) error {
conf, err := loadConfig(ctx)
if err != nil {
return err
}
*cfg = conf
if len(cfg.Servers) == 0 {
return errors.New("no servers are currently configured")
}
return nil
}
}