@@ -26,6 +26,7 @@ import (
26
26
"time"
27
27
28
28
"github.com/manifoldco/promptui"
29
+ "github.com/synfinatic/aws-sso-cli/internal/url"
29
30
"github.com/synfinatic/aws-sso-cli/internal/utils"
30
31
"github.com/synfinatic/aws-sso-cli/sso"
31
32
)
@@ -101,12 +102,7 @@ func (cc *ConfigCmd) Run(ctx *RunContext) error {
101
102
}
102
103
103
104
func setupWizard (ctx * RunContext , reconfig , addSSO bool ) error {
104
- var instanceName , startHostname , ssoRegion , defaultRegion , urlAction string
105
- var defaultLevel , firefoxBrowserPath , browser , configProfilesUrlAction string
106
- var hLimit , hMinutes , cacheRefresh int64
107
- var consoleDuration int32
108
- var autoConfigCheck bool
109
- var urlExecCommand []string
105
+ var s = ctx .Settings
110
106
111
107
// Don't run setup twice
112
108
if ranSetup {
@@ -123,110 +119,81 @@ func setupWizard(ctx *RunContext, reconfig, addSSO bool) error {
123
119
` )
124
120
125
121
if reconfig {
126
- defaultLevel = ctx .Settings .LogLevel
127
- defaultRegion = ctx .Settings .DefaultRegion
128
- urlAction = ctx .Settings .UrlAction
129
- urlExecCommand = ctx .Settings .UrlExecCommand
130
- if ctx .Settings .FirefoxOpenUrlInContainer {
131
- firefoxBrowserPath = urlExecCommand [0 ]
122
+ // migrate old boolean flag to enum
123
+ if s .FirefoxOpenUrlInContainer {
124
+ s .UrlAction = url .OpenUrlContainer
132
125
}
133
- autoConfigCheck = ctx .Settings .AutoConfigCheck
134
- cacheRefresh = ctx .Settings .CacheRefresh
135
- hLimit = ctx .Settings .HistoryLimit
136
- hMinutes = ctx .Settings .HistoryMinutes
137
- browser = ctx .Settings .Browser
138
- consoleDuration = ctx .Settings .ConsoleDuration
139
126
140
127
// upgrade deprecated config option
141
- configProfilesUrlAction = ctx .Settings .ConfigProfilesUrlAction
142
- if ctx .Settings .ConfigUrlAction != "" && configProfilesUrlAction == "" {
143
- configProfilesUrlAction = ctx .Settings .ConfigUrlAction
144
- ctx .Settings .ConfigUrlAction = ""
128
+ if s .ConfigUrlAction != "" && s .ConfigProfilesUrlAction == "" {
129
+ s .ConfigProfilesUrlAction , _ = url .NewConfigProfilesAction (s .ConfigUrlAction )
130
+ s .ConfigUrlAction = ""
145
131
}
146
132
// skips:
147
133
// - SSORegion
148
134
// - DefaultRegion
149
135
// - StartUrl/startHostname
150
136
// - InstanceName
151
137
} else {
152
- hMinutes = 1440
153
- hLimit = 10
154
- defaultLevel = "warn"
155
- }
156
-
157
- if err := logLevelValidate ( defaultLevel ); err != nil {
158
- log . Fatalf ( "Invalid value for --default-level %s" , defaultLevel )
159
- }
160
-
161
- if ! reconfig {
162
- instanceName = promptSsoInstance ( "" )
163
- startHostname = promptStartUrl ( "" )
164
- ssoRegion = promptAwsSsoRegion ( "" )
165
- defaultRegion = promptDefaultRegion ( defaultRegion )
166
-
167
- ctx . Settings = & sso. Settings {}
168
- ctx . Settings . SSO = map [ string ] * sso. SSOConfig { }
138
+ instanceName := promptSsoInstance ( "" )
139
+ startHostname := promptStartUrl ( "" )
140
+ ssoRegion := promptAwsSsoRegion ( "" )
141
+ defaultRegion := promptDefaultRegion ( ssoRegion )
142
+
143
+ s = & sso. Settings {
144
+ SSO : map [ string ] * sso. SSOConfig {},
145
+ UrlAction : "open" ,
146
+ LogLevel : "error" ,
147
+ DefaultRegion : defaultRegion ,
148
+ ConsoleDuration : 60 ,
149
+ CacheRefresh : 168 ,
150
+ AutoConfigCheck : false ,
151
+ HistoryLimit : 10 ,
152
+ HistoryMinutes : 1440 ,
153
+ UrlExecCommand : [] string {},
154
+ }
169
155
170
- ctx . Settings .SSO [instanceName ] = & sso.SSOConfig {
156
+ s .SSO [instanceName ] = & sso.SSOConfig {
171
157
SSORegion : ssoRegion ,
172
158
StartUrl : fmt .Sprintf (START_URL_FORMAT , startHostname ),
173
159
DefaultRegion : defaultRegion ,
174
160
}
175
- consoleDuration = 60
176
- cacheRefresh = 168
177
- autoConfigCheck = false
178
- hLimit = 10
179
- hMinutes = 1440
180
- urlAction = "open"
181
- defaultLevel = "error"
182
- } else if reconfig {
183
- // don't do anything with the SSO for reconfig
184
- } else if addSSO {
185
- log .Errorf ("sorry, not supported yet" )
186
161
}
187
162
188
- ctx .Settings .CacheRefresh = promptCacheRefresh (cacheRefresh )
163
+ // first, caching
164
+ s .CacheRefresh = promptCacheRefresh (s .CacheRefresh )
189
165
190
- if ctx . Settings .CacheRefresh > 0 {
191
- ctx . Settings . AutoConfigCheck = promptAutoConfigCheck (autoConfigCheck )
166
+ if s .CacheRefresh > 0 {
167
+ s . AutoConfigCheck = promptAutoConfigCheck (s . AutoConfigCheck )
192
168
}
193
169
194
- // First check if using Firefox w/ Containers
195
- firefoxBrowserPath = promptUseFirefox (firefoxBrowserPath )
170
+ // next how we open URLs
171
+ s .UrlAction = promptUrlAction (s .UrlAction )
172
+ s .ConfigProfilesUrlAction = promptConfigProfilesUrlAction (s .ConfigProfilesUrlAction , s .UrlAction )
196
173
197
- // if yes, then configure urlAction = 'exec' and our UrlExecCommand
198
- if firefoxBrowserPath != "" {
199
- ctx .Settings .FirefoxOpenUrlInContainer = true
200
- ctx .Settings .UrlAction = "exec"
201
- ctx .Settings .UrlExecCommand = []string {
202
- firefoxBrowserPath ,
203
- `%s` ,
204
- }
174
+ // do we need urlExecCommand?
175
+ if s .UrlAction == url .Exec {
176
+ s .UrlExecCommand = promptUrlExecCommand (s .UrlExecCommand )
177
+ } else if s .UrlAction .IsContainer () {
178
+ s .UrlExecCommand = promptUseFirefox (s .UrlExecCommand )
205
179
} else {
206
- // Not using firefox containers...
207
- ctx .Settings .FirefoxOpenUrlInContainer = false
208
- ctx .Settings .UrlAction = promptUrlAction (urlAction , ctx .Settings .FirefoxOpenUrlInContainer )
180
+ s .UrlExecCommand = []string {}
209
181
}
210
182
211
- ctx .Settings .ConfigProfilesUrlAction = promptConfigProfilesUrlAction (
212
- configProfilesUrlAction , ctx .Settings .UrlAction , ctx .Settings .FirefoxOpenUrlInContainer )
213
-
214
183
// should we prompt user to override default browser?
215
- if ctx . Settings . UrlAction == "open" || ctx . Settings . ConfigProfilesUrlAction == "open" {
216
- ctx . Settings . Browser = promptDefaultBrowser (browser )
184
+ if s . UrlAction == url . Open || s . ConfigProfilesUrlAction == url . Open {
185
+ s . Browser = promptDefaultBrowser (s . Browser )
217
186
}
218
187
219
- // Does either action call `exec` without firefox containers?
220
- if ctx .Settings .UrlAction == "exec" || ctx .Settings .ConfigProfilesUrlAction == "exec" {
221
- if ! ctx .Settings .FirefoxOpenUrlInContainer {
222
- ctx .Settings .UrlExecCommand = promptUrlExecCommand (urlExecCommand )
223
- }
188
+ s .ConsoleDuration = promptConsoleDuration (s .ConsoleDuration )
189
+ s .HistoryLimit = promptHistoryLimit (s .HistoryLimit )
190
+ s .HistoryMinutes = promptHistoryMinutes (s .HistoryMinutes )
191
+ s .LogLevel = promptLogLevel (s .LogLevel )
192
+
193
+ if err := s .Validate (); err != nil {
194
+ return err
224
195
}
225
196
226
- ctx .Settings .ConsoleDuration = promptConsoleDuration (consoleDuration )
227
- ctx .Settings .HistoryLimit = promptHistoryLimit (hLimit )
228
- ctx .Settings .HistoryMinutes = promptHistoryMinutes (hMinutes )
229
- ctx .Settings .LogLevel = promptLogLevel (defaultLevel )
230
197
fmt .Printf ("\n Awesome! Saving the new %s\n " , ctx .Cli .ConfigFile )
231
- return ctx . Settings .Save (ctx .Cli .ConfigFile , reconfig )
198
+ return s .Save (ctx .Cli .ConfigFile , reconfig )
232
199
}
0 commit comments