generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 15
/
util.go
152 lines (133 loc) · 3.57 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package util
import (
"encoding/base64"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/hashicorp/go-multierror"
)
// This package should only be used as a holding pattern to be later moved into more specific packages
func MapAppend(m map[string]interface{}, n ...map[string]interface{}) map[string]interface{} {
if m == nil { // we may get nil maps from legacy users not having user.OnboardingStatus set
m = make(map[string]interface{})
}
for _, item := range n {
for key, value := range item {
m[key] = value
}
}
return m
}
// checks if noun or pulural version of noun (checks if s at end)
func IsSingularOrPlural(check, noun string) bool {
// TODO complex logic
return check == noun || fmt.Sprintf("%ss", noun) == check
}
func DecodeBase64OrReturnSelf(maybeBase64 string) []byte {
res, err := base64.StdEncoding.DecodeString(maybeBase64)
if err != nil {
fmt.Println("could not decode base64 assuming regular string")
return []byte(maybeBase64)
}
return res
}
func RemoveFileExtenstion(path string) string {
return strings.TrimRight(path, filepath.Ext(path))
}
type RunEResult struct {
errChan chan error
num int
}
func (r RunEResult) Await() error {
var allErr error
for i := 0; i < r.num; i++ {
err := <-r.errChan
if err != nil {
allErr = multierror.Append(err)
}
}
if allErr != nil {
return breverrors.WrapAndTrace(allErr)
}
return nil
}
func RunEAsync(calls ...func() error) RunEResult {
res := RunEResult{make(chan error), len(calls)}
for _, c := range calls {
go func(cl func() error) {
err := cl()
res.errChan <- err
}(c)
}
return res
}
func IsGitURL(u string) bool {
return strings.Contains(u, "https://") || strings.Contains(u, "git@")
}
func DoesPathExist(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
return false
}
func InstallVscodeExtension(extensionID string) error {
_, err := TryRunVsCodeCommand([]string{"--install-extension", extensionID, "--force"})
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
}
func IsVSCodeExtensionInstalled(extensionID string) (bool, error) {
out, err := TryRunVsCodeCommand([]string{"--list-extensions"})
if err != nil {
return false, breverrors.WrapAndTrace(err)
}
return strings.Contains(string(out), extensionID), nil
}
func TryRunVsCodeCommand(args []string, extraPaths ...string) ([]byte, error) {
extraPaths = append(commonVSCodePaths, extraPaths...)
out, err := runManyVsCodeCommand(extraPaths, args)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
return out, nil
}
func runManyVsCodeCommand(vscodepaths []string, args []string) ([]byte, error) {
errs := multierror.Append(nil)
for _, vscodepath := range vscodepaths {
out, err := runVsCodeCommand(vscodepath, args)
if err != nil {
errs = multierror.Append(errs, err)
} else {
return out, nil
}
}
return nil, breverrors.WrapAndTrace(errs.ErrorOrNil())
}
func runVsCodeCommand(vscodepath string, args []string) ([]byte, error) {
cmd := exec.Command(vscodepath, args...) // #nosec G204
res, err := cmd.CombinedOutput()
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
return res, nil
}
var commonVSCodePaths = []string{
"code",
"/Applications/Visual Studio Code.app/Contents/Resources/app/bin/code",
"/usr/bin/code",
"/usr/local/bin/code",
"/snap/bin/code",
"/usr/local/share/code/bin/code",
"/usr/share/code/bin/code",
"/usr/share/code-insiders/bin/code-insiders",
"/usr/share/code-oss/bin/code-oss",
"/usr/share/code/bin/code",
}