-
Notifications
You must be signed in to change notification settings - Fork 0
/
execute.go
104 lines (93 loc) · 2.78 KB
/
execute.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
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
"sync"
"github.com/fatih/color"
"gopkg.in/yaml.v2"
)
var colorList = []*color.Color{
color.New(color.FgHiGreen, color.Bold),
color.New(color.FgHiYellow, color.Bold),
color.New(color.FgHiBlue, color.Bold),
color.New(color.FgHiMagenta, color.Bold),
color.New(color.FgHiCyan, color.Bold),
color.New(color.FgHiWhite, color.Bold),
}
type KubeConfigCurrentContext struct {
Currcontext string `yaml:"current-context"`
}
func cmdExec(cmder Commander, fs MockFilesystem, ctx string, kubeConfig string, cmdArg []string, appDir string, execData chan execSummary, wg *sync.WaitGroup) {
var combinedOutput []byte
var combinedErrors []string
var localKubeConfig string = filepath.Join(appDir, sanitizeKubeconfigFile(ctx)+".yaml")
defer func() {
var errorOutput error = nil
if _, err := fs.Stat(localKubeConfig); err == nil {
if err := fs.Remove(localKubeConfig); err != nil {
combinedErrors = append(combinedErrors, fmt.Errorf("cannot remove local kube config file %s, err: %v", localKubeConfig, err).Error())
}
}
if len(combinedErrors) > 0 {
errorOutput = fmt.Errorf(strings.Join(combinedErrors, "\n"))
}
execData <- execSummary{
ctx: ctx,
output: combinedOutput,
err: errorOutput,
}
wg.Done()
}()
fileData, err := yaml.Marshal(&KubeConfigCurrentContext{Currcontext: ctx})
if err != nil {
combinedErrors = append(combinedErrors, fmt.Errorf("yaml marshal config problem, err: %v", err).Error())
return
}
err = fs.WriteFile(localKubeConfig, fileData, 0644)
if err != nil {
combinedErrors = append(combinedErrors, fmt.Errorf("cannot create new file %s, err: %v", localKubeConfig, err).Error())
return
}
cmd := cmder.Command(cmdArg[0], cmdArg[1:]...)
cmd.Env = append(cmd.Env, append(os.Environ(), "KUBECONFIG="+localKubeConfig+":"+kubeConfig, "KUBECTX="+ctx)...)
combinedOutput, err = cmd.CombinedOutput()
if err != nil {
combinedErrors = append(combinedErrors, fmt.Errorf("error in executing command, err: %v", err).Error())
return
}
return
}
func printCmdOutput(execData chan execSummary, stopPrinting chan bool, output io.Writer) {
var coloridx int
L:
for {
select {
case data := <-execData:
for i, line := range strings.Split(string(data.output), "\n") {
if i == 0 {
color := colorList[coloridx%len(colorList)]
color.Fprintf(output, "%s\n", data.ctx)
coloridx++
}
fmt.Fprintf(output, " %s\n", line)
}
if data.err != nil {
color := color.New(color.FgHiRed, color.Bold)
color.Fprintf(output, " error:\n")
fmt.Fprintf(output, " %v\n\n", data.err)
}
case <-stopPrinting:
break L
default:
continue
}
}
}
func sanitizeKubeconfigFile(s string) string {
re := regexp.MustCompile(`[:/]`)
return re.ReplaceAllString(s, "_")
}