Skip to content
This repository was archived by the owner on Apr 30, 2021. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions client/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,41 @@ package client

import (
"bytes"
"cloud.google.com/go/firestore"
"context"
"encoding/json"
"errors"
"fmt"
"golang.org/x/oauth2"
"google.golang.org/api/option"
"io/ioutil"
"log"
"os"
"os/user"
"path"
"time"

"cloud.google.com/go/firestore"
"golang.org/x/oauth2"
"google.golang.org/api/option"
)

func (c *fuzzitClient) ReAuthenticate(force bool) error {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
CacheFile := path.Join(usr.HomeDir, ".fuzzit.cache")
cacheFile := path.Join(usr.HomeDir, ".fuzzit.cache")

if !force {
file, err := os.Open(CacheFile)
file, err := os.Open(cacheFile)
if err != nil {
return err
}
defer file.Close()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why this line is removed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's moved after json.NewDecoder() because on e.g. windows one cannot remove opened file so we have to call file.Close() before os.Remove() and defer file.Close() means it would be called after.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. nice!


err = json.NewDecoder(file).Decode(c)
file.Close()
if err != nil {
// try to prevent being stuck forever if cache file gets corrupted
os.Remove(cacheFile) // if a file
os.RemoveAll(cacheFile) // if a directory
return err
}
}
Expand Down Expand Up @@ -76,7 +80,7 @@ func (c *fuzzitClient) ReAuthenticate(force bool) error {
if err != nil {
return err
}
err = ioutil.WriteFile(CacheFile, cBytes, 0644)
err = ioutil.WriteFile(cacheFile, cBytes, 0644)
if err != nil {
return err
}
Expand Down