diff --git a/cmd/kubectx/state.go b/cmd/kubectx/state.go index d6571594..8090c0aa 100644 --- a/cmd/kubectx/state.go +++ b/cmd/kubectx/state.go @@ -15,6 +15,7 @@ package main import ( + "cmp" "io/ioutil" "os" "path/filepath" @@ -25,7 +26,7 @@ import ( ) func kubectxPrevCtxFile() (string, error) { - home := cmdutil.HomeDir() + home := cmp.Or(os.Getenv("KUBECTX_DIR"), cmdutil.HomeDir()) if home == "" { return "", errors.New("HOME or USERPROFILE environment variable not set") } diff --git a/cmd/kubens/statefile.go b/cmd/kubens/statefile.go index b4e6305a..583ff79c 100644 --- a/cmd/kubens/statefile.go +++ b/cmd/kubens/statefile.go @@ -16,6 +16,7 @@ package main import ( "bytes" + "cmp" "io/ioutil" "os" "path/filepath" @@ -25,7 +26,7 @@ import ( "github.com/ahmetb/kubectx/internal/cmdutil" ) -var defaultDir = filepath.Join(cmdutil.HomeDir(), ".kube", "kubens") +var defaultDir = filepath.Join(cmp.Or(os.Getenv("KUBECTX_DIR"), cmdutil.HomeDir()), ".kube", "kubens") type NSFile struct { dir string diff --git a/internal/kubeconfig/kubeconfigloader.go b/internal/kubeconfig/kubeconfigloader.go index 2fa26b8d..7613befe 100644 --- a/internal/kubeconfig/kubeconfigloader.go +++ b/internal/kubeconfig/kubeconfigloader.go @@ -15,6 +15,7 @@ package kubeconfig import ( + "cmp" "github.com/ahmetb/kubectx/internal/cmdutil" "os" "path/filepath" @@ -68,7 +69,7 @@ func kubeconfigPath() (string, error) { } // default path - home := cmdutil.HomeDir() + home := cmp.Or(os.Getenv("KUBECTX_DIR"), cmdutil.HomeDir()) if home == "" { return "", errors.New("HOME or USERPROFILE environment variable not set") } diff --git a/kubectx b/kubectx index 12ea40c1..cbc6f076 100755 --- a/kubectx +++ b/kubectx @@ -16,14 +16,15 @@ # See the License for the specific language governing permissions and # limitations under the License. -[[ -n $DEBUG ]] && set -x +[[ -n $DEBUG ]] && set -xKUBECTX set -eou pipefail IFS=$'\n\t' SELF_CMD="$0" -KUBECTX="${XDG_CACHE_HOME:-$HOME/.kube}/kubectx" +[ -v KUBECTX_DIR ] && KUBECTX_DIR=${KUBECTX_DIR}/.kube/kubectx +KUBECTX="${KUBECTX_DIR:-${XDG_CACHE_HOME:-$HOME/.kube}/kubectx}" usage() { local SELF diff --git a/kubens b/kubens index 8d5df32e..1451c03a 100755 --- a/kubens +++ b/kubens @@ -23,7 +23,8 @@ IFS=$'\n\t' SELF_CMD="$0" -KUBENS_DIR="${XDG_CACHE_HOME:-$HOME/.kube}/kubens" +[ -v KUBENS_DIR ] && KUBENS_DIR=${KUBENS_DIR}/.kube/kubectx +KUBENS_DIR="${KUBENS_DIR:-${XDG_CACHE_HOME:-$HOME/.kube}/kubectx}" usage() { local SELF