/
context.go
112 lines (104 loc) · 2.46 KB
/
context.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
package cmd
import (
"fmt"
"github.com/fatih/color"
"github.com/metal-stack/metalctl/pkg/api"
"github.com/spf13/cobra"
)
func newContextCmd(c *config) *cobra.Command {
contextCmd := &cobra.Command{
Use: "context <name>",
Aliases: []string{"ctx"},
Short: "manage metalctl context",
Long: "context defines the backend to which metalctl talks to. You can switch back and forth with \"-\"",
ValidArgsFunction: c.comp.ContextListCompletion,
Example: `
~/.metalctl/config.yaml
---
current: prod
contexts:
prod:
url: https://api.metal-stack.io/metal
issuer_url: https://dex.metal-stack.io/dex
client_id: metal_client
client_secret: 456
dev:
url: https://api.metal-stack.dev/metal
issuer_url: https://dex.metal-stack.dev/dex
client_id: metal_client
client_secret: 123
...
`,
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 1 {
return contextSet(args)
}
if len(args) == 0 {
return c.contextList()
}
return nil
},
}
contextShortCmd := &cobra.Command{
Use: "short",
Short: "only show the default context name",
RunE: func(cmd *cobra.Command, args []string) error {
return contextShort()
},
}
contextCmd.AddCommand(contextShortCmd)
return contextCmd
}
func contextShort() error {
ctxs, err := api.GetContexts()
if err != nil {
return err
}
fmt.Println(ctxs.CurrentContext)
return nil
}
func contextSet(args []string) error {
if len(args) < 1 {
return fmt.Errorf("no context name given")
}
if args[0] == "-" {
return previous()
}
ctxs, err := api.GetContexts()
if err != nil {
return err
}
nextCtx := args[0]
_, ok := ctxs.Contexts[nextCtx]
if !ok {
return fmt.Errorf("context %s not found", nextCtx)
}
if nextCtx == ctxs.CurrentContext {
fmt.Printf("%s context \"%s\" already active\n", color.GreenString("✔"), color.GreenString(ctxs.CurrentContext))
return nil
}
ctxs.PreviousContext = ctxs.CurrentContext
ctxs.CurrentContext = nextCtx
return api.WriteContexts(ctxs)
}
func previous() error {
ctxs, err := api.GetContexts()
if err != nil {
return err
}
prev := ctxs.PreviousContext
if prev == "" {
return fmt.Errorf("no previous context found")
}
curr := ctxs.CurrentContext
ctxs.PreviousContext = curr
ctxs.CurrentContext = prev
return api.WriteContexts(ctxs)
}
func (c *config) contextList() error {
ctxs, err := api.GetContexts()
if err != nil {
return err
}
return c.listPrinter.Print(ctxs)
}