-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
291 lines (258 loc) · 6.92 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package main
import (
"bufio"
"fmt"
"mime"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/flytam/filenamify"
"github.com/gofrs/uuid"
"github.com/mitchellh/go-wordwrap"
)
/*
fileExists checks if file already exists.
*/
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
/*
wrapString wraps (long) string for better readability.
*/
func wrapString(message string, width int, ident int) string {
wrapped := wordwrap.WrapString(message, uint(width-ident))
wrapped = strings.ReplaceAll(wrapped, "\n", "\n"+strings.Repeat(" ", ident))
return wrapped
}
/*
copyFile copies source file to destination file.
*/
func copyFile(sourceFile, destinationFile string) {
input, err := os.ReadFile(sourceFile)
if err != nil {
fmt.Printf("error [%v] at os.ReadFile()\n", err)
return
}
err = os.WriteFile(destinationFile, input, 0644)
if err != nil {
fmt.Printf("error [%v] at os.WriteFile()\n", err)
return
}
}
/*
pluralize pluralizes term (simply) by adding 's'.
*/
func pluralize(count int, singular string) string {
if count == 1 {
return singular
}
return singular + "s"
}
/*
runCommand runs a command or program.
*/
func runCommand(commandLine string) error {
parsedArgs := splitCommandLine(commandLine)
cmd := exec.Command(parsedArgs[0], parsedArgs[1:]...)
err := cmd.Run()
if err != nil {
fmt.Printf("error [%v] executing command [%v]\n", err, commandLine)
}
return err
}
/*
splitCommandLine splits command line into components (program, options, parameters).
*/
func splitCommandLine(commandLine string) []string {
var args []string
var inQuote bool
var quoteType rune // ' or "
var currentArg strings.Builder
for _, r := range commandLine {
switch {
case r == '"' || r == '\'':
if inQuote {
if quoteType == r {
inQuote = false
args = append(args, currentArg.String())
currentArg.Reset()
} else {
// Inside a quotation mark a different type is found, so treat it as part of the argument.
currentArg.WriteRune(r)
}
} else {
inQuote = true
quoteType = r
}
case r == ' ' && !inQuote:
if currentArg.Len() > 0 {
args = append(args, currentArg.String())
currentArg.Reset()
}
default:
currentArg.WriteRune(r)
}
}
// add remaining argument, if any
if currentArg.Len() > 0 {
args = append(args, currentArg.String())
}
return args
}
/*
getPassword gets password from pass phrase argument.
*/
func getPassword(passPhrase string) (string, error) {
items := strings.SplitN(passPhrase, ":", 2)
source := strings.ToLower(items[0])
password := ""
if len(items) != 2 {
return "", fmt.Errorf("unable to split pass phrase argument into 'source:password'")
}
switch source {
case "pass":
password = items[1]
case "env":
password = os.Getenv(items[1])
if password == "" {
return "", fmt.Errorf("password empty or env variable [%s] not found", items[1])
}
case "file":
// read password file
lines, err := slurpFile(items[1])
if err != nil || len(lines) == 0 {
return "", fmt.Errorf("unable to read password from file, error = [%w], file = [%v]", err, items[1])
}
password = lines[0]
default:
return "", fmt.Errorf("invalid password source (not 'pass:', 'env:' or 'file:')")
}
return password, nil
}
/*
slurpFile slurps all lines of a text file into a slice of strings.
*/
func slurpFile(filename string) ([]string, error) {
var lines []string
file, err := os.Open(filename)
if err != nil {
return lines, err
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
lines = append(lines, line)
}
return lines, nil
}
/*
getMimeType gets mime type for given file.
*/
func getMimeType(filename string) (string, error) {
file, err := os.Open(filename)
if err != nil {
return "", err
}
defer file.Close()
buffer := make([]byte, 512)
_, err = file.Read(buffer)
if err != nil {
// empty file = default mime type
if err.Error() == "EOF" {
return "application/octet-stream", nil
}
return "", err
}
mimeType := http.DetectContentType(buffer)
// if content type detection fails, resolve mime type based on file extension
if mimeType == "application/octet-stream" {
ext := filepath.Ext(filename)
mimeType = mime.TypeByExtension(ext)
if mimeType == "" {
// unknown mime type = default mime type
mimeType = "application/octet-stream"
}
}
return mimeType, nil
}
/*
promptToFilename derives valid filename from given prompt, prefix, postfix and extension.
*/
func promptToFilename(prompt string, maxLength int, prefix, postfix, extension string) string {
var filename string
var err error
// length correction for 'core' name
maxLength -= 2 // "[" + "]"
if prefix != "" {
maxLength -= len(prefix) + 1
}
if postfix != "" {
maxLength -= 1 + len(postfix)
}
if extension != "" {
maxLength -= 1 + len(extension)
}
// replace problematic characters with visuell similar runes
prompt = strings.ReplaceAll(prompt, "?", "ʔ") // glottal stop
prompt = strings.ReplaceAll(prompt, ":", "ː") // triangular colon
prompt = strings.ReplaceAll(prompt, "/", "∕") // division slash
prompt = strings.ReplaceAll(prompt, "\\", "\") // fullwidth reverse solidus
prompt = strings.ReplaceAll(prompt, "*", "⁎") // low asterisk
prompt = strings.ReplaceAll(prompt, "|", "¦") // broken bar
prompt = strings.ReplaceAll(prompt, "<", "‹") // single left-pointing angle quotation mark
prompt = strings.ReplaceAll(prompt, ">", "›") // single right-pointing angle quotation mark
prompt = strings.ReplaceAll(prompt, "\"", "”") // right double quotation mark
prompt = strings.ReplaceAll(prompt, ".", "․") // one dot leader
filename, err = filenamify.Filenamify(prompt, filenamify.Options{Replacement: " ", MaxLength: maxLength})
if err != nil {
fmt.Printf("error [%v] at filenamify.Filenamify()\n", err)
uuid4, _ := uuid.NewV4()
filename = uuid4.String()
}
filename = "[" + filename + "]"
if prefix != "" {
filename = prefix + "." + filename
}
if postfix != "" {
filename += "." + postfix
}
if extension != "" {
filename += "." + extension
}
return filename
}
/*
buildDestinationFilename builds destination filename based on given parameters and program configuration.
*/
func buildDestinationFilename(now time.Time, prompt, extension string) string {
formatLayout := "20060102-150405"
timestamp := now.Format(formatLayout)
destinationFilename := ""
switch progConfig.HistoryFilenameSchema {
case "prompt":
prefix := ""
if progConfig.HistoryFilenameAddPrefix {
prefix = timestamp
}
postfix := ""
if progConfig.HistoryFilenameAddPostfix {
postfix = timestamp
}
destinationFilename = promptToFilename(prompt, progConfig.HistoryMaxFilenameLength, prefix, postfix, extension)
case "timestamp":
destinationFilename = timestamp
if extension != "" {
destinationFilename += "." + extension
}
}
return destinationFilename
}