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
|
package interaction
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strings"
"sync"
"syscall"
)
type dialogInteractor struct{}
// Invokes a dialog program to create terminal dialog boxes. Fails if no such
// program is available.
var Dialog Interactor = dialogInteractor{}
type dialogStatusSink struct {
closeChan chan struct{}
closeOnce sync.Once
closedChan chan struct{}
updateChan chan struct{}
pipeW *os.File
infoMutex sync.Mutex
statusLine string
progress int
cmd *exec.Cmd
}
func (ss *dialogStatusSink) Close() error {
ss.closeOnce.Do(func() {
close(ss.closeChan)
})
<-ss.closedChan
return nil
}
func (ss *dialogStatusSink) SetProgress(n, ofM int) {
ss.infoMutex.Lock()
defer ss.infoMutex.Unlock()
ss.progress = int((float64(n) / float64(ofM)) * 100)
ss.notify()
}
func (ss *dialogStatusSink) SetStatusLine(status string) {
ss.infoMutex.Lock()
defer ss.infoMutex.Unlock()
ss.statusLine = status
ss.notify()
}
func (ss *dialogStatusSink) notify() {
select {
case ss.updateChan <- struct{}{}:
default:
}
}
func (ss *dialogStatusSink) loop() {
A:
for {
select {
case <-ss.closeChan:
break A
case <-ss.updateChan:
ss.infoMutex.Lock()
statusLine := ss.statusLine
progress := ss.progress
ss.infoMutex.Unlock()
fmt.Fprintf(ss.pipeW, "XXX\n%d\n%s\nXXX\n", progress, statusLine)
}
}
ss.pipeW.Close()
ss.cmd.Wait()
close(ss.closedChan)
}
func (dialogInteractor) Status(c *StatusInfo) (StatusSink, error) {
cmdName, _ := findDialogCommand()
if cmdName == "" {
return nil, fmt.Errorf("cannot find whiptail or dialog binary in path")
}
width := "78"
height := fmt.Sprintf("%d", strings.Count(c.StatusLine, "\n")+5)
var opts []string
if c.Title != "" {
opts = append(opts, "--backtitle", "ACME", "--title", c.Title)
}
opts = append(opts, "--gauge", c.StatusLine, height, width, "0")
pipeR, pipeW, err := os.Pipe()
if err != nil {
return nil, err
}
defer pipeR.Close()
cmd := exec.Command(cmdName, opts...)
cmd.Stdin = pipeR
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Start()
if err != nil {
pipeW.Close()
return nil, err
}
ss := &dialogStatusSink{
closeChan: make(chan struct{}),
closedChan: make(chan struct{}),
updateChan: make(chan struct{}, 10),
pipeW: pipeW,
cmd: cmd,
}
go ss.loop()
return ss, nil
}
func (dialogInteractor) Prompt(c *Challenge) (*Response, error) {
cmdName, cmdType := findDialogCommand()
if cmdName == "" {
return nil, fmt.Errorf("cannot find whiptail or dialog binary in path")
}
width := "78"
height := "49"
yesLabelArg := "--yes-label"
noLabelArg := "--no-label"
noTagsArg := "--no-tags"
if cmdType == "whiptail" {
yesLabelArg = "--yes-button"
noLabelArg = "--no-button"
noTagsArg = "--notags"
}
var opts []string
if c.Title != "" {
opts = append(opts, "--backtitle", "ACME", "--title", c.Title)
}
var err error
var pipeR *os.File
var pipeW *os.File
switch c.ResponseType {
case RTAcknowledge:
opts = append(opts, "--msgbox", c.Body, height, width)
case RTYesNo:
yesLabel := c.YesLabel
if yesLabel == "" {
yesLabel = "Yes"
}
noLabel := c.NoLabel
if noLabel == "" {
noLabel = "No"
}
opts = append(opts, yesLabelArg, yesLabel, noLabelArg, noLabel, "--yesno", c.Body, height, width)
case RTLineString:
pipeR, pipeW, err = os.Pipe()
if err != nil {
return nil, err
}
defer pipeR.Close()
defer pipeW.Close()
opts = append(opts, "--output-fd", "3", "--inputbox", c.Body, height, width)
case RTSelect:
pipeR, pipeW, err = os.Pipe()
if err != nil {
return nil, err
}
defer pipeR.Close()
defer pipeW.Close()
opts = append(opts, "--output-fd", "3", noTagsArg, "--menu", c.Body, height, width, "5")
for _, o := range c.Options {
opts = append(opts, o.Value, o.Title)
}
}
cmd := exec.Command(cmdName, opts...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if pipeW != nil {
cmd.ExtraFiles = append(cmd.ExtraFiles, pipeW)
}
rc, xerr, err := runCommand(cmd)
if err != nil {
return nil, err
}
// If we get error code >1 (particularly 255) the dialog command probably
// doesn't support some option we pass it. Return an error, which should make
// us fall back to stdio.
if rc > 1 {
return nil, xerr
}
res := &Response{}
if pipeW != nil {
pipeW.Close()
}
switch c.ResponseType {
case RTLineString, RTSelect:
b, err := ioutil.ReadAll(pipeR)
if err != nil {
return nil, err
}
res.Value = string(b)
fallthrough
case RTYesNo, RTAcknowledge:
if rc != 0 && rc != 1 {
return nil, xerr
}
res.Cancelled = (rc == 1)
}
return res, nil
}
var dialogCommand = ""
var dialogCommandType = ""
func findDialogCommand() (string, string) {
if dialogCommand != "" {
return dialogCommand, dialogCommandType
}
// not using whiptail for now, see #18
for _, s := range []string{"dialog"} {
p, err := exec.LookPath(s)
if err == nil {
dialogCommand = p
dialogCommandType = s
return dialogCommand, dialogCommandType
}
}
return "", ""
}
func runCommand(cmd *exec.Cmd) (int, error, error) {
err := cmd.Run()
if err == nil {
return 0, nil, nil
}
if e, ok := err.(*exec.ExitError); ok {
if ws, ok := e.Sys().(syscall.WaitStatus); ok {
return ws.ExitStatus(), err, nil
}
}
return 255, err, err
}
|