-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Structural refactoring for multiple kubeconfig support (#219)
- Loading branch information
Showing
17 changed files
with
134 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package cmdutil | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func HomeDir() string { | ||
if v := os.Getenv("XDG_CACHE_HOME"); v != "" { | ||
return v | ||
} | ||
home := os.Getenv("HOME") | ||
if home == "" { | ||
home = os.Getenv("USERPROFILE") // windows | ||
} | ||
return home | ||
} | ||
|
||
// IsNotFoundErr determines if the underlying error is os.IsNotExist. Right now | ||
// errors from github.com/pkg/errors doesn't work with os.IsNotExist. | ||
func IsNotFoundErr(err error) bool { | ||
for e := err; e != nil; e = errors.Unwrap(e) { | ||
if os.IsNotExist(e) { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package cmdutil | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/ahmetb/kubectx/internal/testutil" | ||
) | ||
|
||
func Test_homeDir(t *testing.T) { | ||
type env struct{ k, v string } | ||
cases := []struct { | ||
name string | ||
envs []env | ||
want string | ||
}{ | ||
{ | ||
name: "XDG_CACHE_HOME precedence", | ||
envs: []env{ | ||
{"XDG_CACHE_HOME", "xdg"}, | ||
{"HOME", "home"}, | ||
}, | ||
want: "xdg", | ||
}, | ||
{ | ||
name: "HOME over USERPROFILE", | ||
envs: []env{ | ||
{"HOME", "home"}, | ||
{"USERPROFILE", "up"}, | ||
}, | ||
want: "home", | ||
}, | ||
{ | ||
name: "only USERPROFILE available", | ||
envs: []env{ | ||
{"XDG_CACHE_HOME", ""}, | ||
{"HOME", ""}, | ||
{"USERPROFILE", "up"}, | ||
}, | ||
want: "up", | ||
}, | ||
{ | ||
name: "none available", | ||
envs: []env{ | ||
{"XDG_CACHE_HOME", ""}, | ||
{"HOME", ""}, | ||
{"USERPROFILE", ""}, | ||
}, | ||
want: "", | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.name, func(tt *testing.T) { | ||
var unsets []func() | ||
for _, e := range c.envs { | ||
unsets = append(unsets, testutil.WithEnvVar(e.k, e.v)) | ||
} | ||
|
||
got := HomeDir() | ||
if got != c.want { | ||
t.Errorf("expected:%q got:%q", c.want, got) | ||
} | ||
for _, u := range unsets { | ||
u() | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.