oauth2/google: simplify the mechanism for overriding gcloud config location

Change-Id: I360fac6b13d11dda221beaa6b46ff386adfc2ec8
Reviewed-on: https://go-review.googlesource.com/4670
Reviewed-by: David Symonds <dsymonds@golang.org>
Run-TryBot: David Symonds <dsymonds@golang.org>
This commit is contained in:
Dave Day 2015-02-12 15:46:13 +11:00
parent 864eccb6a0
commit d838a7d6be
4 changed files with 12 additions and 11 deletions

View File

@ -6,6 +6,7 @@ package google
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
@ -137,23 +138,20 @@ func (c *SDKConfig) Scopes() []string {
return c.conf.Scopes
}
func sdkConfigPath() (string, error) {
// sdkConfigPath tries to guess where the gcloud config is located.
// It can be overridden during tests.
var sdkConfigPath = func() (string, error) {
if runtime.GOOS == "windows" {
return filepath.Join(os.Getenv("APPDATA"), "gcloud"), nil
}
unixHomeDir = guessUnixHomeDir()
if unixHomeDir == "" {
return "", fmt.Errorf("unable to get current user home directory: os/user lookup failed; $HOME is empty")
homeDir := guessUnixHomeDir()
if homeDir == "" {
return "", errors.New("unable to get current user home directory: os/user lookup failed; $HOME is empty")
}
return filepath.Join(unixHomeDir, ".config", "gcloud"), nil
return filepath.Join(homeDir, ".config", "gcloud"), nil
}
var unixHomeDir string
func guessUnixHomeDir() string {
if unixHomeDir != "" {
return unixHomeDir
}
usr, err := user.Current()
if err == nil {
return usr.HomeDir

View File

@ -7,7 +7,10 @@ package google
import "testing"
func TestSDKConfig(t *testing.T) {
unixHomeDir = "testdata"
sdkConfigPath = func() (string, error) {
return "testdata/gcloud", nil
}
tests := []struct {
account string
accessToken string