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

add rancher server command

This commit is contained in:
Georges Chaudy
2019-08-01 00:16:11 +01:00
parent cf08f1c0fe
commit b130a9bfce
5 changed files with 165 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
}

155
cmd/server.go Normal file
View File

@@ -0,0 +1,155 @@
package cmd
import (
"bufio"
"fmt"
"os"
"strings"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
type serverData struct {
Name string
URL string
}
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",
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,
},
},
}
}
func serverCurrent(ctx *cli.Context) error {
cf, err := loadConfig(ctx)
if err != nil {
return err
}
fmt.Printf("%s\n", cf.CurrentServer)
return nil
}
func serverDelete(ctx *cli.Context) error {
cf, err := loadConfig(ctx)
if err != nil {
return err
}
serverName, err := serverSelect(ctx)
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
}
func serverLs(ctx *cli.Context) error {
cf, err := loadConfig(ctx)
if err != nil {
return err
}
writer := NewTableWriter([][]string{
{"NAME", "Name"},
{"URL", "URL"},
}, ctx)
defer writer.Close()
for name, server := range cf.Servers {
writer.Write(&serverData{
Name: name,
URL: server.URL,
})
}
return writer.Err()
}
func serverSwitch(ctx *cli.Context) error {
cf, err := loadConfig(ctx)
if err != nil {
return err
}
serverName, err := serverSelect(ctx)
if err != nil {
return err
}
_, ok := cf.Servers[serverName]
if !ok {
return errors.New("Server not found")
}
cf.CurrentServer = serverName
cf.Write()
return nil
}
func serverSelect(ctx *cli.Context) (string, error) {
serverName := ""
if ctx.NArg() == 1 {
serverName = ctx.Args().First()
} else {
serverLs(ctx)
fmt.Print("Select a Server:")
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
if err != nil {
return "", err
}
serverName = strings.TrimSpace(input)
}
return serverName, nil
}

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(),