Skip to content

Commit 93d4c50

Browse files
committed
Handle gracefull shutdown
1 parent df0a88e commit 93d4c50

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ var rootCmd = &cobra.Command{
1616
Use: "aws-cli-manager",
1717
Short: "AWS CLI Manager is a tool to manage AWS CLI profiles",
1818
Run: func(cmd *cobra.Command, args []string) {
19+
// SelectProfile now returns a boolean indicating whether a profile was selected
20+
// We don't need to do anything with the return value here
1921
profile.SelectProfile()
2022
},
2123
}

cmd/select.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@ var selectCmd = &cobra.Command{
1313
Run: func(cmd *cobra.Command, args []string) {
1414
if len(args) == 0 {
1515
// If no profile is specified, show the selection menu
16-
profile.SelectProfile()
16+
// SelectProfile now returns a boolean indicating whether a profile was selected
17+
selected := profile.SelectProfile()
18+
if !selected {
19+
// User cancelled the selection with Ctrl+C
20+
return
21+
}
1722
} else {
1823
// If a profile is specified, select it directly
1924
profileName := args[0]

pkg/profile/profile.go

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
"sort"
99
"strings"
1010

11+
"github.com/manifoldco/promptui"
1112
"github.com/user-cube/aws-cli-manager/v2/pkg/models"
1213
"github.com/user-cube/aws-cli-manager/v2/pkg/settings"
1314
"github.com/user-cube/aws-cli-manager/v2/pkg/sharedModules"
1415
"github.com/user-cube/aws-cli-manager/v2/pkg/ui"
1516
"gopkg.in/yaml.v2"
1617
)
1718

18-
func SelectProfile() {
19+
func SelectProfile() bool {
1920
// Get current profile first
2021
currentProfile := GetCurrentProfile()
2122
profileNames, awsProfiles := GetProfiles()
@@ -52,13 +53,20 @@ func SelectProfile() {
5253

5354
index, _, err := prompt.Run()
5455
if err != nil {
56+
// Check if it's an interrupt error (Ctrl+C)
57+
if err == promptui.ErrInterrupt {
58+
fmt.Println("\nProfile selection cancelled")
59+
return false
60+
}
61+
// Handle other errors
5562
ui.PrintError("Error selecting profile: %v", err)
5663
os.Exit(1)
5764
}
5865

5966
// Get the selected profile name
6067
selectedProfile := sortedNames[index]
6168
setProfile(selectedProfile, awsProfiles)
69+
return true
6270
}
6371

6472
func GetProfiles() (profileNames []string, awsProfiles models.AwsProfile) {
@@ -203,6 +211,12 @@ func PromptProfileName() string {
203211

204212
result, err := prompt.Run()
205213
if err != nil {
214+
// Check if it's an interrupt error (Ctrl+C)
215+
if err == promptui.ErrInterrupt {
216+
fmt.Println("\nProfile creation cancelled")
217+
os.Exit(0)
218+
}
219+
// Handle other errors
206220
ui.PrintError("Failed to get profile name: %v", err)
207221
os.Exit(1)
208222
}
@@ -231,6 +245,11 @@ func PromptProfileDetails() models.ProfileDetails {
231245

232246
region, err := regionPrompt.Run()
233247
if err != nil {
248+
// Check if it's an interrupt error (Ctrl+C)
249+
if err == promptui.ErrInterrupt {
250+
fmt.Println("\nProfile creation cancelled")
251+
os.Exit(0)
252+
}
234253
ui.PrintError("Failed to get region: %v", err)
235254
os.Exit(1)
236255
}
@@ -244,6 +263,11 @@ func PromptProfileDetails() models.ProfileDetails {
244263

245264
ssoIndex, _, err := ssoPrompt.Run()
246265
if err != nil {
266+
// Check if it's an interrupt error (Ctrl+C)
267+
if err == promptui.ErrInterrupt {
268+
fmt.Println("\nProfile creation cancelled")
269+
os.Exit(0)
270+
}
247271
ui.PrintError("Failed to get SSO information: %v", err)
248272
os.Exit(1)
249273
}
@@ -257,6 +281,11 @@ func PromptProfileDetails() models.ProfileDetails {
257281

258282
config, err := configPrompt.Run()
259283
if err != nil {
284+
// Check if it's an interrupt error (Ctrl+C)
285+
if err == promptui.ErrInterrupt {
286+
fmt.Println("\nProfile creation cancelled")
287+
os.Exit(0)
288+
}
260289
ui.PrintError("Failed to get config file information: %v", err)
261290
os.Exit(1)
262291
}
@@ -275,6 +304,11 @@ func PromptProfileDetails() models.ProfileDetails {
275304

276305
credentials, err := credentialsPrompt.Run()
277306
if err != nil {
307+
// Check if it's an interrupt error (Ctrl+C)
308+
if err == promptui.ErrInterrupt {
309+
fmt.Println("\nProfile creation cancelled")
310+
os.Exit(0)
311+
}
278312
ui.PrintError("Failed to get credentials file information: %v", err)
279313
os.Exit(1)
280314
}

0 commit comments

Comments
 (0)