@@ -4,12 +4,8 @@ import (
4
4
"fmt"
5
5
"time"
6
6
7
- "github.com/null93/aws-knox/pkg/ansi"
8
- "github.com/null93/aws-knox/pkg/color"
9
7
"github.com/null93/aws-knox/sdk/credentials"
10
8
"github.com/null93/aws-knox/sdk/picker"
11
- . "github.com/null93/aws-knox/sdk/style"
12
- "github.com/pkg/browser"
13
9
"github.com/spf13/cobra"
14
10
)
15
11
@@ -56,47 +52,18 @@ var selectCredentialsCmd = &cobra.Command{
56
52
ExitWithError (4 , "session with passed name not found" , err )
57
53
}
58
54
if session .ClientToken == nil || session .ClientToken .IsExpired () {
59
- if err := session .RegisterClient (); err != nil {
60
- ExitWithError (5 , "failed to register client" , err )
61
- }
62
- userCode , deviceCode , url , urlFull , err := session .StartDeviceAuthorization ()
63
- if err != nil {
64
- ExitWithError (6 , "failed to start device authorization" , err )
65
- }
66
- yellow := color .ToForeground (YellowColor ).Decorator ()
67
- gray := color .ToForeground (LightGrayColor ).Decorator ()
68
- title := TitleStyle .Decorator ()
69
- DefaultStyle .Printfln ("" )
70
- DefaultStyle .Printfln ("%s %s" , title ("SSO Session: " ), gray (session .Name ))
71
- DefaultStyle .Printfln ("%s %s" , title ("SSO Start URL: " ), gray (session .StartUrl ))
72
- DefaultStyle .Printfln ("%s %s" , title ("Authorization URL:" ), gray (url ))
73
- DefaultStyle .Printfln ("%s %s" , title ("Device Code: " ), yellow (userCode ))
74
- DefaultStyle .Printfln ("" )
75
- DefaultStyle .Printf ("Waiting for authorization to complete..." )
76
- err = browser .OpenURL (urlFull )
77
- if err != nil {
78
- ansi .MoveCursorUp (6 )
79
- ansi .ClearDown ()
80
- ExitWithError (7 , "failed to open url in browser" , err )
81
- }
82
- err = session .WaitForToken (deviceCode )
83
- ansi .MoveCursorUp (6 )
84
- ansi .ClearDown ()
85
- if err != nil {
86
- ExitWithError (8 , "failed to wait for token" , err )
87
- }
88
- err = session .Save ()
55
+ err := ClientLogin (session )
89
56
if err != nil {
90
- ExitWithError (9 , "failed to save session " , err )
57
+ ExitWithError (5 , "failed to authorize device login " , err )
91
58
}
92
59
}
93
60
if accountId == "" {
94
61
accountIds , err := session .GetAccounts ()
95
62
if err != nil {
96
- ExitWithError (10 , "failed to get account ids" , err )
63
+ ExitWithError (6 , "failed to get account ids" , err )
97
64
}
98
65
if len (accountIds ) == 0 {
99
- ExitWithError (11 , "no accounts found" , err )
66
+ ExitWithError (7 , "no accounts found" , err )
100
67
}
101
68
p := picker .NewPicker ()
102
69
p .WithMaxHeight (5 )
@@ -108,14 +75,14 @@ var selectCredentialsCmd = &cobra.Command{
108
75
}
109
76
selection := p .Pick ()
110
77
if selection == nil {
111
- ExitWithError (12 , "failed to pick an account id" , err )
78
+ ExitWithError (8 , "failed to pick an account id" , err )
112
79
}
113
80
accountId = selection .Value .(string )
114
81
}
115
82
roles , err := session .GetRoles (accountId )
116
83
if roleName == "" {
117
84
if err != nil {
118
- ExitWithError (13 , "failed to get roles" , err )
85
+ ExitWithError (9 , "failed to get roles" , err )
119
86
}
120
87
p := picker .NewPicker ()
121
88
p .WithMaxHeight (5 )
@@ -131,30 +98,30 @@ var selectCredentialsCmd = &cobra.Command{
131
98
}
132
99
selection := p .Pick ()
133
100
if selection == nil {
134
- ExitWithError (14 , "failed to pick a role name" , err )
101
+ ExitWithError (10 , "failed to pick a role name" , err )
135
102
}
136
103
roleName = selection .Value .(string )
137
104
}
138
105
role := roles .FindByName (roleName )
139
106
if role == nil {
140
- ExitWithError (15 , "role with passed name not found" , err )
107
+ ExitWithError (11 , "role with passed name not found" , err )
141
108
}
142
109
if role .Credentials == nil || role .Credentials .IsExpired () {
143
110
err := session .RefreshRoleCredentials (role )
144
111
if err != nil {
145
- ExitWithError (16 , "failed to get credentials" , err )
112
+ ExitWithError (12 , "failed to get credentials" , err )
146
113
}
147
114
err = role .Credentials .Save (session .Name , role .CacheKey ())
148
115
if err != nil {
149
- ExitWithError (17 , "failed to save credentials" , err )
116
+ ExitWithError (13 , "failed to save credentials" , err )
150
117
}
151
118
}
152
119
if err := role .MarkLastUsed (); err != nil {
153
- ExitWithError (18 , "failed to mark last used role" , err )
120
+ ExitWithError (14 , "failed to mark last used role" , err )
154
121
}
155
122
json , err := role .Credentials .ToJSON ()
156
123
if err != nil {
157
- ExitWithError (19 , "failed to convert credentials to json" , err )
124
+ ExitWithError (15 , "failed to convert credentials to json" , err )
158
125
}
159
126
fmt .Println (json )
160
127
},
0 commit comments