-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
162 lines (125 loc) · 2.58 KB
/
utils.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
package main
import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/ysmood/whisper/lib/piper"
"golang.org/x/term"
)
func exit(err error) {
fmt.Fprintln(os.Stderr, "Error:", err.Error())
os.Exit(1)
}
func getPassphrase(location string) string {
return readPassphrase(fmt.Sprintf("Enter passphrase for private key %s: ", location))
}
func readPassphrase(prompt string) string {
fmt.Fprint(os.Stderr, prompt)
fd := int(os.Stdin.Fd())
if !term.IsTerminal(fd) {
exit(ErrUnableReadPassphrase)
}
inputPass, err := term.ReadPassword(fd)
if err != nil {
exit(err)
}
fmt.Fprintln(os.Stderr)
return string(inputPass)
}
func readLine(prompt string) string {
fmt.Fprint(os.Stderr, prompt)
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
if err != nil {
exit(err)
}
return strings.TrimSpace(line)
}
func getInput(path, defaultPath string) io.ReadCloser {
if path == "" {
path = defaultPath
}
if path == "" {
return os.Stdin
}
if strings.HasPrefix(path, "https://") {
res, err := http.Get(path) //nolint:noctx
if err != nil {
exit(err)
}
return res.Body
}
f, err := os.Open(path)
if err != nil {
exit(err)
}
return f
}
func getOutput(file string) io.WriteCloser {
if file == "" {
return piper.NopCloser(os.Stdout)
}
err := os.MkdirAll(filepath.Dir(file), 0o700)
if err != nil {
exit(err)
}
return newLazyFileWriter(file)
}
type publicKeysFlag []string
func (i *publicKeysFlag) String() string {
return strings.Join(*i, ", ")
}
func (i *publicKeysFlag) Set(value string) error {
*i = append(*i, value)
return nil
}
func isBase64(in io.Reader) bool {
dec := base64.NewDecoder(base64.StdEncoding, in)
buf := bytes.NewBuffer(nil)
_, err := io.Copy(buf, dec)
return err == nil
}
type lazyFileWriter struct {
path string
file *os.File
}
func (w *lazyFileWriter) Write(p []byte) (n int, err error) {
if w.file == nil {
w.file, err = os.OpenFile(w.path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0o600)
if err != nil {
return 0, err
}
}
return w.file.Write(p)
}
func (w *lazyFileWriter) Close() error {
if w.file != nil {
return w.file.Close()
}
return nil
}
func newLazyFileWriter(path string) *lazyFileWriter {
return &lazyFileWriter{path: path}
}
// remove duplicated strings in the sorted slice.
func uniqueStrings(sorted []string) []string {
if len(sorted) == 0 {
return sorted
}
last := sorted[0]
unique := []string{last}
for _, s := range sorted[1:] {
if s == last {
continue
}
last = s
unique = append(unique, s)
}
return unique
}