/
script.go
136 lines (115 loc) · 3.32 KB
/
script.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright 2023 The Katanomi Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package testing
import (
"bytes"
"embed"
"fmt"
"os"
"os/exec"
"path"
"strings"
)
// DefaultScriptOutputPrefix the prefix of script output
const DefaultScriptOutputPrefix = "##output##"
// RestoreDirectories restore directories from embed.FS to targetDir
func RestoreDirectories(fs embed.FS, dirName string, targetDir string) {
entries, err := fs.ReadDir(dirName)
if err != nil {
panic(err)
}
for _, entry := range entries {
if entry.IsDir() {
RestoreDirectories(fs, path.Join(dirName, entry.Name()), targetDir)
} else {
filePath := path.Join(dirName, entry.Name())
targetFilePath := path.Join(targetDir, filePath)
fileContent, _ := fs.ReadFile(filePath)
os.MkdirAll(path.Dir(targetFilePath), 0750)
err = os.WriteFile(targetFilePath, fileContent, 0750)
if err != nil {
panic(fmt.Sprintf("failed to save %s to %s, err: %s", filePath, targetFilePath, err))
}
}
}
}
// ExecBashScript exec bash script with params
func ExecBashScript(script string, params ...string) *ScriptResult {
return ExecScript("bash", append([]string{script}, params...)...)
}
// ExecScript exec script with params
func ExecScript(name string, arg ...string) *ScriptResult {
c := exec.Command(name, arg...)
var stdout bytes.Buffer
var stderr bytes.Buffer
c.Stdout = &stdout
c.Stderr = &stderr
err := c.Run()
result := &ScriptResult{
stdout: stdout,
stderr: stderr,
}
if err != nil {
if exitError, ok := err.(*exec.ExitError); ok {
result.exitError = exitError
}
}
return result
}
// ScriptResult the result of script execution
type ScriptResult struct {
stdout bytes.Buffer
stderr bytes.Buffer
exitError *exec.ExitError
OutputDataPrefix string
}
// Error get the error of a script execution
func (p *ScriptResult) Error() error {
return p.exitError
}
// Stdout get the output of a script execution
func (p *ScriptResult) Stdout() string {
return p.stdout.String()
}
// Stderr get the error output of a script execution
func (p *ScriptResult) Stderr() string {
return p.stderr.String()
}
// ExitCode get the exit code of a script execution
func (p *ScriptResult) ExitCode() int {
if p.exitError == nil {
return 0
}
return p.exitError.ExitCode()
}
// ExitMessage get the exit message of a script execution
func (p *ScriptResult) ExitMessage() string {
if p.exitError == nil {
return ""
}
return p.exitError.String()
}
// OutputData get the output of a script execution,
// it can be a structured data, e.g: json string
func (p *ScriptResult) OutputData(dataPrefix string) string {
if dataPrefix == "" {
dataPrefix = DefaultScriptOutputPrefix
}
parts := strings.Split(p.Stdout(), "\n")
for i := len(parts) - 1; i >= 0; i-- {
if strings.HasPrefix(parts[i], dataPrefix) {
return strings.TrimPrefix(parts[i], dataPrefix)
}
}
return ""
}