Skip to content

Commit

Permalink
repository: Expand ~ in homedir (#489)
Browse files Browse the repository at this point in the history
Fixes: #488
  • Loading branch information
Mattias- committed Mar 14, 2021
1 parent f849094 commit 97cc85d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/google/go-cmp v0.5.5
github.com/google/wire v0.5.0
github.com/int128/oauth2cli v1.13.0
github.com/mitchellh/go-homedir v1.1.0
github.com/pkg/browser v0.0.0-20210115035449-ce105d075bb4
github.com/spf13/cobra v1.1.3
github.com/spf13/pflag v1.0.5
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/go-homedir v1.0.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/mitchellh/gox v0.4.0/go.mod h1:Sd9lOJ0+aimLBi73mGofS1ycjY8lL3uZM3JPS42BGNg=
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"github.com/google/wire"
"github.com/int128/kubelogin/pkg/infrastructure/logger"
"github.com/spf13/cobra"
"k8s.io/client-go/util/homedir"
)

// Set provides an implementation and interface for Cmd.
Expand All @@ -24,7 +23,7 @@ type Interface interface {
}

var defaultListenAddress = []string{"127.0.0.1:8000", "127.0.0.1:18000"}
var defaultTokenCacheDir = homedir.HomeDir() + "/.kube/cache/oidc-login"
var defaultTokenCacheDir = "~/.kube/cache/oidc-login"

const defaultAuthenticationTimeoutSec = 180

Expand Down
9 changes: 9 additions & 0 deletions pkg/tokencache/repository/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/google/wire"
"github.com/int128/kubelogin/pkg/oidc"
"github.com/int128/kubelogin/pkg/tokencache"
homedir "github.com/mitchellh/go-homedir"
)

//go:generate mockgen -destination mock_repository/mock_repository.go github.com/int128/kubelogin/pkg/tokencache/repository Interface
Expand Down Expand Up @@ -41,6 +42,10 @@ func (r *Repository) FindByKey(dir string, key tokencache.Key) (*oidc.TokenSet,
if err != nil {
return nil, fmt.Errorf("could not compute the key: %w", err)
}
dir, err = homedir.Expand(dir)
if err != nil {
return nil, fmt.Errorf("could not expand homedir: %w", err)
}
p := filepath.Join(dir, filename)
f, err := os.Open(p)
if err != nil {
Expand All @@ -59,6 +64,10 @@ func (r *Repository) FindByKey(dir string, key tokencache.Key) (*oidc.TokenSet,
}

func (r *Repository) Save(dir string, key tokencache.Key, tokenSet oidc.TokenSet) error {
dir, err := homedir.Expand(dir)
if err != nil {
return fmt.Errorf("could not expand homedir: %w", err)
}
if err := os.MkdirAll(dir, 0700); err != nil {
return fmt.Errorf("could not create directory %s: %w", dir, err)
}
Expand Down

0 comments on commit 97cc85d

Please sign in to comment.