forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
100 lines (75 loc) · 2.16 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
package saml2aws
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/segmentio/go-prompt"
)
// LoginDetails used to authenticate to ADFS
type LoginDetails struct {
Username string
Password string
Hostname string
}
// Validate validate the login details
func (ld *LoginDetails) Validate() error {
if ld.Hostname == "" {
return errors.New("Missing hostname")
}
if ld.Username == "" {
return errors.New("Missing username")
}
if ld.Password == "" {
return errors.New("Missing password")
}
return nil
}
// PromptForLoginDetails prompt the user to present their username, password and hostname
func PromptForLoginDetails(loginDetails *LoginDetails) error {
loginDetails.Hostname = promptFor("Hostname [%s]", loginDetails.Hostname)
fmt.Println("To use saved username and password just hit enter.")
loginDetails.Username = promptFor("Username [%s]", loginDetails.Username)
if enteredPassword := prompt.PasswordMasked("Password"); enteredPassword != "" {
loginDetails.Password = enteredPassword
}
fmt.Println("")
return nil
}
// PromptForAWSRoleSelection present a list of roles to the user for selection
func PromptForAWSRoleSelection(accounts []*AWSAccount) (*AWSRole, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Println("Please choose the role you would like to assume: ")
roles := []*AWSRole{}
for _, account := range accounts {
fmt.Println(account.Name)
for _, role := range account.Roles {
fmt.Println("[", len(roles), "]: ", role.Name)
fmt.Println()
roles = append(roles, role)
}
}
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")
}
return roles[v], 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
}