-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
117 lines (94 loc) · 2.43 KB
/
cli.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package awslogin
import (
"strconv"
"strings"
"time"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/youyo/awslogin/browse"
"github.com/youyo/awsprofile"
)
// Run
func Run(cmd *cobra.Command, args []string) (err error) {
profile := viper.GetString("profile")
durationSeconds := viper.GetInt("duration_seconds")
browser := viper.GetString("browser")
outputUrl := viper.GetBool("output-url")
awsSession := NewAwsSession(profile, time.Duration(durationSeconds)*time.Second)
temporaryCredentials, err := BuildTemporaryCredentials(awsSession)
if err != nil {
return err
}
requestUrl := BuildSigninTokenRequestURL(temporaryCredentials, strconv.Itoa(durationSeconds))
signinToken, err := RequestSigninToken(requestUrl)
if err != nil {
return err
}
signinUrl := BuildSigninURL(signinToken, *awsSession.Config.Region)
if outputUrl {
cmd.Println(signinUrl)
return nil
}
if browser != "" {
browse.StartWith(signinUrl, browser)
} else {
browse.Start(signinUrl)
}
return nil
}
func PreRun(cmd *cobra.Command, args []string) (err error) {
selectProfile := viper.GetBool("select-profile")
configs := awsprofile.NewConfigs()
file, err := awsprofile.GetConfigsPath()
if err != nil {
return err
}
if err := configs.Parse(file); err != nil {
return err
}
if selectProfile {
profiles, err := configs.ProfileNames()
if err != nil {
return err
}
prompt := promptui.Select{
Label: "Profiles",
Templates: &promptui.SelectTemplates{
Label: `{{ . | green }}`,
Active: `{{ ">" | blue }} {{ . | red }}`,
Inactive: `{{ . | cyan }}`,
Selected: `{{ . | yellow }}`,
},
Items: profiles,
Size: 25,
Searcher: func(input string, index int) bool {
item := profiles[index]
profileName := strings.Replace(strings.ToLower(item), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
if strings.Contains(profileName, input) {
return true
}
return false
},
StartInSearchMode: true,
}
index, _, err := prompt.Run()
if err != nil {
return err
}
viper.Set("profile", profiles[index])
}
for _, config := range *configs {
if config.ProfileName == viper.GetString("profile") {
durationSeconds := config.GetDurationSeconds()
if durationSeconds != 0 {
viper.Set("duration_seconds", durationSeconds)
} else {
viper.Set("duration_seconds", 3600)
}
break
}
}
return nil
}