Skip to content

[WIP] feat: Support for config file reading#55

Open
jellonek wants to merge 1 commit into
cloudoperators:mainfrom
jellonek:viper
Open

[WIP] feat: Support for config file reading#55
jellonek wants to merge 1 commit into
cloudoperators:mainfrom
jellonek:viper

Conversation

@jellonek
Copy link
Copy Markdown

Add support for configuration files as described in #49

TODO:

  • update README.md
  • remove assumption for correct value of $HOME (by using https://pkg.go.dev/os#UserHomeDir instead of plain env variable)
  • * add test coverage
    (* not mentioned in acceptance criteria but seems to be reasonable).

Copilot AI review requested due to automatic review settings May 21, 2026 15:18
@jellonek jellonek requested review from a team as code owners May 21, 2026 15:18
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 --config CLI flag (currently via pflag) to specify a custom config file path.
  • Promote github.com/spf13/pflag to a direct dependency in go.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 thread main.go Outdated
Comment on lines +34 to +38
viper.BindPFlags(pflag.CommandLine)

pflag.StringVarP(&config, "config", "c", "", "Path to configuration file")
pflag.Parse()

Comment thread main.go Outdated
Comment thread main.go Outdated
Comment on lines +34 to +38
viper.BindPFlags(pflag.CommandLine)

pflag.StringVarP(&config, "config", "c", "", "Path to configuration file")
pflag.Parse()

Comment thread main.go Outdated
Comment on lines +34 to +36
viper.BindPFlags(pflag.CommandLine)

pflag.StringVarP(&config, "config", "c", "", "Path to configuration file")
Comment thread main.go Outdated
Comment thread main.go Outdated
Comment thread main.go
Comment on lines +28 to +29
home := os.Getenv("HOME")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's already noted in TODO section of PR description.

Comment thread main.go
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
}
}
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Already noted in TODO section of this Work In Progress PR description.

@jellonek jellonek force-pushed the viper branch 2 times, most recently from 194fa3d to 85c341e Compare May 21, 2026 15:42
Add support for configuration files as described in cloudoperators#49

Signed-off-by: Piotr Skamruk <piotr.skamruk@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants