-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (68 loc) · 1.56 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
package main
import (
"io/ioutil"
"log"
"os"
"os/exec"
"sync"
)
const (
appsubdir = ".mkubectx"
)
type execSummary struct {
ctx string
output []byte
err error
}
type Commander interface {
Command(string, ...string) *exec.Cmd
}
type RealCommander struct{}
func (c RealCommander) Command(command string, args ...string) *exec.Cmd {
return exec.Command(command, args...)
}
type MockFilesystem interface {
Stat(string) (os.FileInfo, error)
Remove(string) error
WriteFile(string, []byte, os.FileMode) error
}
type RealFilesystem struct{}
func (r RealFilesystem) Stat(name string) (os.FileInfo, error) {
return os.Stat(name)
}
func (r RealFilesystem) Remove(name string) error {
return os.Remove(name)
}
func (r RealFilesystem) WriteFile(name string, data []byte, filemode os.FileMode) error {
return ioutil.WriteFile(name, data, filemode)
}
func main() {
cmdArg := getCliFlags()
kubeConfig, err := getKubeConfig()
if err != nil {
log.Fatal(err)
}
getKubeConfigContexts(kubeConfig)
appDir, err := createLocalKubeConfig(appsubdir)
if err != nil {
log.Fatal(err)
}
execData := make(chan execSummary)
stopPrinting := make(chan bool)
defer func() {
stopPrinting <- true
}()
go printCmdOutput(execData, stopPrinting, os.Stdout)
filteredContexts, err := getFilteredContexts()
if err != nil {
log.Fatal(err)
}
var wg sync.WaitGroup
wg.Add(len(filteredContexts))
cmder := RealCommander{}
fs := RealFilesystem{}
for _, ctx := range filteredContexts {
go cmdExec(cmder, fs, ctx.Name, kubeConfig, cmdArg, appDir, execData, &wg)
}
wg.Wait()
}