@@ -14,8 +14,10 @@ import (
14
14
)
15
15
16
16
const (
17
- defaultSepalater = "/"
18
- defaultformat = "{{.Context}}" + defaultSepalater + "{{.Namespace}}"
17
+ defaultSepalater = "/"
18
+ defaultContextFormat = "{{.Context}}"
19
+ defaultNamespaceFormat = "{{.Namespace}}"
20
+ defaultformat = defaultContextFormat + defaultSepalater + defaultNamespaceFormat
19
21
)
20
22
21
23
type kubeContext struct {
@@ -30,26 +32,45 @@ func main() {
30
32
31
33
config , err := kubeConfig .RawConfig ()
32
34
if err != nil {
33
- fmt .Fprintln (os .Stdout , "could not get kubeconfig" )
35
+ fmt .Fprintln (os .Stdout , "[ERROR] could not get kubeconfig" )
36
+ return
34
37
}
35
38
if len (config .Contexts ) == 0 {
36
- fmt .Fprintln (os .Stdout , "kubeconfig is empty" )
39
+ fmt .Fprintln (os .Stdout , "[ERROR] kubeconfig is empty" )
40
+ return
37
41
}
38
42
39
43
curCtx := config .CurrentContext
44
+ if curCtx == "" {
45
+ kctx := kubeContext {
46
+ Context : "empty" ,
47
+ }
48
+ if err := printContext (kctx , defaultContextFormat ); err != nil {
49
+ fmt .Fprintln (os .Stdout , "[ERROR] could not print kube context" )
50
+ }
51
+ return
52
+ }
53
+
54
+ kctx := kubeContext {
55
+ Context : curCtx ,
56
+ }
57
+
40
58
curNs := config .Contexts [curCtx ].Namespace
41
59
if curNs == "" {
42
60
curNs = corev1 .NamespaceDefault
43
61
}
62
+ kctx .Namespace = curNs
44
63
45
64
format := defaultformat
46
65
if len (os .Args ) > 1 {
47
66
format = os .Args [1 ]
48
67
}
49
68
50
- kctx := kubeContext {
51
- Context : curCtx ,
52
- Namespace : curNs ,
69
+ if err := printContext (kctx , format ); err != nil {
70
+ fmt .Fprintf (os .Stdout , "[ERROR] could not print kube context: %v\n " , err )
53
71
}
54
- template .Must (template .New ("kube-tmux" ).Parse (format )).Execute (os .Stdout , kctx )
72
+ }
73
+
74
+ func printContext (kctx kubeContext , format string ) error {
75
+ return template .Must (template .New ("kube-tmux" ).Parse (format )).Execute (os .Stdout , kctx )
55
76
}
0 commit comments