-
Notifications
You must be signed in to change notification settings - Fork 6
feat: form for interactive config file creation #53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
225b2c4
feat: form for interactive config file creation
SLH335 09c7a84
feat: separate config struct for config creation
SLH335 15364f7
fix: abort config creation when not overwriting
SLH335 1a3629b
chore: use /etc/docker as default form config path
SLH335 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path" | ||
|
|
||
| "github.com/charmbracelet/huh" | ||
| ) | ||
|
|
||
| type setupConfig struct { | ||
| IdentityEndpoint string `json:"endpoint,omitempty"` | ||
| ApplicationCredentialID string `json:"applicationCredentialId,omitempty"` | ||
| ApplicationCredentialSecret string `json:"applicationCredentialSecret,omitempty"` | ||
| Region string `json:"region,omitempty"` | ||
| MountDir string `json:"mountDir,omitempty"` | ||
| } | ||
|
|
||
| func createConfiguration(configPath string) error { | ||
| var config setupConfig | ||
|
|
||
| credentialInstructions := | ||
| `To get an Application Credential ID you must create new Application Credentials | ||
| via the web console (Horizon). | ||
| Navigate to "Identity > Application Credentials > Create Application Credential" | ||
| Enter a descriptive name and optionally a description and expiration date. | ||
| Press 'Create Application Credential', copy the displayed ID and paste it here. | ||
|
|
||
| Do not close the window yet.` | ||
|
|
||
| mountDirInstructions := `Directory used for mounting cinder volumes | ||
| Leave blank for default` | ||
|
|
||
| stat, err := os.Stat(configPath) | ||
| if err == nil { | ||
| if stat.IsDir() { | ||
| return fmt.Errorf("The configuration file path already is a directory. Delete it or choose a different path to continue.") | ||
| } else { | ||
| overwriteFile := false | ||
|
|
||
| err = huh.NewForm( | ||
| huh.NewGroup( | ||
| huh.NewConfirm().Title("The config file already exists. Overwrite it?").Description(fmt.Sprintf("Path: '%s'", configPath)).Value(&overwriteFile), | ||
| ), | ||
| ).Run() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if !overwriteFile { | ||
| return fmt.Errorf("user aborted") | ||
| } | ||
| } | ||
| } else if errors.Is(err, os.ErrNotExist) { | ||
| err = os.MkdirAll(path.Dir(configPath), 0775) | ||
| } | ||
|
|
||
| err = huh.NewForm( | ||
| huh.NewGroup( | ||
| huh.NewInput().Title("Open Stack endpoint URL").Value(&config.IdentityEndpoint), | ||
| huh.NewInput().Title("Open Stack Region Name").Value(&config.Region), | ||
| ), | ||
| huh.NewGroup( | ||
| huh.NewInput().Title("Application Credential ID").Value(&config.ApplicationCredentialID).Description(credentialInstructions), | ||
| huh.NewInput().Title("Application Credential Secret").Value(&config.ApplicationCredentialSecret).Description("Copy the displayed Secret and paste it here").EchoMode(huh.EchoModePassword), | ||
| ), | ||
| huh.NewGroup( | ||
| huh.NewInput().Title("Mount Dir").Value(&config.MountDir).Description(mountDirInstructions), | ||
| ), | ||
| ).Run() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return writeConfigurationFile(config, configPath) | ||
| } | ||
|
|
||
| func writeConfigurationFile(config setupConfig, path string) error { | ||
| configText, err := json.MarshalIndent(config, "", " ") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = os.WriteFile(path, configText, 0664) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.