-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.go
172 lines (146 loc) · 2.97 KB
/
shell.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package tlkn
import (
"bytes"
"context"
"crypto/sha256"
"encoding/base64"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"text/template"
)
// Debug will enable debug logging
var Debug bool
// ExitError alias
type ExitError = exec.ExitError
func logDebug(v ...interface{}) {
if !Debug {
return
}
fmt.Println("[tlkn debug]\n", fmt.Sprint(v...))
}
func Tmpl(tmpl []byte, a interface{}) []byte {
// hash template to allow for caching
hash := sha256.New()
hash.Write(tmpl)
sha := string(base64.URLEncoding.EncodeToString(hash.Sum(nil)))
t, err := template.New(sha).Parse(string(tmpl))
if err != nil {
os.Stderr.WriteString("Error: could not compile bash template\n")
return nil
}
b := bytes.Buffer{}
t.Execute(&b, a)
return b.Bytes()
}
// BashCmd creates a *exec.Cmd using the given bash command string
// writes to os.Stderr and os.Stdout by default
func BashCmd(ctx context.Context, cmd string) *exec.Cmd {
bash := exec.CommandContext(ctx, "bash", "-c", trimLefts(cmd))
bash.Stderr = os.Stderr
bash.Stdout = os.Stdout
return bash
}
func Parallel(fns ...func() error) error {
errCh := make(chan error)
var wg sync.WaitGroup
for _, fn := range fns {
wg.Add(1)
go func(fn func() error) {
err := fn()
if err != nil {
errCh <- err
}
wg.Done()
}(fn)
}
go func() {
wg.Wait()
close(errCh)
}()
for err := range errCh {
return err
}
return nil
}
// Prompt prompts user for input with default value.
func Prompt(prompt string, args ...interface{}) string {
var s string
fmt.Printf(prompt+": ", args...)
fmt.Scanln(&s)
return s
}
// PromptRequired prompts user for input with default value and requires an input.
func PromptRequired(prompt string, args ...interface{}) (s string) {
for strings.Trim(s, " ") == "" {
s = Prompt(prompt, args...)
}
return s
}
// PromptConfirm continues prompting until the input is boolean-ish.
func PromptConfirm(prompt string, args ...interface{}) bool {
for {
switch Prompt(prompt, args...) {
case "Yes", "yes", "y", "Y":
return true
case "No", "no", "n", "N":
return false
}
}
}
// PromptChoose prompts for a single selection from `list`, returning in the index.
func PromptChoose(prompt string, list []string) int {
fmt.Println()
for i, val := range list {
fmt.Printf(" %d) %s\n", i+1, val)
}
fmt.Println()
i := -1
for {
s := Prompt(prompt)
// index
n, err := strconv.Atoi(s)
if err == nil {
if n > 0 && n <= len(list) {
i = n - 1
break
} else {
continue
}
}
// value
i = indexOf(s, list)
if i != -1 {
break
}
}
return i
}
func trimLefts(s string) string {
nl := true
return strings.Map(func(r rune) rune {
// is space or tab at start of line
if nl && r == 0x0020 || r == 0x0009 {
return -1
}
// is newline
if r == 0x000A {
nl = true
return r
}
nl = false
return r
}, s)
}
// index of `s` in `list`.
func indexOf(s string, list []string) int {
for i, val := range list {
if val == s {
return i
}
}
return -1
}