/
lib.go
95 lines (78 loc) · 1.65 KB
/
lib.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
package run
import (
"fmt"
"strings"
"unicode"
"strconv"
"github.com/howeyc/gopass"
)
func getQuoteSplitter(sep rune) func(rune) bool{
lastQuote := rune(0)
return func(c rune) bool {
switch {
// quote end
case c == lastQuote:
lastQuote = rune(0)
return false
// quoted text
case lastQuote != rune(0):
return false
// quote start
case unicode.In(c, unicode.Quotation_Mark):
lastQuote = c
return false
// unquoted text, need split
default:
if sep!=rune(0){
return c==sep
}else{
// use space as default separator
return unicode.IsSpace(c)
}
}
}
}
func parseCommand(command string) (exe string, arg, env []string) {
for _, a:= range strings.FieldsFunc(command, getQuoteSplitter(rune(0))){
if unq,err:= strconv.Unquote(a);err!=nil{
arg = append(arg, a)
}else{
arg = append(arg, unq)
}
}
for i, item := range arg {
if strings.Contains(item, "=") {
if env == nil {
env = []string{item}
continue
}
env = append(env, item)
} else {
// end of env prefix, return tralling command
exe = item
arg = arg[i+1:]
return
}
}
exe = arg[0]
arg = arg[1:]
return
}
// Prompt prompts user for input with plain value.
func Prompt(prompt string) string {
fmt.Print(prompt)
var input string
fmt.Scanln(&input)
fmt.Println(input)
return input
}
// PromptHidden prompts user for hidden terminal input.
func PromptHidden(prompt string) string {
fmt.Print(prompt)
return string(gopass.GetPasswd())
}
// PromptMasked prompts user for masked terminal input.
func PromptMasked(prompt string) string {
fmt.Print(prompt)
return string(gopass.GetPasswdMasked())
}