forked from Versent/saml2aws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
input.go
68 lines (48 loc) · 1.45 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
package saml2aws
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
"syscall"
"golang.org/x/crypto/ssh/terminal"
)
// PromptForLoginCreds prompt the user to present their username and password
func PromptForLoginCreds() (*LoginCreds, error) {
reader := bufio.NewReader(os.Stdin)
fmt.Print("Enter Username: ")
username, _ := reader.ReadString('\n')
fmt.Print("Enter Password: ")
bytePassword, err := terminal.ReadPassword(int(syscall.Stdin))
if err != nil {
return nil, err
}
password := string(bytePassword)
fmt.Println("")
return &LoginCreds{strings.TrimSpace(username), strings.TrimSpace(password)}, 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
}