<commit_before>package auth

import (
    "golang.org/x/oauth2"
    "encoding/json"
    "io/ioutil"
)


func FileSource(path string, token *oauth2.Token, conf *oauth2.Config) oauth2.TokenSource {
    return &fileSource{
        tokenPath: path,
        tokenSource: conf.TokenSource(oauth2.NoContext, token),
    }
}

type fileSource struct {
    tokenPath string
    tokenSource oauth2.TokenSource
}

func (self *fileSource) Token() (*oauth2.Token, error) {
    token, err := self.tokenSource.Token()
    if err != nil {
        return token, err
    }

    // Save token to file
    SaveToken(self.tokenPath, token)

    return token, nil
}

func ReadToken(path string) (*oauth2.Token, bool, error) {
    if !fileExists(path) {
        return nil, false, nil
    }

    content, err := ioutil.ReadFile(path)
    if err != nil {
        return nil, true, err
    }
    token := &oauth2.Token{}
    return token, true, json.Unmarshal(content, token)
}

func SaveToken(path string, token *oauth2.Token) error {
    data, err := json.MarshalIndent(token, "", "  ")
    if err != nil {
        return err
    }

    if err = mkdir(path); err != nil {
        return err
    }
    return ioutil.WriteFile(path, data, 0600)
}<commit_msg>Write to temp file first<commit_after>package auth

import (
    "golang.org/x/oauth2"
    "encoding/json"
    "os"
    "io/ioutil"
)


func FileSource(path string, token *oauth2.Token, conf *oauth2.Config) oauth2.TokenSource {
    return &fileSource{
        tokenPath: path,
        tokenSource: conf.TokenSource(oauth2.NoContext, token),
    }
}

type fileSource struct {
    tokenPath string
    tokenSource oauth2.TokenSource
}

func (self *fileSource) Token() (*oauth2.Token, error) {
    token, err := self.tokenSource.Token()
    if err != nil {
        return token, err
    }

    // Save token to file
    SaveToken(self.tokenPath, token)

    return token, nil
}

func ReadToken(path string) (*oauth2.Token, bool, error) {
    if !fileExists(path) {
        return nil, false, nil
    }

    content, err := ioutil.ReadFile(path)
    if err != nil {
        return nil, true, err
    }
    token := &oauth2.Token{}
    return token, true, json.Unmarshal(content, token)
}

func SaveToken(path string, token *oauth2.Token) error {
    data, err := json.MarshalIndent(token, "", "  ")
    if err != nil {
        return err
    }

    if err = mkdir(path); err != nil {
        return err
    }

    // Write to temp file first
    tmpFile := path + ".tmp"
    err = ioutil.WriteFile(tmpFile, data, 0600)
    if err != nil {
        os.Remove(tmpFile)
        return err
    }

    // Move file to correct path
    return os.Rename(tmpFile, path)
}
{'fraction_non_alphanumeric': 0.1204460966542751, 'fraction_numerical': 0.010408921933085501, 'mean_word_length': 2.526867627785059, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4573258', 'n_tokens_mistral': 918, 'n_tokens_neox': 892, 'n_words': 239}