From 22c44aa7d223c0de6df27ae9046cb065ed9d1584 Mon Sep 17 00:00:00 2001 From: Gino <29125616+ginokent@users.noreply.github.com> Date: Wed, 25 May 2022 16:55:14 +0900 Subject: [PATCH] Change to not use GOOGLE_APPLICATION_CREDENTIALS (#27) --- main.go | 26 +++++--------------------- main_test.go | 39 ++++++++++++++++++++------------------- 2 files changed, 25 insertions(+), 40 deletions(-) diff --git a/main.go b/main.go index 85b2cfe..2431c1e 100644 --- a/main.go +++ b/main.go @@ -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" @@ -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") ) @@ -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 { @@ -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()) } }() diff --git a/main_test.go b/main_test.go index f6fcef7..01b2ab9 100644 --- a/main_test.go +++ b/main_test.go @@ -13,7 +13,8 @@ import ( const ( // all - testEmptyString = "" + testEmptyString = "" + GOOGLE_APPLICATION_CREDENTIALS = "GOOGLE_APPLICATION_CREDENTIALS" // generateTableSchemaCode, getAllTables testPublicDataProjectID = "bigquery-public-data" @@ -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 @@ -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 ( @@ -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 ( @@ -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 ( @@ -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 ( @@ -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 ( @@ -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 ( @@ -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 (