[WIP] feat: Support for config file reading#55
Open
jellonek wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR starts adding configuration management support (per #49) by introducing Viper-based config/env initialization in the CLI entrypoint, along with a --config flag to point to an explicit config file.
Changes:
- Add Viper initialization and multi-location config file discovery logic in
main.go. - Add a
--configCLI flag (currently viapflag) to specify a custom config file path. - Promote
github.com/spf13/pflagto a direct dependency ingo.mod.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 8 comments.
| File | Description |
|---|---|
| main.go | Adds Viper setup, config-file discovery, and a --config flag prior to running the Cobra CLI. |
| go.mod | Adds spf13/pflag as a direct dependency to support the new flag parsing. |
Comments suppressed due to low confidence (1)
main.go:57
- Typo in comment: the listed fallback file name "$HOME/cloudclt.yaml" should be "$HOME/cloudctl.yaml".
// ./cloudctl.yaml
// $HOME/cloudclt.yaml
// $XDG_CONFIG_HOME/cloudctl/cloudctl.yaml
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+34
to
+38
| viper.BindPFlags(pflag.CommandLine) | ||
|
|
||
| pflag.StringVarP(&config, "config", "c", "", "Path to configuration file") | ||
| pflag.Parse() | ||
|
|
Comment on lines
+34
to
+38
| viper.BindPFlags(pflag.CommandLine) | ||
|
|
||
| pflag.StringVarP(&config, "config", "c", "", "Path to configuration file") | ||
| pflag.Parse() | ||
|
|
Comment on lines
+34
to
+36
| viper.BindPFlags(pflag.CommandLine) | ||
|
|
||
| pflag.StringVarP(&config, "config", "c", "", "Path to configuration file") |
Comment on lines
+28
to
+29
| home := os.Getenv("HOME") | ||
|
|
Author
There was a problem hiding this comment.
That's already noted in TODO section of PR description.
Comment on lines
+41
to
+73
| if len(config) > 0 { | ||
| // First we are trying config provided as command line parameter. | ||
| viper.SetConfigFile(config) | ||
| } else { | ||
| // Then we are searching for ".cloudctl.yaml" in current or home directory | ||
| viper.AddConfigPath(".") | ||
| viper.AddConfigPath(home) | ||
| viper.SetConfigName(".cloudctl") | ||
| } | ||
|
|
||
| err := viper.ReadInConfig() | ||
| if _, ok := err.(viper.ConfigFileNotFoundError); ok { | ||
| // If reading config in above described locations failed, we are looking for configuration | ||
| // in these locations: | ||
| // ./cloudctl.yaml | ||
| // $HOME/cloudclt.yaml | ||
| // $XDG_CONFIG_HOME/cloudctl/cloudctl.yaml | ||
| // $XDG_CONFIG_HOME/cloudctl.yaml | ||
| // $HOME/.config/cloudctl/cloudctl.yaml | ||
| // $HOME/.config/cloudctl.yaml | ||
| viper.SetConfigName("cloudctl") | ||
| if xdgConfig := os.Getenv("XDG_CONFIG_HOME"); len(xdgConfig) > 0 { | ||
| viper.AddConfigPath(path.Join(xdgConfig, "cloudctl")) | ||
| viper.AddConfigPath(xdgConfig) | ||
| } else { | ||
| viper.AddConfigPath(path.Join(home, ".config", "cloudctl")) | ||
| viper.AddConfigPath(path.Join(home, ".config")) | ||
| } | ||
| err = viper.ReadInConfig() | ||
| if _, ok := err.(viper.ConfigFileNotFoundError); ok { | ||
| err = nil | ||
| } | ||
| } |
Author
There was a problem hiding this comment.
Already noted in TODO section of this Work In Progress PR description.
194fa3d to
85c341e
Compare
Add support for configuration files as described in cloudoperators#49 Signed-off-by: Piotr Skamruk <piotr.skamruk@gmail.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Add support for configuration files as described in #49
TODO:
$HOME(by using https://pkg.go.dev/os#UserHomeDir instead of plain env variable)(* not mentioned in acceptance criteria but seems to be reasonable).