Skip to content

Commit

Permalink
Hello :)
Browse files Browse the repository at this point in the history
It's my first PR to an Go project here, so be kind to me :)

It's possible to set KUBECTX_DIR and KUBENS_DIR as an environemnt
variable.

Example:

> KUBECTX_DIR="/foo/bar"

will result in /foo/bar/.kube/kubectx

When KUBECTX_DIR is not set, kubectx and kubens fall back
to the default behavior.

issue: ahmetb#402
  • Loading branch information
mnlwldr committed Mar 19, 2024
1 parent 8fb8c9f commit 146ebef
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion cmd/kubectx/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package main

import (
"cmp"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -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")
}
Expand Down
3 changes: 2 additions & 1 deletion cmd/kubens/statefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package main

import (
"bytes"
"cmp"
"io/ioutil"
"os"
"path/filepath"
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion internal/kubeconfig/kubeconfigloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package kubeconfig

import (
"cmp"
"github.com/ahmetb/kubectx/internal/cmdutil"
"os"
"path/filepath"
Expand Down Expand Up @@ -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")
}
Expand Down
5 changes: 3 additions & 2 deletions kubectx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion kubens
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 146ebef

Please sign in to comment.