3 Commits

Author SHA1 Message Date
Ahmet Alp Balkan
979012e094 Release v0.9.4 2021-07-06 15:24:06 -07:00
peter woodman
ff2f9661a2 stop using XDG_CACHE_HOME as home directory (#299)
* stop using XDG_CACHE_HOME as home directory

XDG_CACHE_HOME is not a substitute for $HOME, see [1].

[1]: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

* fix bats testing setup/teardown

since cmdutil.Homedir() would treat $XDG_CACHE_HOME as $HOME, deleting
$XDG_CACHE_HOME would wipe out previous kubens state. now that we're not
doing that, we need to make a real synthetic $HOME and clear it out so
that $HOME/.kube/kubens doesn't persist between runs.
2021-05-28 16:09:32 -07:00
Ahmet Alp Balkan
34e9024835 add -V/--version flag to go implementations (#295)
Uses goreleaser to pass ldflags.

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
2021-04-09 09:43:48 -07:00
9 changed files with 58 additions and 13 deletions

View File

@@ -52,6 +52,9 @@ func parseArgs(argv []string) Op {
if v == "--help" || v == "-h" {
return HelpOp{}
}
if v == "--version" || v == "-V" {
return VersionOp{}
}
if v == "--current" || v == "-c" {
return CurrentOp{}
}

View File

@@ -39,11 +39,12 @@ func printUsage(out io.Writer) error {
%PROG% -c, --current : show the current context name
%PROG% <NEW_NAME>=<NAME> : rename context <NAME> to <NEW_NAME>
%PROG% <NEW_NAME>=. : rename current-context to <NEW_NAME>
%PROG% -u, --unset : unset the current context
%PROG% -u, --unset : unset the current context
%PROG% -d <NAME> [<NAME...>] : delete context <NAME> ('.' for current-context)
%SPAC% (this command won't delete the user/cluster entry
%SPAC% referenced by the context entry)
%PROG% -h,--help : show this message`
%PROG% -h,--help : show this message
%PROG% -V,--version : show version`
help = strings.ReplaceAll(help, "%PROG%", selfName())
help = strings.ReplaceAll(help, "%SPAC%", strings.Repeat(" ", len(selfName())))

20
cmd/kubectx/version.go Normal file
View File

@@ -0,0 +1,20 @@
package main
import (
"fmt"
"io"
"github.com/pkg/errors"
)
var (
version = "v0.0.0+unknown" // populated by goreleaser
)
// VersionOps describes printing version string.
type VersionOp struct{}
func (_ VersionOp) Run(stdout, _ io.Writer) error {
_, err := fmt.Fprintf(stdout, "%s\n", version)
return errors.Wrap(err, "write error")
}

View File

@@ -45,6 +45,9 @@ func parseArgs(argv []string) Op {
if v == "--help" || v == "-h" {
return HelpOp{}
}
if v == "--version" || v == "-V" {
return VersionOp{}
}
if v == "--current" || v == "-c" {
return CurrentOp{}
}

View File

@@ -38,7 +38,8 @@ func printUsage(out io.Writer) error {
%PROG% - : switch to the previous namespace in this context
%PROG% -c, --current : show the current namespace
%PROG% -h,--help : show this message
`
%PROG% -V,--version : show version`
// TODO this replace logic is duplicated between this and kubectx
help = strings.ReplaceAll(help, "%PROG%", selfName())

20
cmd/kubens/version.go Normal file
View File

@@ -0,0 +1,20 @@
package main
import (
"fmt"
"io"
"github.com/pkg/errors"
)
var (
version = "v0.0.0+unknown" // populated by goreleaser
)
// VersionOps describes printing version string.
type VersionOp struct{}
func (_ VersionOp) Run(stdout, _ io.Writer) error {
_, err := fmt.Fprintf(stdout, "%s\n", version)
return errors.Wrap(err, "write error")
}

View File

@@ -21,9 +21,6 @@ import (
)
func HomeDir() string {
if v := os.Getenv("XDG_CACHE_HOME"); v != "" {
return v
}
home := os.Getenv("HOME")
if home == "" {
home = os.Getenv("USERPROFILE") // windows

View File

@@ -28,12 +28,12 @@ func Test_homeDir(t *testing.T) {
want string
}{
{
name: "XDG_CACHE_HOME precedence",
name: "don't use XDG_CACHE_HOME as homedir",
envs: []env{
{"XDG_CACHE_HOME", "xdg"},
{"HOME", "home"},
},
want: "xdg",
want: "home",
},
{
name: "HOME over USERPROFILE",
@@ -46,7 +46,6 @@ func Test_homeDir(t *testing.T) {
{
name: "only USERPROFILE available",
envs: []env{
{"XDG_CACHE_HOME", ""},
{"HOME", ""},
{"USERPROFILE", "up"},
},
@@ -55,7 +54,6 @@ func Test_homeDir(t *testing.T) {
{
name: "none available",
envs: []env{
{"XDG_CACHE_HOME", ""},
{"HOME", ""},
{"USERPROFILE", ""},
},

View File

@@ -2,13 +2,15 @@
# bats setup function
setup() {
export XDG_CACHE_HOME="$(mktemp -d)"
export KUBECONFIG="${XDG_CACHE_HOME}/config"
TEMP_HOME="$(mktemp -d)"
export TEMP_HOME
export HOME=$TEMP_HOME
export KUBECONFIG="${TEMP_HOME}/config"
}
# bats teardown function
teardown() {
rm -rf "$XDG_CACHE_HOME"
rm -rf "$TEMP_HOME"
}
use_config() {