Skip to content

Commit

Permalink
Change to not use GOOGLE_APPLICATION_CREDENTIALS (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored May 25, 2022
1 parent f415cfe commit 22c44aa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 40 deletions.
26 changes: 5 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ const (
// optName
optNameProjectID = "project"
optNameDataset = "dataset"
optNameKeyFile = "keyfile"
optNameOutputFile = "output"
// envName
envNameGoogleApplicationCredentials = "GOOGLE_APPLICATION_CREDENTIALS"
envNameGCloudProjectID = "GCLOUD_PROJECT_ID"
envNameBigQueryDataset = "BIGQUERY_DATASET"
envNameOutputFile = "OUTPUT_FILE"
envNameGCloudProjectID = "GCLOUD_PROJECT_ID"
envNameBigQueryDataset = "BIGQUERY_DATASET"
envNameOutputFile = "OUTPUT_FILE"
// defaultValue
defaultValueEmpty = ""
defaultValueOutputFile = "bqschema.generated.go"
Expand All @@ -41,7 +39,6 @@ var (
// optValue
optValueProjectID = flag.String(optNameProjectID, defaultValueEmpty, "")
optValueDataset = flag.String(optNameDataset, defaultValueEmpty, "")
optValueKeyFile = flag.String(optNameKeyFile, defaultValueEmpty, "path to service account json key file")
optValueOutputPath = flag.String(optNameOutputFile, defaultValueEmpty, "path to output the generated code")
)

Expand All @@ -60,12 +57,6 @@ func main() {
func Run(ctx context.Context) (err error) {
flag.Parse()

var keyfile string
keyfile, err = getOptOrEnvOrDefault(optNameKeyFile, *optValueKeyFile, envNameGoogleApplicationCredentials, "")
if err != nil {
return fmt.Errorf("getOptOrEnvOrDefault: %w", err)
}

var project string
project, err = getOptOrEnvOrDefault(optNameProjectID, *optValueProjectID, envNameGCloudProjectID, "")
if err != nil {
Expand All @@ -84,20 +75,13 @@ func Run(ctx context.Context) (err error) {
return fmt.Errorf("getOptOrEnvOrDefault: %w", err)
}

// set GOOGLE_APPLICATION_CREDENTIALS for Google Cloud SDK
if os.Getenv(envNameGoogleApplicationCredentials) != keyfile {
if err = os.Setenv(envNameGoogleApplicationCredentials, keyfile); err != nil {
return fmt.Errorf("os.Setenv: %w", err)
}
}

client, err := bigquery.NewClient(ctx, project)
if err != nil {
return fmt.Errorf("bigquery.NewClient: %w", err)
}
defer func() {
if err = client.Close(); err != nil {
warnln("client.Close: " + err.Error())
if closeErr := client.Close(); closeErr != nil {
warnln("client.Close: " + closeErr.Error())
}
}()

Expand Down
39 changes: 20 additions & 19 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (

const (
// all
testEmptyString = ""
testEmptyString = ""
GOOGLE_APPLICATION_CREDENTIALS = "GOOGLE_APPLICATION_CREDENTIALS"

// generateTableSchemaCode, getAllTables
testPublicDataProjectID = "bigquery-public-data"
Expand Down Expand Up @@ -48,8 +49,8 @@ const (

func Test_Run(t *testing.T) {
t.Run("正常系_testPublicDataProjectID_"+testPublicDataProjectID+"_testSupportedDatasetID_"+testSupportedDatasetID, func(t *testing.T) {
if os.Getenv(envNameGoogleApplicationCredentials) == "" {
t.Skip("WARN: " + envNameGoogleApplicationCredentials + " is not set")
if os.Getenv(GOOGLE_APPLICATION_CREDENTIALS) == "" {
t.Skip("WARN: " + GOOGLE_APPLICATION_CREDENTIALS + " is not set")
}

// projectID
Expand Down Expand Up @@ -85,8 +86,8 @@ func Test_Run(t *testing.T) {

func Test_Generate(t *testing.T) {
t.Run("正常系_testSupportedDatasetID_"+testSupportedDatasetID, func(t *testing.T) {
if os.Getenv(envNameGoogleApplicationCredentials) == "" {
t.Skip("WARN: " + envNameGoogleApplicationCredentials + " is not set")
if os.Getenv(GOOGLE_APPLICATION_CREDENTIALS) == "" {
t.Skip("WARN: " + GOOGLE_APPLICATION_CREDENTIALS + " is not set")
}

var (
Expand All @@ -101,8 +102,8 @@ func Test_Generate(t *testing.T) {
})

t.Run("正常系_testNotSupportedDatasetID_"+testNotSupportedDatasetID, func(t *testing.T) {
if os.Getenv(envNameGoogleApplicationCredentials) == "" {
t.Skip("WARN: " + envNameGoogleApplicationCredentials + " is not set")
if os.Getenv(GOOGLE_APPLICATION_CREDENTIALS) == "" {
t.Skip("WARN: " + GOOGLE_APPLICATION_CREDENTIALS + " is not set")
}

var (
Expand Down Expand Up @@ -185,8 +186,8 @@ func Test_generateTableSchemaCode(t *testing.T) {
ctx = context.Background()
)

if os.Getenv(envNameGoogleApplicationCredentials) == "" {
t.Skip("WARN: " + envNameGoogleApplicationCredentials + " is not set")
if os.Getenv(GOOGLE_APPLICATION_CREDENTIALS) == "" {
t.Skip("WARN: " + GOOGLE_APPLICATION_CREDENTIALS + " is not set")
}

var (
Expand Down Expand Up @@ -227,8 +228,8 @@ func Test_generateTableSchemaCode(t *testing.T) {
ctx = context.Background()
)

if os.Getenv(envNameGoogleApplicationCredentials) == "" {
t.Skip("WARN: " + envNameGoogleApplicationCredentials + " is not set")
if os.Getenv(GOOGLE_APPLICATION_CREDENTIALS) == "" {
t.Skip("WARN: " + GOOGLE_APPLICATION_CREDENTIALS + " is not set")
}

var (
Expand All @@ -247,8 +248,8 @@ func Test_generateTableSchemaCode(t *testing.T) {
ctx = context.Background()
)

if os.Getenv(envNameGoogleApplicationCredentials) == "" {
t.Skip("WARN: " + envNameGoogleApplicationCredentials + " is not set")
if os.Getenv(GOOGLE_APPLICATION_CREDENTIALS) == "" {
t.Skip("WARN: " + GOOGLE_APPLICATION_CREDENTIALS + " is not set")
}

var (
Expand Down Expand Up @@ -280,8 +281,8 @@ func Test_generateTableSchemaCode(t *testing.T) {
func Test_getAllTables(t *testing.T) {
t.Run("正常系_testPublicDataProjectID_testSupportedDatasetID", func(t *testing.T) {

if os.Getenv(envNameGoogleApplicationCredentials) == "" {
t.Skip("WARN: " + envNameGoogleApplicationCredentials + " is not set")
if os.Getenv(GOOGLE_APPLICATION_CREDENTIALS) == "" {
t.Skip("WARN: " + GOOGLE_APPLICATION_CREDENTIALS + " is not set")
}

var (
Expand All @@ -296,14 +297,14 @@ func Test_getAllTables(t *testing.T) {

t.Run("異常系_testProjectNotFound_testDatasetNotFound", func(t *testing.T) {

backupValue, exist := os.LookupEnv(envNameGoogleApplicationCredentials)
_ = os.Setenv(envNameGoogleApplicationCredentials, testGoogleApplicationCredentials)
backupValue, exist := os.LookupEnv(GOOGLE_APPLICATION_CREDENTIALS)
_ = os.Setenv(GOOGLE_APPLICATION_CREDENTIALS, testGoogleApplicationCredentials)
defer func() {
if exist {
_ = os.Setenv(envNameGoogleApplicationCredentials, backupValue)
_ = os.Setenv(GOOGLE_APPLICATION_CREDENTIALS, backupValue)
return
}
_ = os.Unsetenv(envNameGoogleApplicationCredentials)
_ = os.Unsetenv(GOOGLE_APPLICATION_CREDENTIALS)
}()

var (
Expand Down

0 comments on commit 22c44aa

Please sign in to comment.