Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try to prevent being stuck forever if cache file gets corrupted #13

Merged
merged 1 commit into from Aug 2, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

@@ -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()

This comment has been minimized.

Copy link
@yevgenypats

yevgenypats Aug 2, 2019

Collaborator

Why this line is removed?

This comment has been minimized.

Copy link
@kjk

kjk Aug 2, 2019

Author Contributor

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.

This comment has been minimized.

Copy link
@yevgenypats

yevgenypats Aug 2, 2019

Collaborator

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
}
}
@@ -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
}
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.