diff --git a/cmd/switcher/context.go b/cmd/switcher/context.go index 85fd1cfa..00f8c80d 100644 --- a/cmd/switcher/context.go +++ b/cmd/switcher/context.go @@ -15,7 +15,6 @@ package switcher import ( "fmt" - "os" delete_context "github.com/danielfoehrkn/kubeswitch/pkg/subcommands/delete-context" "github.com/danielfoehrkn/kubeswitch/pkg/subcommands/history" @@ -247,7 +246,7 @@ func setFlagsForContextCommands(command *cobra.Command) { command.Flags().StringVar( &configPath, "config-path", - os.ExpandEnv("$HOME/.kube/switch-config.yaml"), + getDefaultConfigPath(), "path on the local filesystem to the configuration file.") // not used for setContext command. Makes call in switch.sh script easier (no need to exclude flag from call) command.Flags().BoolVar( diff --git a/cmd/switcher/gardener.go b/cmd/switcher/gardener.go index 5d0855e4..dc734761 100644 --- a/cmd/switcher/gardener.go +++ b/cmd/switcher/gardener.go @@ -14,8 +14,6 @@ package switcher import ( - "os" - gardenercontrolplane "github.com/danielfoehrkn/kubeswitch/pkg/subcommands/gardener" "github.com/spf13/cobra" ) @@ -47,7 +45,7 @@ func init() { controlplaneCmd.Flags().StringVar( &configPath, "config-path", - os.ExpandEnv("$HOME/.kube/switch-config.yaml"), + getDefaultConfigPath(), "path on the local filesystem to the configuration file.") gardenerCmd.AddCommand(controlplaneCmd) diff --git a/cmd/switcher/hooks.go b/cmd/switcher/hooks.go index 1a73d588..bdf49748 100644 --- a/cmd/switcher/hooks.go +++ b/cmd/switcher/hooks.go @@ -49,7 +49,7 @@ func init() { hookLsCmd.Flags().StringVar( &configPath, "config-path", - os.ExpandEnv("$HOME/.kube/switch-config.yaml"), + getDefaultConfigPath(), "path on the local filesystem to the configuration file.") hookLsCmd.Flags().StringVar( @@ -63,7 +63,7 @@ func init() { hookCmd.Flags().StringVar( &configPath, "config-path", - os.ExpandEnv("$HOME/.kube/switch-config.yaml"), + getDefaultConfigPath(), "path on the local filesystem to the configuration file.") hookCmd.Flags().StringVar( diff --git a/cmd/switcher/switcher.go b/cmd/switcher/switcher.go index 056f831a..18279b29 100644 --- a/cmd/switcher/switcher.go +++ b/cmd/switcher/switcher.go @@ -441,6 +441,17 @@ func getKubeconfigPathFromFlag() string { return os.ExpandEnv(kubeconfigPath) } +// getDefaultConfigPath returns the default path to the switch configuration file. +// It can be overridden via the SWITCH_CONFIG_PATH environment variable, which is +// useful for directory-based environment managers such as direnv. When the +// environment variable is unset, it falls back to "$HOME/.kube/switch-config.yaml". +func getDefaultConfigPath() string { + if env := os.Getenv("SWITCH_CONFIG_PATH"); env != "" { + return os.ExpandEnv(env) + } + return os.ExpandEnv("$HOME/.kube/switch-config.yaml") +} + // isDuplicatePath searches through all kubeconfig stores in the switch-config.yaml and checks if the // given path is already configured in any of these stores // returns true if it is already configureed diff --git a/docs/kubeconfig_stores.md b/docs/kubeconfig_stores.md index 8c28c14f..dca84701 100644 --- a/docs/kubeconfig_stores.md +++ b/docs/kubeconfig_stores.md @@ -4,7 +4,12 @@ If you neither provide a flag or a `SwitchConfig` file, it will default to the file `~/.kube/config`. The `SwitchConfig` file is expected to be in the default location -on the local filesystem at `~/.kube/switch-config.yaml` or set via flag `--config-path`. +on the local filesystem at `~/.kube/switch-config.yaml`, set via flag `--config-path`, +or set via the `SWITCH_CONFIG_PATH` environment variable. The flag takes precedence +over the environment variable. Using `SWITCH_CONFIG_PATH` is handy with directory-based +environment managers such as [direnv](https://direnv.net/) to select a per-directory +configuration without shell aliases or wrapper scripts (e.g. add +`export SWITCH_CONFIG_PATH=$PWD/switch-config.yaml` to a directory's `.envrc`). Example config files can be found [here](../resources/demo-config-files) but also in the documentation for each store (see below).