@@ -184,17 +184,47 @@ func writeCredsProfile(credsPath string) error {
184184 return upsertSection (credsPath , credsSectionName , credentialsDefaults ())
185185}
186186
187- // Setup checks for the localstack AWS profile and prompts to create or update it if needed.
188- // resolvedHost must be a host:port string (e.g. "localhost.localstack.cloud:4566").
189- // In non-interactive mode, emits a note instead of prompting.
190- func Setup (ctx context.Context , sink output.Sink , interactive bool , resolvedHost string ) error {
187+ func emitMissingProfileNote (sink output.Sink ) {
188+ output .EmitNote (sink , "LocalStack AWS profile is incomplete. Run 'lstk setup aws'." )
189+ }
190+
191+ func needsProfileSetup (resolvedHost string ) (profileStatus , error ) {
191192 configPath , credsPath , err := awsPaths ()
192193 if err != nil {
193- output .EmitWarning (sink , fmt .Sprintf ("could not determine AWS config paths: %v" , err ))
194- return nil
194+ return profileStatus {}, err
195195 }
196196
197197 status , err := checkProfileStatus (configPath , credsPath , resolvedHost )
198+ if err != nil {
199+ return profileStatus {}, err
200+ }
201+
202+ return status , nil
203+ }
204+
205+ func profilePresence () (configOK , credsOK bool , err error ) {
206+ configPath , credsPath , err := awsPaths ()
207+ if err != nil {
208+ return false , false , err
209+ }
210+
211+ configOK , err = sectionExists (configPath , configSectionName )
212+ if err != nil {
213+ return false , false , err
214+ }
215+ credsOK , err = sectionExists (credsPath , credsSectionName )
216+ if err != nil {
217+ return false , false , err
218+ }
219+
220+ return configOK , credsOK , nil
221+ }
222+
223+ // EnsureProfile checks for the LocalStack AWS profile and either emits a note when it is incomplete
224+ // or triggers the interactive setup flow.
225+ // resolvedHost must be a host:port string (e.g. "localhost.localstack.cloud:4566").
226+ func EnsureProfile (ctx context.Context , sink output.Sink , interactive bool , resolvedHost string ) error {
227+ status , err := needsProfileSetup (resolvedHost )
198228 if err != nil {
199229 output .EmitWarning (sink , fmt .Sprintf ("could not check AWS profile: %v" , err ))
200230 return nil
@@ -203,21 +233,52 @@ func Setup(ctx context.Context, sink output.Sink, interactive bool, resolvedHost
203233 return nil
204234 }
205235
206- if ! interactive {
207- output .EmitNote (sink , fmt .Sprintf ("No complete LocalStack AWS profile found. Run lstk interactively to configure one, or add a [profile %s] section to ~/.aws/config manually." , profileName ))
236+ configOK , credsOK , err := profilePresence ()
237+ if err != nil {
238+ output .EmitWarning (sink , fmt .Sprintf ("could not check AWS profile presence: %v" , err ))
239+ return nil
240+ }
241+ if interactive && ! configOK && ! credsOK {
242+ return Setup (ctx , sink , resolvedHost )
243+ }
244+
245+ emitMissingProfileNote (sink )
246+ return nil
247+ }
248+
249+ // Setup checks for the localstack AWS profile and prompts to create or update it if needed.
250+ // resolvedHost must be a host:port string (e.g. "localhost.localstack.cloud:4566").
251+ func Setup (ctx context.Context , sink output.Sink , resolvedHost string ) error {
252+ status , err := needsProfileSetup (resolvedHost )
253+ if err != nil {
254+ output .EmitWarning (sink , fmt .Sprintf ("could not check AWS profile: %v" , err ))
255+ return nil
256+ }
257+ if ! status .anyNeeded () {
258+ output .EmitNote (sink , "LocalStack AWS profile is already configured." )
259+ return nil
260+ }
261+
262+ configPath , credsPath , err := awsPaths ()
263+ if err != nil {
264+ output .EmitWarning (sink , fmt .Sprintf ("could not determine AWS config paths: %v" , err ))
208265 return nil
209266 }
210267
211268 responseCh := make (chan output.InputResponse , 1 )
212269 output .EmitUserInputRequest (sink , output.UserInputRequestEvent {
213- Prompt : "Configure AWS profile in ~/.aws/ ?" ,
270+ Prompt : "Set up a LocalStack profile for AWS CLI and SDKs in ~/.aws?" ,
214271 Options : []output.InputOption {{Key : "y" , Label : "Y" }, {Key : "n" , Label : "n" }},
215272 ResponseCh : responseCh ,
216273 })
217274
218275 select {
219276 case resp := <- responseCh :
220- if resp .Cancelled || resp .SelectedKey == "n" {
277+ if resp .Cancelled {
278+ return nil
279+ }
280+ if resp .SelectedKey == "n" {
281+ output .EmitNote (sink , "Skipped adding LocalStack AWS profile." )
221282 return nil
222283 }
223284 if status .configNeeded {
@@ -232,12 +293,10 @@ func Setup(ctx context.Context, sink output.Sink, interactive bool, resolvedHost
232293 return nil
233294 }
234295 }
235- output .EmitSuccess (sink , "AWS profile successfully configured" )
236- output .EmitNote (sink , fmt .Sprintf ("Try: aws s3 mb s3://test --profile %s" , profileName ))
296+ output .EmitSuccess (sink , "Created LocalStack profile in ~/.aws/config" )
237297 case <- ctx .Done ():
238298 return ctx .Err ()
239299 }
240300
241301 return nil
242302}
243-
0 commit comments