forked from iamacarpet/ssh-bastion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactive.go
executable file
·44 lines (37 loc) · 1.02 KB
/
interactive.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
package main
import (
"io"
"fmt"
"strconv"
"golang.org/x/crypto/ssh/terminal"
)
func InteractiveSelection(c io.ReadWriter, prompt string, choices []string) (string, error) {
t := terminal.NewTerminal(c, "Please Enter A Server ID: ")
fmt.Fprintf(c, "%s\r\n", prompt)
for i, v := range choices {
fmt.Fprintf(c, " [ %2d ] %s\r\n", i+1, v)
}
var ct int = 0
for {
// Only allow a maxmimum of 3 attempts.
if ct > 3 {
fmt.Fprintf(c, "Maximum Number of Attempts Reached\r\n")
return "", fmt.Errorf("Maximum Number of Attempts Reached")
} else {
ct += 1
}
sel, err := t.ReadLine()
if err != nil {
return "", err
}
i, err := strconv.Atoi(sel)
if err != nil {
continue
}
if (i < 0) || (i > len(choices)) {
continue
} else {
return choices[(i-1)], err
}
}
}