Skip to content

Commit

Permalink
make runnable on windows
Browse files Browse the repository at this point in the history
Signed-off-by: bakito <[email protected]>
  • Loading branch information
bakito authored and HarrisonWAffel committed Mar 19, 2024
1 parent 5d18482 commit 1632410
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 7 deletions.
8 changes: 8 additions & 0 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 10 additions & 3 deletions cmd/kubectl_token.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
url2 "net/url"
"os"
"os/signal"
"runtime"
"strings"
"time"

Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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,
},
},
Expand Down
5 changes: 2 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"net/url"
"os"
"path"
"path/filepath"
"strings"

"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -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 = ""
Expand Down
8 changes: 7 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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{
Expand Down

0 comments on commit 1632410

Please sign in to comment.