generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 9
/
util.go
108 lines (93 loc) · 2.35 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
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"
"golang.org/x/text/encoding/charmap"
)
// 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 IsVSCodeExtensionInstalled(extensionID string) (bool, error) {
cmdddd := exec.Command("code", "--list-extensions") // #nosec G204
in, err := cmdddd.Output()
if err != nil {
return false, breverrors.WrapAndTrace(err)
}
d := charmap.CodePage850.NewDecoder()
out, err := d.Bytes(in)
if err != nil {
return false, breverrors.WrapAndTrace(err)
}
return strings.Contains(string(out), extensionID), nil
}