Skip to content

Commit 15364f7

Browse files
SLH335RocketRene
andcommitted
fix: abort config creation when not overwriting
Co-authored-by: René Kuhn <[email protected]>
1 parent 09c7a84 commit 15364f7

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

createConfig.go

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@ import (
1111
)
1212

1313
type setupConfig struct {
14-
IdentityEndpoint string `json:"endpoint"`
15-
ApplicationCredentialID string `json:"applicationCredentialId"`
16-
ApplicationCredentialSecret string `json:"applicationCredentialSecret"`
17-
Region string `json:"region"`
18-
MountDir string `json:"mountDir"`
14+
IdentityEndpoint string `json:"endpoint,omitempty"`
15+
ApplicationCredentialID string `json:"applicationCredentialId,omitempty"`
16+
ApplicationCredentialSecret string `json:"applicationCredentialSecret,omitempty"`
17+
Region string `json:"region,omitempty"`
18+
MountDir string `json:"mountDir,omitempty"`
1919
}
2020

21-
func createConfiguration(configPath string) (config setupConfig, err error) {
21+
func createConfiguration(configPath string) error {
22+
var config setupConfig
23+
2224
credentialInstructions :=
2325
`To get an Application Credential ID you must create new Application Credentials
2426
via the web console (Horizon).
@@ -31,21 +33,26 @@ Do not close the window yet.`
3133
mountDirInstructions := `Directory used for mounting cinder volumes
3234
Leave blank for default`
3335

34-
var overwriteFile bool = true
35-
3636
stat, err := os.Stat(configPath)
3737
if err == nil {
3838
if stat.IsDir() {
39-
return setupConfig{}, fmt.Errorf("The configuration file path already is a directory. Delete it or choose a different path to continue.")
39+
return fmt.Errorf("The configuration file path already is a directory. Delete it or choose a different path to continue.")
4040
} else {
41+
overwriteFile := false
42+
4143
err = huh.NewForm(
4244
huh.NewGroup(
4345
huh.NewConfirm().Title("The config file already exists. Overwrite it?").Description(fmt.Sprintf("Path: '%s'", configPath)).Value(&overwriteFile),
4446
),
4547
).Run()
48+
if err != nil {
49+
return err
50+
}
51+
if !overwriteFile {
52+
return fmt.Errorf("user aborted")
53+
}
4654
}
47-
}
48-
if errors.Is(err, os.ErrNotExist) {
55+
} else if errors.Is(err, os.ErrNotExist) {
4956
err = os.MkdirAll(path.Dir(configPath), 0775)
5057
}
5158

@@ -63,15 +70,10 @@ Leave blank for default`
6370
),
6471
).Run()
6572
if err != nil {
66-
return setupConfig{}, err
67-
}
68-
69-
err = writeConfigurationFile(config, configPath)
70-
if err != nil {
71-
return setupConfig{}, err
73+
return err
7274
}
7375

74-
return config, nil
76+
return writeConfigurationFile(config, configPath)
7577
}
7678

7779
func writeConfigurationFile(config setupConfig, path string) error {

main.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,14 @@ func main() {
6060
}
6161

6262
if createConfig {
63-
_, err := createConfiguration(configFile)
64-
if err != nil {
63+
err := createConfiguration(configFile)
64+
if err == nil {
65+
log.Info("Configuration file written successfully")
66+
} else if err.Error() == "user aborted" {
67+
log.Info("Configuration setup aborted")
68+
} else {
6569
log.Fatalf("Failed to write configuration file: %s", err)
6670
}
67-
log.Info("Configuration file written successfully")
6871
os.Exit(0)
6972
}
7073

0 commit comments

Comments
 (0)