detached aa585b0

Refactor LoadClientConfig with guard clauses and clearer prompts

commit
aa585b0659dfb92be1c81b2b71aae2927bd7ad2b
author
bnema · · 2023-10-24 10:17 UTC
parent
e731c2b
signature
unsigned
1 file · +18 -16
M internal/app/newclient.go +18 -16
@@ -73,34 +73,36 @@ func readAndUnmarshalConfig(filePath string, config *Config) error {
7373 return yaml.Unmarshal(configData, config)
7474 }
7575
76-// LoadClientConfig loads the client configuration.
7776 func (config *Config) LoadClientConfig() (*Config, error) {
7877 configDir, configFilePath := configPath()
7978
80- if err := os.MkdirAll(configDir, 0755); err != nil {
79+ err := os.MkdirAll(configDir, 0755)
80+ if err != nil {
8181 return nil, fmt.Errorf("error creating configuration directory: %w", err)
8282 }
8383
84- // Check if the file exists
85- if _, err := os.Stat(configFilePath); os.IsNotExist(err) {
84+ _, err = os.Stat(configFilePath)
85+ if os.IsNotExist(err) {
8686 fmt.Printf("Config file not found, creating it at %s\n", configFilePath)
8787
88- // Prompt user for initial settings
89- config.Http.BackendURL = readUserInput("Enter the backend URL:")
90- config.General.GordonToken = readUserInput("Enter the Gordon token:")
88+ config.Http.BackendURL = readUserInput("Enter the backend URL (e.g. https://gordon.mydomain.com):")
89+ config.General.GordonToken = readUserInput("Enter the token (check your backend config.yml):")
9190
92- // Save the new configuration
93- if err := config.SaveConfig(); err != nil {
91+ err = config.SaveConfig()
92+ if err != nil {
9493 return nil, fmt.Errorf("error saving new configuration: %w", err)
9594 }
96- } else if err != nil {
97- // Some other error occurred while checking the file
95+
96+ return config, nil
97+ }
98+
99+ if err != nil {
98100 return nil, fmt.Errorf("error checking configuration file: %w", err)
99- } else {
100- // File exists, read and unmarshal it
101- if err := readAndUnmarshalConfig(configFilePath, config); err != nil {
102- return nil, err
103- }
101+ }
102+
103+ err = readAndUnmarshalConfig(configFilePath, config)
104+ if err != nil {
105+ return nil, err
104106 }
105107
106108 return config, nil
@@ -73,34 +73,36 @@ func readAndUnmarshalConfig(filePath string, config *Config) error {
73 return yaml.Unmarshal(configData, config)73 return yaml.Unmarshal(configData, config)
74 }74 }
75 75
76-// LoadClientConfig loads the client configuration.
77 func (config *Config) LoadClientConfig() (*Config, error) {76 func (config *Config) LoadClientConfig() (*Config, error) {
78 configDir, configFilePath := configPath()77 configDir, configFilePath := configPath()
79 78
80- if err := os.MkdirAll(configDir, 0755); err != nil {79+ err := os.MkdirAll(configDir, 0755)
80+ if err != nil {
81 return nil, fmt.Errorf("error creating configuration directory: %w", err)81 return nil, fmt.Errorf("error creating configuration directory: %w", err)
82 }82 }
83 83
84- // Check if the file exists84+ _, err = os.Stat(configFilePath)
85- if _, err := os.Stat(configFilePath); os.IsNotExist(err) {85+ if os.IsNotExist(err) {
86 fmt.Printf("Config file not found, creating it at %s\n", configFilePath)86 fmt.Printf("Config file not found, creating it at %s\n", configFilePath)
87 87
88- // Prompt user for initial settings88+ config.Http.BackendURL = readUserInput("Enter the backend URL (e.g. https://gordon.mydomain.com):")
89- config.Http.BackendURL = readUserInput("Enter the backend URL:")89+ config.General.GordonToken = readUserInput("Enter the token (check your backend config.yml):")
90- config.General.GordonToken = readUserInput("Enter the Gordon token:")
91 90
92- // Save the new configuration91+ err = config.SaveConfig()
93- if err := config.SaveConfig(); err != nil {92+ if err != nil {
94 return nil, fmt.Errorf("error saving new configuration: %w", err)93 return nil, fmt.Errorf("error saving new configuration: %w", err)
95 }94 }
96- } else if err != nil {95+
97- // Some other error occurred while checking the file96+ return config, nil
97+ }
98+
99+ if err != nil {
98 return nil, fmt.Errorf("error checking configuration file: %w", err)100 return nil, fmt.Errorf("error checking configuration file: %w", err)
99- } else {101+ }
100- // File exists, read and unmarshal it102+
101- if err := readAndUnmarshalConfig(configFilePath, config); err != nil {103+ err = readAndUnmarshalConfig(configFilePath, config)
102- return nil, err104+ if err != nil {
103- }105+ return nil, err
104 }106 }
105 107
106 return config, nil108 return config, nil

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 7

Tags 106

↑↓ moveenter openesc close