Skip to content

Commit

Permalink
RAI-24760 Handle token directory creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ginal committed Apr 12, 2024
1 parent 4614e6d commit 05f6fcf
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions rai/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ func cacheName() (string, error) {
return path.Join(usr.HomeDir, ".rai", "tokens.json"), nil
}

// Returns the directory of the token cache file.
func cacheDir() (string, error) {
usr, err := user.Current()
if err != nil {
return "", err
}
return path.Join(usr.HomeDir, ".rai"), nil
}

// Read the access token corresponding to the given ClientID from the local
// token cache, returns nil if the token does not exist.
func readAccessToken(creds *ClientCredentials) (*AccessToken, error) {
Expand Down Expand Up @@ -107,12 +116,22 @@ func writeAccessToken(clientID string, token *AccessToken) {
}

func writeTokenCache(cache map[string]*AccessToken) {
dname, err := cacheDir()
if err != nil {
fmt.Println(errors.Wrapf(err, "failed to find token directory"))
} else {
err = os.MkdirAll(dname, 0775)
if err != nil {
fmt.Println(errors.Wrapf(err, "failed to create token directory"))
}
}
fname, err := cacheName()
if err != nil {
return
}
f, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
fmt.Println(errors.Wrapf(err, "failed to open token file"))
return
}
if err := json.NewEncoder(f).Encode(cache); err != nil {
Expand Down

0 comments on commit 05f6fcf

Please sign in to comment.