-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
148 lines (119 loc) · 3.27 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"strings"
"sync"
yaml "gopkg.in/yaml.v2"
)
// Context is struct for parsing context section of kubeconfig file
type Context struct {
Context struct {
Namespace string `yaml:"namespace"`
} `yaml:"context"`
Name string `yaml:"name"`
}
// Kubeconfig is struct for parsing kubeconfig file
type Kubeconfig struct {
CurrentContext string `yaml:"current-context"`
Contexts []Context `yaml:"contexts"`
}
func getCurrentContext(configs []Kubeconfig) string {
for _, config := range configs {
if config.CurrentContext != "" {
return config.CurrentContext
}
}
return ""
}
func getContextNamespace(context string, configs []Kubeconfig) string {
for _, config := range configs {
for _, ctx := range config.Contexts {
if ctx.Name == context {
return ctx.Context.Namespace
}
}
}
return "default"
}
var allowedOutput = []string{"json", "slug", "context", "namespace"}
func validateOutputFlag(output string) string {
for _, variant := range allowedOutput {
if output == variant {
return ""
}
}
return fmt.Sprintf(`Unexpected output value "%s". Allowed values: %s`, output, strings.Join(allowedOutput, ", "))
}
func main() {
kubeconfigRawEnv := os.Getenv("KUBECONFIG")
if kubeconfigRawEnv == "" {
fmt.Fprintln(os.Stderr, "Empty KUBECONFIG env variable")
os.Exit(1)
}
outputFlag := flag.String("o", "slug", `Output values. Either "json", "context", "namespace" or "slug" (context/namespace)`)
separatorFlag := flag.String("s", "/", `Separator for slug. Create stylish prompt with -s='⎈ '`)
flag.Parse()
validationError := validateOutputFlag(*outputFlag)
if validationError != "" {
fmt.Println(validationError)
os.Exit(1)
}
output := *outputFlag
separator := *separatorFlag
kubeconfigFilePaths := strings.Split(kubeconfigRawEnv, ":")
for i, path := range kubeconfigFilePaths {
kubeconfigFilePaths[i] = strings.TrimSpace(path)
}
parsedKubeconfigs := make([]Kubeconfig, len(kubeconfigFilePaths))
var wg sync.WaitGroup
wg.Add(len(kubeconfigFilePaths))
for i, file := range kubeconfigFilePaths {
// Parse all files, skip files with syntax errors
go func(fileName string, i int) {
defer wg.Done()
var config Kubeconfig
file, err := os.Open(fileName)
if err != nil {
fmt.Fprintf(os.Stderr, "Error opening file %s %v", fileName, err)
return
}
err = yaml.NewDecoder(file).Decode(&config)
if err != nil {
fmt.Fprintf(os.Stderr, "Error parsing %s %v", fileName, err)
return
}
file.Close()
parsedKubeconfigs[i] = config
}(file, i)
}
wg.Wait()
currentContext := getCurrentContext(parsedKubeconfigs)
if output == "context" {
fmt.Print(currentContext)
}
currentNamespace := getContextNamespace(currentContext, parsedKubeconfigs)
if output == "namespace" {
fmt.Print(currentNamespace)
}
if output == "slug" {
fmt.Printf("%s%s%s", currentContext, separator, currentNamespace)
}
if output == "json" {
jsonOutput := struct {
Context string `json:"context"`
Namespace string `json:"namespace"`
}{
Context: currentContext,
Namespace: currentNamespace,
}
bytes, err := json.Marshal(&jsonOutput)
if err != nil {
fmt.Printf("Error while formatting json\n%s", err)
os.Exit(1)
}
fmt.Print(string(bytes))
}
}