forked from rancher/kontainer-engine
/
env.go
47 lines (41 loc) · 1 KB
/
env.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
package cmd
import (
"fmt"
"github.com/rancher/kontainer-engine/store"
"github.com/rancher/kontainer-engine/utils"
"github.com/urfave/cli"
)
// EnvCommand defines the env command
func EnvCommand() cli.Command {
return cli.Command{
Name: "env",
Usage: "Set cluster as current context",
Action: env,
}
}
func env(ctx *cli.Context) error {
name := ctx.Args().Get(0)
if name == "" || name == "--help" {
return cli.ShowCommandHelp(ctx, "env")
}
clusters, err := store.GetAllClusterFromStore()
if err != nil {
return err
}
_, ok := clusters[name]
if !ok {
return fmt.Errorf("cluster %v can't be found", name)
}
config, err := getConfigFromFile()
if err != nil {
return err
}
config.CurrentContext = name
if err := setConfigToFile(config); err != nil {
return err
}
configFile := utils.KubeConfigFilePath()
fmt.Printf("Current context is set to %s\n", name)
fmt.Printf("run `export KUBECONFIG=%v` or `--kubeconfig %s` to use the config file\n", configFile, configFile)
return nil
}