Skip to content

Commit 4aa9160

Browse files
authored
feat(init): no overwrite prompt when adding a new profile (#3097)
1 parent 6ecde0b commit 4aa9160

7 files changed

+148
-14
lines changed

internal/namespaces/init/init.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,12 @@ Default path for configuration file is based on the following priority order:
136136
// Show logo banner, or simple welcome message
137137
printScalewayBanner()
138138

139-
err := promptProfileOverride(ctx, configPath, profileName)
139+
config, err := loadConfigOrEmpty(configPath)
140+
if err != nil {
141+
return nil, err
142+
}
143+
144+
err = promptProfileOverride(ctx, config, configPath, profileName)
140145
if err != nil {
141146
return nil, err
142147
}
@@ -215,11 +220,6 @@ Default path for configuration file is based on the following priority order:
215220
DefaultProjectID: &args.ProjectID, // An API key is always bound to a project.
216221
}
217222

218-
config, err := loadConfigOrEmpty(configPath)
219-
if err != nil {
220-
return nil, err
221-
}
222-
223223
// Save the profile as default or as a named profile
224224
if profileName == scw.DefaultProfileName {
225225
// Default configuration

internal/namespaces/init/init_test.go

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,9 @@ func TestInit(t *testing.T) {
108108
},
109109
Profiles: map[string]*scw.Profile{
110110
"test": {
111-
AccessKey: &dummyAccessKey,
112-
SecretKey: &dummySecretKey,
111+
AccessKey: &dummyAccessKey,
112+
SecretKey: &dummySecretKey,
113+
DefaultZone: scw.StringPtr("fr-test"), // Used to check profile override
113114
},
114115
},
115116
}
@@ -154,5 +155,46 @@ func TestInit(t *testing.T) {
154155
"yes",
155156
},
156157
}))
158+
159+
t.Run("No Prompt Overwrite for new profile", core.Test(&core.TestConfig{
160+
Commands: GetCommands(),
161+
BeforeFunc: core.BeforeFuncCombine(
162+
baseBeforeFunc(),
163+
beforeFuncSaveConfig(dummyConfig),
164+
),
165+
Cmd: appendArgs("scw -p test2 init", defaultArgs),
166+
Check: core.TestCheckCombine(
167+
core.TestCheckGolden(),
168+
checkConfig(func(t *testing.T, ctx *core.CheckFuncCtx, config *scw.Config) {
169+
assert.NotNil(t, config.Profiles["test2"], "new profile should have been created")
170+
}),
171+
),
172+
TmpHomeDir: true,
173+
PromptResponseMocks: []string{
174+
// Do you want to override the current config? (Should not be prompted as profile is a new one)
175+
"no",
176+
},
177+
}))
178+
179+
t.Run("Prompt Overwrite for existing profile", core.Test(&core.TestConfig{
180+
Commands: GetCommands(),
181+
BeforeFunc: core.BeforeFuncCombine(
182+
baseBeforeFunc(),
183+
beforeFuncSaveConfig(dummyConfig),
184+
),
185+
Cmd: appendArgs("scw -p test init", defaultArgs),
186+
Check: core.TestCheckCombine(
187+
core.TestCheckGolden(),
188+
checkConfig(func(t *testing.T, ctx *core.CheckFuncCtx, config *scw.Config) {
189+
assert.NotNil(t, config.Profiles["test"].DefaultZone)
190+
assert.Equal(t, *config.Profiles["test"].DefaultZone, "fr-test")
191+
}),
192+
),
193+
TmpHomeDir: true,
194+
PromptResponseMocks: []string{
195+
// Do you want to override the current config? (Should not be prompted as profile is a new one)
196+
"no",
197+
},
198+
}))
157199
})
158200
}

internal/namespaces/init/prompt.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,22 @@ func promptDefaultZone(ctx context.Context) (scw.Zone, error) {
163163
return scw.ParseZone(zone)
164164
}
165165

