mirror of
https://github.com/ahmetb/kubectx.git
synced 2026-03-16 19:02:14 +00:00
Compare commits
1 Commits
dependabot
...
abalkan/xd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ec39a19beb |
@@ -24,11 +24,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func kubectxPrevCtxFile() (string, error) {
|
func kubectxPrevCtxFile() (string, error) {
|
||||||
home := cmdutil.HomeDir()
|
dir := cmdutil.CacheDir()
|
||||||
if home == "" {
|
if dir == "" {
|
||||||
return "", errors.New("HOME or USERPROFILE environment variable not set")
|
return "", errors.New("HOME or USERPROFILE environment variable not set")
|
||||||
}
|
}
|
||||||
return filepath.Join(home, ".kube", "kubectx"), nil
|
return filepath.Join(dir, "kubectx"), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// readLastContext returns the saved previous context
|
// readLastContext returns the saved previous context
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ func Test_writeLastContext(t *testing.T) {
|
|||||||
|
|
||||||
func Test_kubectxFilePath(t *testing.T) {
|
func Test_kubectxFilePath(t *testing.T) {
|
||||||
t.Setenv("HOME", filepath.FromSlash("/foo/bar"))
|
t.Setenv("HOME", filepath.FromSlash("/foo/bar"))
|
||||||
|
t.Setenv("XDG_CACHE_HOME", "")
|
||||||
|
|
||||||
expected := filepath.Join(filepath.FromSlash("/foo/bar"), ".kube", "kubectx")
|
expected := filepath.Join(filepath.FromSlash("/foo/bar"), ".kube", "kubectx")
|
||||||
v, err := kubectxPrevCtxFile()
|
v, err := kubectxPrevCtxFile()
|
||||||
@@ -84,6 +85,19 @@ func Test_kubectxFilePath(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_kubectxFilePath_xdgCacheHome(t *testing.T) {
|
||||||
|
t.Setenv("XDG_CACHE_HOME", filepath.FromSlash("/tmp/xdg-cache"))
|
||||||
|
|
||||||
|
expected := filepath.Join(filepath.FromSlash("/tmp/xdg-cache"), "kubectx")
|
||||||
|
v, err := kubectxPrevCtxFile()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if v != expected {
|
||||||
|
t.Fatalf("expected=\"%s\" got=\"%s\"", expected, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func Test_kubectxFilePath_error(t *testing.T) {
|
func Test_kubectxFilePath_error(t *testing.T) {
|
||||||
t.Setenv("HOME", "")
|
t.Setenv("HOME", "")
|
||||||
t.Setenv("USERPROFILE", "")
|
t.Setenv("USERPROFILE", "")
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import (
|
|||||||
"github.com/ahmetb/kubectx/internal/cmdutil"
|
"github.com/ahmetb/kubectx/internal/cmdutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
var defaultDir = filepath.Join(cmdutil.HomeDir(), ".kube", "kubens")
|
var defaultDir = filepath.Join(cmdutil.CacheDir(), "kubens")
|
||||||
|
|
||||||
type NSFile struct {
|
type NSFile struct {
|
||||||
dir string
|
dir string
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ package cmdutil
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
)
|
)
|
||||||
|
|
||||||
func HomeDir() string {
|
func HomeDir() string {
|
||||||
@@ -27,6 +28,19 @@ func HomeDir() string {
|
|||||||
return home
|
return home
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CacheDir returns XDG_CACHE_HOME if set, otherwise $HOME/.kube,
|
||||||
|
// matching the bash scripts' behavior: ${XDG_CACHE_HOME:-$HOME/.kube}.
|
||||||
|
func CacheDir() string {
|
||||||
|
if xdg := os.Getenv("XDG_CACHE_HOME"); xdg != "" {
|
||||||
|
return xdg
|
||||||
|
}
|
||||||
|
home := HomeDir()
|
||||||
|
if home == "" {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return filepath.Join(home, ".kube")
|
||||||
|
}
|
||||||
|
|
||||||
// IsNotFoundErr determines if the underlying error is os.IsNotExist.
|
// IsNotFoundErr determines if the underlying error is os.IsNotExist.
|
||||||
func IsNotFoundErr(err error) bool {
|
func IsNotFoundErr(err error) bool {
|
||||||
for e := err; e != nil; e = errors.Unwrap(e) {
|
for e := err; e != nil; e = errors.Unwrap(e) {
|
||||||
|
|||||||
@@ -15,6 +15,7 @@
|
|||||||
package cmdutil
|
package cmdutil
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/ahmetb/kubectx/internal/testutil"
|
"github.com/ahmetb/kubectx/internal/testutil"
|
||||||
@@ -78,3 +79,29 @@ func Test_homeDir(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCacheDir(t *testing.T) {
|
||||||
|
t.Run("XDG_CACHE_HOME set", func(t *testing.T) {
|
||||||
|
t.Setenv("XDG_CACHE_HOME", "/tmp/xdg-cache")
|
||||||
|
t.Setenv("HOME", "/home/user")
|
||||||
|
if got := CacheDir(); got != "/tmp/xdg-cache" {
|
||||||
|
t.Errorf("expected:%q got:%q", "/tmp/xdg-cache", got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("XDG_CACHE_HOME unset, falls back to HOME/.kube", func(t *testing.T) {
|
||||||
|
t.Setenv("XDG_CACHE_HOME", "")
|
||||||
|
t.Setenv("HOME", "/home/user")
|
||||||
|
want := filepath.Join("/home/user", ".kube")
|
||||||
|
if got := CacheDir(); got != want {
|
||||||
|
t.Errorf("expected:%q got:%q", want, got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
t.Run("neither set", func(t *testing.T) {
|
||||||
|
t.Setenv("XDG_CACHE_HOME", "")
|
||||||
|
t.Setenv("HOME", "")
|
||||||
|
t.Setenv("USERPROFILE", "")
|
||||||
|
if got := CacheDir(); got != "" {
|
||||||
|
t.Errorf("expected:%q got:%q", "", got)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user