From 163241099f12fa3551a14c6de34076bc53d53875 Mon Sep 17 00:00:00 2001 From: Marc Brugger Date: Mon, 14 Nov 2022 16:27:04 +0000 Subject: [PATCH] make runnable on windows Signed-off-by: bakito --- cmd/common.go | 8 ++++++++ cmd/kubectl_token.go | 13 ++++++++++--- config/config.go | 5 ++--- main.go | 8 +++++++- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/cmd/common.go b/cmd/common.go index 8df88569..8931b90e 100644 --- a/cmd/common.go +++ b/cmd/common.go @@ -707,3 +707,11 @@ func tickerContext(ctx context.Context, duration time.Duration) <-chan time.Time }() return ticker.C } + +func ConfigDir() (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", err + } + return filepath.Join(homeDir, ".rancher"), nil +} diff --git a/cmd/kubectl_token.go b/cmd/kubectl_token.go index f27881ca..78b1c71e 100644 --- a/cmd/kubectl_token.go +++ b/cmd/kubectl_token.go @@ -17,6 +17,7 @@ import ( url2 "net/url" "os" "os/signal" + "runtime" "strings" "time" @@ -39,8 +40,6 @@ Example: $ rancher token delete all ` -var deleteCommandUsage = fmt.Sprintf("Delete cached token used for kubectl login at [%s] \n %s", os.ExpandEnv("${HOME}/.rancher"), deleteExample) - type LoginInput struct { server string userID string @@ -78,6 +77,14 @@ var supportedAuthProviders = map[string]bool{ } func CredentialCommand() cli.Command { + configDir, err := ConfigDir() + if err != nil { + if runtime.GOOS == "windows" { + configDir = "%HOME%\\.rancher" + } else { + configDir = "${HOME}/.rancher" + } + } return cli.Command{ Name: "token", Usage: "Authenticate and generate new kubeconfig token", @@ -111,7 +118,7 @@ func CredentialCommand() cli.Command { Subcommands: []cli.Command{ cli.Command{ Name: "delete", - Usage: deleteCommandUsage, + Usage: fmt.Sprintf("Delete cached token used for kubectl login at [%s] \n %s", configDir, deleteExample), Action: deleteCachedCredential, }, }, diff --git a/config/config.go b/config/config.go index 4bf48f4e..cac6a08f 100644 --- a/config/config.go +++ b/config/config.go @@ -5,7 +5,7 @@ import ( "fmt" "net/url" "os" - "path" + "path/filepath" "strings" "github.com/sirupsen/logrus" @@ -84,11 +84,10 @@ func GetFilePermissionWarnings(path string) ([]string, error) { } func (c Config) Write() error { - err := os.MkdirAll(path.Dir(c.Path), 0700) + err := os.MkdirAll(filepath.Dir(c.Path), 0700) if err != nil { return err } - logrus.Infof("Saving config to %s", c.Path) p := c.Path c.Path = "" diff --git a/main.go b/main.go index 122211e8..de121ec3 100644 --- a/main.go +++ b/main.go @@ -87,6 +87,12 @@ func mainErr() error { app.Version = VERSION app.Author = "Rancher Labs, Inc." app.Email = "" + + configDir, err := cmd.ConfigDir() + if err != nil { + return err + } + app.Flags = []cli.Flag{ cli.BoolFlag{ Name: "debug", @@ -96,7 +102,7 @@ func mainErr() error { Name: "config, c", Usage: "Path to rancher config", EnvVar: "RANCHER_CONFIG_DIR", - Value: os.ExpandEnv("${HOME}/.rancher"), + Value: configDir, }, } app.Commands = []cli.Command{