166-
// promptProfileOverride prompt user if profileName is getting override in configPath
167-
func promptProfileOverride(ctx context.Context, configPath string, profileName string) error {
168-
config, err := scw.LoadConfigFromPath(configPath)
166+
// promptProfileOverride prompt user if profileName is getting override in config
167+
func promptProfileOverride(ctx context.Context, config *scw.Config, configPath string, profileName string) error {
168+
var profile *scw.Profile
169+
var profileExists bool
170+
171+
if profileName == scw.DefaultProfileName {
172+
profile = &config.Profile
173+
profileExists = true
174+
} else {
175+
profile, profileExists = config.Profiles[profileName]
176+
}
169177

170-
// If it is not a new config, ask if we want to override the existing config
171-
if err == nil && !config.IsEmpty() {
178+
if !config.IsEmpty() && profileExists {
172179
_, _ = interactive.PrintlnWithoutIndent(`
173180
Current config is located at ` + configPath + `
174-
` + terminal.Style(fmt.Sprint(config), color.Faint) + `
181+
` + terminal.Style(fmt.Sprint(profile), color.Faint) + `
175182
`)
176183
overrideConfig, err := interactive.PromptBoolWithConfig(&interactive.PromptBoolConfig{
177184
Prompt: fmt.Sprintf("Do you want to override the current profile (%s) ?", profileName),
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: ""
6+
form: {}
7+
headers:
8+
User-Agent:
9+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
10+
url: https://api.scaleway.com/iam/v1alpha1/api-keys/SCWXXXXXXXXXXXXXXXXX
11+
method: GET
12+
response:
13+
body: '{"message":"authentication is denied","method":"api_key","reason":"not_found","type":"denied_authentication"}'
14+
headers:
15+
Content-Length:
16+
- "109"
17+
Content-Security-Policy:
18+
- default-src 'none'; frame-ancestors 'none'
19+
Content-Type:
20+
- application/json
21+
Date:
22+
- Thu, 27 Apr 2023 09:09:09 GMT
23+
Server:
24+
- Scaleway API-Gateway
25+
Strict-Transport-Security:
26+
- max-age=63072000
27+
X-Content-Type-Options:
28+
- nosniff
29+
X-Frame-Options:
30+
- DENY
31+
X-Request-Id:
32+
- 4765a621-1ac1-4a7f-bdb4-4602f94764eb
33+
status: 401 Unauthorized
34+
code: 401
35+
duration: ""
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
2+
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
3+
✅ Initialization completed with success.
4+
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
5+
{
6+
"message": "Initialization completed with success",
7+
"details": ""
8+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
version: 1
3+
interactions:
4+
- request:
5+
body: ""
6+
form: {}
7+
headers:
8+
User-Agent:
9+
- scaleway-sdk-go/v1.0.0-beta.7+dev (go1.20.1; linux; amd64) cli-e2e-test
10+
url: https://api.scaleway.com/iam/v1alpha1/api-keys/SCWXXXXXXXXXXXXXXXXX
11+
method: GET
12+
response:
13+
body: '{"message":"authentication is denied","method":"api_key","reason":"not_found","type":"denied_authentication"}'
14+
headers:
15+
Content-Length:
16+
- "109"
17+
Content-Security-Policy:
18+
- default-src 'none'; frame-ancestors 'none'
19+
Content-Type:
20+
- application/json
21+
Date:
22+
- Thu, 27 Apr 2023 09:09:09 GMT
23+
Server:
24+
- Scaleway API-Gateway
25+
Strict-Transport-Security:
26+
- max-age=63072000
27+
X-Content-Type-Options:
28+
- nosniff
29+
X-Frame-Options:
30+
- DENY
31+
X-Request-Id:
32+
- 314626bd-48a4-4e66-91bc-7a31b3f17210
33+
status: 401 Unauthorized
34+
code: 401
35+
duration: ""
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
🎲🎲🎲 EXIT CODE: 1 🎲🎲🎲
2+
🟥🟥🟥 STDERR️️ 🟥🟥🟥️
3+
Initialization canceled
4+
🟥🟥🟥 JSON STDERR 🟥🟥🟥
5+
{
6+
"error": "initialization canceled"
7+
}

0 commit comments

Comments
 (0)