From 557f1a39749344ce0bccee02a31ae3e49ff706c9 Mon Sep 17 00:00:00 2001 From: Markus Wennrich Date: Tue, 29 Sep 2026 09:07:18 +0200 Subject: [PATCH 1/3] enable environment based configuration --- README.md | 6 ++++++ cmd/config/config.go | 2 +- cmd/root.go | 5 +++++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 04a72be..2be37a5 100644 --- a/README.md +++ b/README.md @@ -110,3 +110,9 @@ $ metalctlv2 ctx add --activate --default-project ``` The configuration file is by default written to `~/.metal-stack/config.yaml`. + +## Environment Variables + +All flags can also be provided as environment variables prefixed with `METAL_STACK_`. The flag name is uppercased and dashes are replaced by underscores, e.g. `--api-url` becomes `METAL_STACK_API_URL`. + +An explicitly provided flag takes precedence over an environment variable, which in turn takes precedence over the context in `~/.metal-stack/config.yaml`. diff --git a/cmd/config/config.go b/cmd/config/config.go index fdc692c..a83c3b5 100644 --- a/cmd/config/config.go +++ b/cmd/config/config.go @@ -21,7 +21,7 @@ const ( // BinaryName is the name of the cli in all help texts BinaryName = "metalctlv2" // ConfigDir is the directory in either the homedir or in /etc where the cli searches for a file config.yaml - // also used as prefix for environment based configuration, e.g. METAL_STACK_CLOUD_ will be the variable prefix. + // also used as prefix for environment based configuration, e.g. METAL_STACK_ will be the variable prefix. ConfigDir = "metal-stack" ) diff --git a/cmd/root.go b/cmd/root.go index 821a391..e9adb01 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -3,6 +3,7 @@ package cmd import ( "log/slog" "os" + "strings" client "github.com/metal-stack/api/go/client" "github.com/metal-stack/metal-lib/pkg/genericcli" @@ -42,6 +43,10 @@ func Execute() { } func NewRootCmd(c *config.Config) *cobra.Command { + viper.SetEnvPrefix(strings.ToUpper(config.ConfigDir)) + viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) + viper.AutomaticEnv() + rootCmd := &cobra.Command{ Use: config.BinaryName, Aliases: []string{"m"}, From 36c31ba6567684958a83c605500de7f0fed5ca60 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Fri, 2 Oct 2026 14:48:43 +0200 Subject: [PATCH 2/3] Explicitly set. --- cmd/root.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/cmd/root.go b/cmd/root.go index e9adb01..7f37abc 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -45,7 +45,6 @@ func Execute() { func NewRootCmd(c *config.Config) *cobra.Command { viper.SetEnvPrefix(strings.ToUpper(config.ConfigDir)) viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_")) - viper.AutomaticEnv() rootCmd := &cobra.Command{ Use: config.BinaryName, @@ -77,6 +76,13 @@ func NewRootCmd(c *config.Config) *cobra.Command { genericcli.Must(viper.BindPFlags(rootCmd.PersistentFlags())) + viper.MustBindEnv("config") + viper.MustBindEnv("force-color") + viper.MustBindEnv("debug") + viper.MustBindEnv("timeout") + viper.MustBindEnv("api-url") + viper.MustBindEnv("api-token") + markdownCmd := &cobra.Command{ Use: "markdown", Short: "create markdown documentation", From d1291f815717a52801c74233733de31da47764b7 Mon Sep 17 00:00:00 2001 From: Gerrit Date: Fri, 2 Oct 2026 14:49:32 +0200 Subject: [PATCH 3/3] Docs. --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2be37a5..6c17657 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,6 @@ The configuration file is by default written to `~/.metal-stack/config.yaml`. ## Environment Variables -All flags can also be provided as environment variables prefixed with `METAL_STACK_`. The flag name is uppercased and dashes are replaced by underscores, e.g. `--api-url` becomes `METAL_STACK_API_URL`. +Important flags can also be provided as environment variables prefixed with `METAL_STACK_`. The flag name is uppercased and dashes are replaced by underscores, e.g. `--api-url` becomes `METAL_STACK_API_URL`. An explicitly provided flag takes precedence over an environment variable, which in turn takes precedence over the context in `~/.metal-stack/config.yaml`.