-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.go
102 lines (83 loc) · 2.14 KB
/
functions.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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"github.com/manifoldco/promptui"
)
func startNewShell(kc string) {
// Get the current working directory.
cwd, err := os.Getwd()
if err != nil {
panic(err)
}
// Set envvar with path to kubeconfig file
os.Setenv("KUBECONFIG", kc)
// Transfer stdin, stdout, and stderr to the new process
// and also set target directory for the shell to start in.
pa := os.ProcAttr{
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
Dir: cwd,
}
// Start up a new shell.
proc, err := os.StartProcess(os.Getenv("SHELL"), []string{os.Getenv("SHELL")}, &pa)
if err != nil {
panic(err)
}
// Wait until user exits the shell
_, err = proc.Wait()
if err != nil {
panic(err)
}
}
type kubeconfig struct {
Name string
Path string
Context string
}
func selectKubeconfig() string {
kcpath := getConfig()
files, err := ioutil.ReadDir(kcpath)
if err != nil {
log.Fatal(err)
}
kubeconfigList := []kubeconfig{}
for _, f := range files {
kubeconfigList = append(kubeconfigList, kubeconfig{
f.Name(),
kcpath,
getKubeconfigContexts(kcpath + "/" + f.Name()),
})
}
templates := &promptui.SelectTemplates{
Label: "{{ . | bold }}",
Active: "> {{ .Name | cyan | bold }}",
Inactive: " {{ .Name }}",
Details: `
{{ "Contexts: " }}{{ .Context }}`,
}
searcher := func(input string, index int) bool {
kc := kubeconfigList[index]
name := strings.Replace(strings.ToLower(kc.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
prompt := promptui.Select{
Label: "Current Config : " + getCurrentKubeConfig(),
Items: kubeconfigList,
Templates: templates,
Size: 10,
Searcher: searcher,
StartInSearchMode: true,
HideSelected: true,
// HideHelp: true,
}
i, _, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return ""
}
return kubeconfigList[i].Path + "/" + kubeconfigList[i].Name
}