This repository has been archived by the owner on Dec 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexec.go
109 lines (100 loc) · 3.04 KB
/
exec.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
package testutils
import (
"fmt"
"os/exec"
"regexp"
"strings"
// get auth clients for gcp
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
// FlagWasSet ...
func FlagWasSet(flagArgs []string, flagName string) bool {
re := regexp.MustCompile(flagName)
found := false
for _, arg := range flagArgs {
if re.MatchString(arg) {
found = true
break
}
}
return found
}
// SetAppResourcesPath ...
func SetAppResourcesPath(flagArgs []string) []string {
if len(flagArgs) > 2 {
mainCmd := flagArgs[0]
app := flagArgs[1]
if mainCmd == "create" || mainCmd == "update" || mainCmd == "stop" || mainCmd == "start" {
resourcesPath := ""
switch strings.ToUpper(app) {
case "ALERT":
resourcesPath = TestConfig.Alert.AppResourcesPath
case "BDBA":
resourcesPath = TestConfig.BDBA.AppResourcesPath
case "BLACKDUCK":
resourcesPath = TestConfig.BlackDuck.AppResourcesPath
case "OPSSIGHT":
resourcesPath = TestConfig.OpsSight.AppResourcesPath
case "POLARIS":
resourcesPath = TestConfig.Polaris.AppResourcesPath
case "POLARIS_REPORTING":
resourcesPath = TestConfig.PolarisReporting.AppResourcesPath
}
if resourcesPath != "" && !FlagWasSet(flagArgs, "--app-resources-path") {
flagArgs = append(flagArgs, fmt.Sprintf("--app-resources-path=%s", resourcesPath))
}
}
}
return flagArgs
}
// SetBlackDuckRegistry ...
func SetBlackDuckRegistry(flagArgs []string) []string {
if len(flagArgs) > 2 {
mainCmd := flagArgs[0]
app := flagArgs[1]
if mainCmd == "create" || mainCmd == "update" {
registry := ""
switch strings.ToUpper(app) {
case "BLACKDUCK":
registry = TestConfig.BlackDuck.Registry
}
if registry != "" && !FlagWasSet(flagArgs, "--registry") {
flagArgs = append(flagArgs, fmt.Sprintf("--registry=%s", registry))
}
}
}
return flagArgs
}
// SynopsysctlOperator ...
func SynopsysctlOperator(cmdString string, args ...interface{}) (string, error) {
fullCmdString := fmt.Sprintf(cmdString, args...)
cmdValues := strings.Fields(fullCmdString)
cmdValues = append([]string{TestConfig.SynopsysctlOperatorPath}, cmdValues...)
return ExecCmd(cmdValues...)
}
// Synospysctl ...
func Synospysctl(cmdString string, args ...interface{}) (string, error) {
fullCmdString := fmt.Sprintf(cmdString, args...)
cmdValues := strings.Fields(fullCmdString)
cmdValues = SetAppResourcesPath(cmdValues)
cmdValues = SetBlackDuckRegistry(cmdValues)
cmdValues = append([]string{TestConfig.SynopsysctlPath}, cmdValues...)
return ExecCmd(cmdValues...)
}
// Helm ...
func Helm(cmdString string, args ...interface{}) (string, error) {
fullCmdString := fmt.Sprintf(cmdString, args...)
cmdValues := strings.Fields(fullCmdString)
cmdValues = append([]string{"helm"}, cmdValues...)
return ExecCmd(cmdValues...)
}
//ExecCmd ...
func ExecCmd(args ...string) (string, error) {
fmt.Printf("\033[2m >> %s\033[0m\n", strings.Join(args, " "))
cmd := exec.Command(args[0], args[1:]...)
stdoutErr, err := cmd.CombinedOutput()
if err != nil {
return "", fmt.Errorf("%s [%+v]", string(stdoutErr), err)
}
return string(stdoutErr), nil
}