forked from StanfordSNR/guardian-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
166 lines (139 loc) · 3.16 KB
/
ui.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
package guardianagent
import (
"bytes"
"fmt"
"os"
"os/exec"
"strconv"
"strings"
"sync"
"github.com/howeyc/gopass"
i "github.com/sternhenri/interact"
)
type UI interface {
Ask(prompt Prompt) (int, error)
Confirm(msg string) bool
Inform(msg string)
Alert(msg string)
AskPassword(msg string) (string, error)
}
type FancyTerminalUI struct {
mu sync.Mutex
}
type AskPassUI struct {
askPass string
}
type Prompt struct {
Question string
Choices []string
}
func formatPrompt(params Prompt) (formattedPrompt string) {
var buf bytes.Buffer
buf.WriteString(params.Question)
for i, v := range params.Choices {
buf.WriteString(fmt.Sprintf("\n %d) %s", i+1, v))
}
buf.WriteString("\n\nAnswer (enter a number): ")
formattedPrompt = buf.String()
return
}
func mapToChoice(vs []string) []i.Choice {
vsm := make([]i.Choice, len(vs))
for j, v := range vs {
vsm[j] = i.Choice{Text: v}
}
return vsm
}
func (tui *FancyTerminalUI) Ask(params Prompt) (reply int, err error) {
tui.mu.Lock()
defer tui.mu.Unlock()
var resp int64
i.Run(&i.Interact{
Questions: []*i.Question{
{
Quest: i.Quest{
Msg: params.Question,
Choices: i.Choices{
Alternatives: mapToChoice(params.Choices),
},
},
Action: func(c i.Context) interface{} {
resp, _ = c.Ans().Int()
return nil
},
},
},
})
reply = int(resp)
return
}
func (tui *FancyTerminalUI) Inform(msg string) {
tui.mu.Lock()
defer tui.mu.Unlock()
fmt.Println(msg)
}
func (tui *FancyTerminalUI) Alert(msg string) {
tui.mu.Lock()
defer tui.mu.Unlock()
fmt.Fprintln(os.Stderr, msg)
}
func (tui *FancyTerminalUI) AskPassword(msg string) (string, error) {
tui.mu.Lock()
defer tui.mu.Unlock()
fmt.Println(msg)
passBytes, err := gopass.GetPasswd()
if err == nil {
return string(passBytes), nil
}
return "", err
}
func (tui *FancyTerminalUI) Confirm(msg string) bool {
prompt := Prompt{Question: msg, Choices: []string{"Yes", "No"}}
ans, err := tui.Ask(prompt)
return err == nil && ans == 1
}
func NewAskPassUI() *AskPassUI {
askPass := os.Getenv("SSH_ASKPASS")
if askPass != "" {
return &AskPassUI{askPass: askPass}
}
return &AskPassUI{askPass: "ssh-askpass"}
}
func (aui *AskPassUI) Ask(params Prompt) (reply int, err error) {
reply = -1
var convErr error
for convErr != nil || reply <= 0 || reply > len(params.Choices) { // 1 indexed
cmd := exec.Command(aui.askPass, formatPrompt(params))
out, err := cmd.Output()
if err != nil {
return reply, err
}
sReply := strings.TrimSpace(string(out))
reply, convErr = strconv.Atoi(sReply)
}
return
}
func (aui *AskPassUI) Inform(msg string) {
fmt.Println(msg)
}
func (aui *AskPassUI) Alert(msg string) {
cmd := exec.Command(aui.askPass, msg)
cmd.Run()
}
func (aui *AskPassUI) AskPassword(msg string) (string, error) {
cmd := exec.Command(aui.askPass, msg)
out, err := cmd.Output()
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
func (aui *AskPassUI) Confirm(msg string) bool {
cmd := exec.Command(aui.askPass, msg)
out, err := cmd.Output()
if err != nil {
return false
}
outStr := strings.ToLower(strings.TrimSpace(string(out)))
return len(outStr) == 0 || outStr == "yes"
}