-
Notifications
You must be signed in to change notification settings - Fork 52
/
util.go
135 lines (111 loc) · 2.89 KB
/
util.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
package util
// @TODO split up and move into /pkg directory
import (
"fmt"
"io"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"strconv"
"strings"
"text/template"
"github.com/google/uuid"
)
func CreateDirIfDoesNotExist(path string) error {
if _, err := os.Stat(path); os.IsNotExist(err) {
err = os.MkdirAll(path, os.ModePerm)
return err
}
return nil
}
func CleanGoIdentifier(identifier string) string {
return strings.ReplaceAll(identifier, "-", "")
}
// @TODO how can we make these type of helpers extensible?
var FuncMap = template.FuncMap{
"Title": strings.Title,
"ToLower": strings.ToLower,
"CleanGoIdentifier": CleanGoIdentifier,
"GenerateUUID": uuid.New,
}
func GetCwd() string {
dir, err := os.Getwd()
if err != nil {
log.Fatalf("Getting working directory failed: %v\n", err)
panic(err)
}
return dir
}
func ExecuteCommand(cmd *exec.Cmd, pathPrefix string, envars []string) {
cmd.Dir = pathPrefix
if !filepath.IsAbs(pathPrefix) {
dir := GetCwd()
cmd.Dir = path.Join(dir, pathPrefix)
}
stdoutPipe, _ := cmd.StdoutPipe()
stderrPipe, _ := cmd.StderrPipe()
var errStdout, errStderr error
cmd.Env = os.Environ()
if envars != nil {
cmd.Env = append(os.Environ(), envars...)
}
err := cmd.Start()
if err != nil {
log.Fatalf("Starting command failed: %v\n", err)
}
go func() {
_, errStdout = io.Copy(os.Stdout, stdoutPipe)
}()
go func() {
_, errStderr = io.Copy(os.Stderr, stderrPipe)
}()
err = cmd.Wait()
if err != nil {
log.Fatalf("Executing command failed: %v\n", err)
}
if errStdout != nil {
log.Printf("Failed to capture stdout: %v\n", errStdout)
}
if errStderr != nil {
log.Printf("Failed to capture stderr: %v\n", errStderr)
}
}
// ExecuteCommandOutput runs the command and returns its
// combined standard output and standard error.
func ExecuteCommandOutput(cmd *exec.Cmd, pathPrefix string, envars []string) string {
cmd.Dir = pathPrefix
if !filepath.IsAbs(pathPrefix) {
dir := GetCwd()
cmd.Dir = path.Join(dir, pathPrefix)
}
cmd.Env = os.Environ()
if envars != nil {
cmd.Env = append(os.Environ(), envars...)
}
out, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("Executing command with output failed: (%v) %s\n", err, out)
}
return string(out)
}
// AppendProjectEnvToCmdEnv will add all the keys and values from envMap
// into envList as key-value pair strings (e.g.: "key=value")
func AppendProjectEnvToCmdEnv(envMap map[string]string, envList []string) []string {
for key, val := range envMap {
if val != "" {
envList = append(envList, fmt.Sprintf("%s=%s", key, val))
}
}
return envList
}
// IndentString will Add x space char padding at the beginging of each line.
func IndentString(content string, spaces int) string {
var result string
subStr := strings.Split(content, "\n")
for _, s := range subStr {
result += fmt.Sprintf("%"+strconv.Itoa(spaces)+"s%s\n", "", s)
}
return result
}