func (src *gcloudTokenSource) Token() (*oauth2.Token, error) {
	cfg, err := GcloudConfig()
	if err != nil {
		return nil, err
	}
	return cfg.oauthToken(), nil
}
Token helps gcloudTokenSource implement oauth2.TokenSource.
package util
import (
	"bytes"
	"context"
	"encoding/json"
	"fmt"
	"runtime"
	"time"
	"github.com/GoogleCloudPlatform/cloudsql-proxy/logging"
	"golang.org/x/oauth2"
	exec "golang.org/x/sys/execabs"
)
type GcloudConfigData struct {
	Configuration struct {
		Properties struct {
			Core struct {
				Project string
				Account string
			}
		}
	}
	Credential struct {
		AccessToken string    `json:"access_token"`
		TokenExpiry time.Time `json:"token_expiry"`
	}
}
func (cfg *GcloudConfigData) oauthToken() *oauth2.Token {
	return &oauth2.Token{
		AccessToken: cfg.Credential.AccessToken,
		Expiry:      cfg.Credential.TokenExpiry,
	}
}
type GcloudStatusCode int
const (
	GcloudOk GcloudStatusCode = iota
	GcloudNotFound
	GcloudExecErr
)
type GcloudError struct {
	GcloudError error
	Status      GcloudStatusCode
}
func (e *GcloudError) Error() string {
	return e.GcloudError.Error()
}
func GcloudConfig() (*GcloudConfigData, error) {
	gcloudCmd := "gcloud"
	if runtime.GOOS == "windows" {
		gcloudCmd = gcloudCmd + ".cmd"
	}
	if _, err := exec.LookPath(gcloudCmd); err != nil {
		return nil, &GcloudError{err, GcloudNotFound}
	}
	buf, errbuf := new(bytes.Buffer), new(bytes.Buffer)
	cmd := exec.Command(gcloudCmd, "--format", "json", "config", "config-helper", "--min-expiry", "1h")
	cmd.Stdout = buf
	cmd.Stderr = errbuf
	if err := cmd.Run(); err != nil {
		err = fmt.Errorf("error reading config: %v; stderr was:\n%v", err, errbuf)
		logging.Errorf("GcloudConfig: %v", err)
		return nil, &GcloudError{err, GcloudExecErr}
	}
	data := &GcloudConfigData{}
	if err := json.Unmarshal(buf.Bytes(), data); err != nil {
		logging.Errorf("Failed to unmarshal bytes from gcloud: %v", err)
		logging.Errorf("   gcloud returned:\n%s", buf)
		return nil, &GcloudError{err, GcloudExecErr}
	}
	return data, nil
}
type gcloudTokenSource struct {
}