/
context.go
174 lines (157 loc) · 3.74 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
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
package cmd
import (
"fmt"
"io/ioutil"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"gopkg.in/yaml.v3"
)
// Contexts contains all configuration contexts of metalctl
type Contexts struct {
CurrentContext string `yaml:"current"`
PreviousContext string `yaml:"previous"`
Contexts map[string]Context
}
// Context configure metalctl behaviour
type Context struct {
ApiURL string `yaml:"url"`
IssuerURL string `yaml:"issuer_url"`
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
HMAC *string `yaml:"hmac"`
}
var (
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: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
if len(args) != 0 {
return nil, cobra.ShellCompDirectiveNoFileComp
}
return 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 contextList()
}
return nil
},
PreRun: bindPFlags,
}
contextShortCmd = &cobra.Command{
Use: "short",
Short: "only show the default context name",
RunE: func(cmd *cobra.Command, args []string) error {
return contextShort()
},
PreRun: bindPFlags,
}
defaultCtx = Context{
ApiURL: "http://localhost:8080/metal",
IssuerURL: "http://localhost:8080/",
}
)
func init() {
contextCmd.AddCommand(contextShortCmd)
}
func contextShort() error {
ctxs, err := 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 := getContexts()
if err != nil {
return err
}
curr := args[0]
_, ok := ctxs.Contexts[curr]
if !ok {
return fmt.Errorf("context %s not found", curr)
}
ctxs.PreviousContext = ctxs.CurrentContext
ctxs.CurrentContext = curr
return writeContexts(ctxs)
}
func previous() error {
ctxs, err := getContexts()
if err != nil {
return err
}
prev := ctxs.PreviousContext
if prev == "" {
prev = ctxs.CurrentContext
}
curr := ctxs.CurrentContext
ctxs.PreviousContext = curr
ctxs.CurrentContext = prev
return writeContexts(ctxs)
}
func contextList() error {
ctxs, err := getContexts()
if err != nil {
return err
}
return printer.Print(ctxs)
}
func mustDefaultContext() Context {
ctxs, err := getContexts()
if err != nil {
return defaultCtx
}
ctx, ok := ctxs.Contexts[ctxs.CurrentContext]
if !ok {
return defaultCtx
}
return ctx
}
func getContexts() (*Contexts, error) {
var ctxs Contexts
cfgFile := viper.GetViper().ConfigFileUsed()
c, err := ioutil.ReadFile(cfgFile)
if err != nil {
return nil, fmt.Errorf("unable to read config, please create a config.yaml in either: /etc/metalctl/, $HOME/.metalctl/ or in the current directory, see metalctl ctx -h for examples")
}
err = yaml.Unmarshal(c, &ctxs)
return &ctxs, err
}
func writeContexts(ctxs *Contexts) error {
cfgFile := viper.GetViper().ConfigFileUsed()
fmt.Printf("update config:%s\n", cfgFile)
c, err := yaml.Marshal(ctxs)
if err != nil {
return err
}
return ioutil.WriteFile(cfgFile, c, 0644)
}