-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.go
56 lines (48 loc) · 1 KB
/
utils.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
package main
import (
"io/ioutil"
"log"
"os"
"strings"
yaml "gopkg.in/yaml.v2"
)
func getConfig() string {
home := os.Getenv("HOME")
kcp, _ := ioutil.ReadFile(home + "/.kcs")
kcs := strings.Split(string(kcp), "=")
if kcs[0] == "KUBECONFIG_FILES" && len(kcs[1]) != 0 {
return strings.TrimSuffix(kcs[1], "\n")
} else {
log.Fatal("wrong config")
}
return ""
}
type kubeconfigyaml struct {
Contexts []context `yaml:"contexts"`
}
type context struct {
Name string `yaml:"name"`
}
func getKubeconfigContexts(kcf string) string {
var c kubeconfigyaml
yamlFile, err := ioutil.ReadFile(kcf)
if err != nil {
return ""
}
err = yaml.Unmarshal(yamlFile, &c)
if err != nil {
return ""
}
var v string
for _, cx := range c.Contexts {
v = v + cx.Name + " | "
}
return v[:len(v)-3]
}
func getCurrentKubeConfig() string {
if os.Getenv("KUBECONFIG") == "" {
return "None"
}
k := strings.Split(os.Getenv("KUBECONFIG"), "/")
return k[len(k)-1]
}