<commit_before>// Copyright 2015 Google Inc. All Rights Reserved.
// Author: <EMAIL> (<NAME>)

// Utility code for working with OAuth. Subject to interface change.
package oauthutil

import (
	"encoding/json"
	"flag"
	"fmt"
	"log"
	"os"
	"os/user"
	"path"

	"golang.org/x/oauth2"
)

// Create an oauth2.TokenSource that retrieves tokens in the following
// preference order:
//
//  1. From a copy of the latest obtained token, if still valid.
//
//  2. From a cache on disk, when it contains a valid token. The supplied file
//     basename will be placed in the user's home directory.
//
//  3. From an oauth token refresher, seeded with the last valid cached token.
//
//  4. By exchanging a new authorization code obtained via the supplied flag.
//     If the flag has not been set, the program will be aborted with a helpful
//     message.
//
// Note in particular the warning about aborting the program in #4.
func NewTerribleTokenSource(
	config *oauth2.Config,
	authCodeFlag *flag.Flag,
	cacheFilename string) (oauth2.TokenSource, error) {
	// Expand the path to the token cache.
	homedir, err := getHomeDir()
	if err != nil {
		return nil, fmt.Errorf("Finding homedir: %v", err)
	}

	cachePath := path.Join(homedir, cacheFilename)
<commit_msg>Catch nil flags.<commit_after>// Copyright 2015 Google Inc. All Rights Reserved.
// Author: <EMAIL> (<NAME>)

// Utility code for working with OAuth. Subject to interface change.
package oauthutil

import (
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"log"
	"os"
	"os/user"
	"path"

	"golang.org/x/oauth2"
)

// Create an oauth2.TokenSource that retrieves tokens in the following
// preference order:
//
//  1. From a copy of the latest obtained token, if still valid.
//
//  2. From a cache on disk, when it contains a valid token. The supplied file
//     basename will be placed in the user's home directory.
//
//  3. From an oauth token refresher, seeded with the last valid cached token.
//
//  4. By exchanging a new authorization code obtained via the supplied flag.
//     If the flag has not been set, the program will be aborted with a helpful
//     message.
//
// Note in particular the warning about aborting the program in #4.
func NewTerribleTokenSource(
	config *oauth2.Config,
	authCodeFlag *flag.Flag,
	cacheFilename string) (oauth2.TokenSource, error) {
	// Catch the common mistake of an unknown flag.
	if authCodeFlag == nil {
		return nil, errors.New("NewTerribleTokenSource passed a nil flag.")
	}

	// Expand the path to the token cache.
	homedir, err := getHomeDir()
	if err != nil {
		return nil, fmt.Errorf("Finding homedir: %v", err)
	}

	cachePath := path.Join(homedir, cacheFilename)

{'fraction_non_alphanumeric': 0.1054013503375844, 'fraction_numerical': 0.009752438109527382, 'mean_word_length': 3.2672, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '2611927', 'n_tokens_mistral': 868, 'n_tokens_neox': 830, 'n_words': 347}