1
0
mirror of https://github.com/rancher/cli.git synced 2026-02-05 09:48:36 +01:00

Merge pull request #243 from chaudyg/19925

#19925 add rancher server command
This commit is contained in:
Dan Ramich
2019-08-05 09:14:52 -07:00
committed by GitHub
5 changed files with 244 additions and 22 deletions

View File

@@ -220,7 +220,11 @@ func verifyCert(caCert []byte) (string, error) {
return string(caCert), nil
}
func loadConfig(path string) (config.Config, error) {
func loadConfig(ctx *cli.Context) (config.Config, error) {
path := ctx.GlobalString("config")
if path == "" {
path = os.ExpandEnv("${HOME}/.rancher/cli2.json")
}
cf := config.Config{
Path: path,
Servers: make(map[string]*config.ServerConfig),
@@ -240,12 +244,7 @@ func loadConfig(path string) (config.Config, error) {
}
func lookupConfig(ctx *cli.Context) (*config.ServerConfig, error) {
path := ctx.GlobalString("config")
if path == "" {
path = os.ExpandEnv("${HOME}/.rancher/cli2.json")
}
cf, err := loadConfig(path)
cf, err := loadConfig(ctx)
if nil != err {
return nil, err
}

View File

@@ -1,8 +1,6 @@
package cmd
import (
"os"
"github.com/rancher/cli/cliclient"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
@@ -36,12 +34,7 @@ be automatically selected.
}
func contextSwitch(ctx *cli.Context) error {
path := ctx.GlobalString("cf")
if path == "" {
path = os.ExpandEnv("${HOME}/.rancher/cli2.json")
}
cf, err := loadConfig(path)
cf, err := loadConfig(ctx)
if err != nil {
return err
}

View File

@@ -70,12 +70,7 @@ func loginSetup(ctx *cli.Context) error {
return cli.ShowCommandHelp(ctx, "login")
}
path := ctx.GlobalString("cf")
if path == "" {
path = os.ExpandEnv("${HOME}/.rancher/cli2.json")
}
cf, err := loadConfig(path)
cf, err := loadConfig(ctx)
if err != nil {
return err
}

234
cmd/server.go Normal file
View File

@@ -0,0 +1,234 @@
package cmd
import (
"bufio"
"fmt"
"os"
"sort"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/rancher/cli/config"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
type serverData struct {
Index int
Current string
Name string
URL string
}
// ServerCommand defines the 'rancher server' sub-commands
func ServerCommand() cli.Command {
return cli.Command{
Name: "server",
Usage: "Operations for the server",
Description: `Switch or view the server currently in focus.
`,
Subcommands: []cli.Command{
{
Name: "current",
Usage: "Display the current server",
Action: serverCurrent,
},
{
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: serverDelete,
},
{
Name: "ls",
Usage: "List all servers",
Action: serverLs,
},
{
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: serverSwitch,
},
},
}
}
// serverCurrent command to display the name of the current server in the local config
func serverCurrent(ctx *cli.Context) error {
cf, err := loadConfig(ctx)
if err != nil {
return err
}
serverName := cf.CurrentServer
URL := cf.Servers[serverName].URL
fmt.Printf("Name: %s URL: %s\n", serverName, URL)
return nil
}
// serverDelete command to delete a server from the local config
func serverDelete(ctx *cli.Context) error {
cf, err := loadConfig(ctx)
if err != nil {
return err
}
var serverName string
if ctx.NArg() == 1 {
serverName = ctx.Args().First()
} else {
serverName, err = serverFromInput(ctx, cf)
if err != nil {
return err
}
}
_, ok := cf.Servers[serverName]
if !ok {
return errors.New("Server not found")
}
delete(cf.Servers, serverName)
cf.Write()
logrus.Infof("Server %s deleted", serverName)
return nil
}
// serverLs command to list rancher servers from the local config
func serverLs(ctx *cli.Context) error {
cf, err := loadConfig(ctx)
if err != nil {
return err
}
writer := NewTableWriter([][]string{
{"CURRENT", "Current"},
{"NAME", "Name"},
{"URL", "URL"},
}, ctx)
defer writer.Close()
for name, server := range cf.Servers {
var current string
if name == cf.CurrentServer {
current = "*"
}
writer.Write(&serverData{
Current: current,
Name: name,
URL: server.URL,
})
}
return writer.Err()
}
// serverSwitch command to switch rancher server.
func serverSwitch(ctx *cli.Context) error {
cf, err := loadConfig(ctx)
if err != nil {
return err
}
var serverName string
if ctx.NArg() == 1 {
serverName = ctx.Args().First()
} else {
serverName, err = serverFromInput(ctx, cf)
if err != nil {
return err
}
}
_, 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
cf.Write()
return nil
}
// 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) {
serverNames := getServerNames(cf)
displayListServers(ctx, cf)
fmt.Print("Select a Server:")
reader := bufio.NewReader(os.Stdin)
errMessage := fmt.Sprintf("Invalid input, enter a number between 1 and %v: ", len(serverNames))
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 nil != err {
fmt.Print(errMessage)
continue
}
if i <= len(serverNames) && i != 0 {
selection = i - 1
break
}
fmt.Print(errMessage)
continue
}
}
return serverNames[selection], nil
}
// displayListServers displays the list of rancher servers
func displayListServers(ctx *cli.Context, cf config.Config) error {
writer := NewTableWriter([][]string{
{"INDEX", "Index"},
{"NAME", "Name"},
{"URL", "URL"},
}, ctx)
defer writer.Close()
for idx, server := range getServerNames(cf) {
writer.Write(&serverData{
Index: idx + 1,
Name: server,
URL: cf.Servers[server].URL,
})
}
return writer.Err()
}
// getServerNames returns an order slice of existing server names
func getServerNames(cf config.Config) []string {
var serverNames []string
for server := range cf.Servers {
serverNames = append(serverNames, server)
}
sort.Strings(serverNames)
return serverNames
}

View File

@@ -8,7 +8,7 @@ import (
"github.com/pkg/errors"
"github.com/rancher/cli/cmd"
"github.com/rancher/cli/rancher_prompt"
rancherprompt "github.com/rancher/cli/rancher_prompt"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
@@ -96,6 +96,7 @@ func mainErr() error {
cmd.NodeCommand(),
cmd.ProjectCommand(),
cmd.PsCommand(),
cmd.ServerCommand(),
cmd.SettingsCommand(),
cmd.SSHCommand(),
cmd.UpCommand(),