forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
86 lines (62 loc) · 1.84 KB
/
input.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
package saml2aws
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/segmentio/go-prompt"
)
// LoginDetails used to authenticate to ADFS
type LoginDetails struct {
Username string
Password string
Hostname string
}
// PromptForLoginDetails prompt the user to present their username, password and hostname
func PromptForLoginDetails(username, hostname string) (*LoginDetails, error) {
hostname = promptFor("Hostname [%s]", hostname)
username = promptFor("Username [%s]", username)
password := prompt.PasswordMasked("Password")
fmt.Println("")
return &LoginDetails{
Username: strings.TrimSpace(username),
Password: strings.TrimSpace(password),
Hostname: strings.TrimSpace(hostname),
}, nil
}
// PromptForAWSRoleSelection present a list of roles to the user for selection
func PromptForAWSRoleSelection(roles []string) (*AWSRole, error) {
if len(roles) == 1 {
tok := strings.Split(roles[0], ",")
return &AWSRole{tok[0], tok[1]}, nil
}
reader := bufio.NewReader(os.Stdin)
fmt.Println("Please choose the role you would like to assume: ")
for i, role := range roles {
fmt.Println("[", i, "]: ", strings.Split(role, ",")[1])
}
fmt.Print("Selection: ")
selectedroleindex, _ := reader.ReadString('\n')
v, err := strconv.Atoi(strings.TrimSpace(selectedroleindex))
if err != nil {
return nil, fmt.Errorf("Unrecognised role index")
}
if v > len(roles) {
return nil, fmt.Errorf("You selected an invalid role index")
}
selectedRole := roles[v]
tok := strings.Split(selectedRole, ",")
return &AWSRole{tok[1], tok[0]}, nil
}
func promptFor(promptString, defaultValue string) string {
var val string
// do while
for ok := true; ok; ok = strings.TrimSpace(defaultValue) == "" && strings.TrimSpace(val) == "" {
val = prompt.String(promptString, defaultValue)
}
if val == "" {
val = defaultValue
}
return val
}