Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/switcher/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 1 addition & 3 deletions cmd/switcher/gardener.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
package switcher

import (
"os"

gardenercontrolplane "github.com/danielfoehrkn/kubeswitch/pkg/subcommands/gardener"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions cmd/switcher/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
11 changes: 11 additions & 0 deletions cmd/switcher/switcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion docs/kubeconfig_stores.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
Loading