From 13b2685442b9e73484598936950abcab8a6c4ff6 Mon Sep 17 00:00:00 2001 From: Philip Krauss Date: Sun, 14 Jan 2024 17:30:28 +0100 Subject: [PATCH] read from .env --- xata/utils.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/xata/utils.go b/xata/utils.go index 80db0d5..300c70f 100644 --- a/xata/utils.go +++ b/xata/utils.go @@ -161,11 +161,11 @@ func loadConfig(fieName string) (config, error) { return cfg, nil } -// getBranchName retrieves the branch name. -// If not found, falls back to defaultBranchName -func getBranchName() string { - if branchName, found := os.LookupEnv(branchNameEnvVar); found { - return branchName +// Get value from env var with fallback to godotenv +// return default value if not found +func getEnvVar(name string, defaultValue string) string { + if val, found := os.LookupEnv(name); found { + return val } var myEnv map[string]string @@ -177,20 +177,22 @@ func getBranchName() string { } } - if branchName, found := myEnv[branchNameEnvVar]; found { - return branchName + if val, found := myEnv[name]; found { + return val } + return defaultValue +} - return defaultBranchName +// getBranchName retrieves the branch name. +// If not found, falls back to defaultBranchName +func getBranchName() string { + return getEnvVar(branchNameEnvVar, defaultBranchName) } // Get the region if the corresponding env var `XATA_REGION` is set // otherwise return the default region: us-east-1 func getRegion() string { - if region, found := os.LookupEnv("XATA_REGION"); found { - return region - } - return defaultRegion + return getEnvVar("XATA_REGION", defaultRegion) } // loadDatabaseConfig will return config with defaults if the error is not nil. @@ -200,9 +202,11 @@ func loadDatabaseConfig() (databaseConfig, error) { branchName: defaultBranchName, domainWorkspace: defaultDataPlaneDomain, } + // Setup with env var // XATA_WORKSPACE_ID to set the workspace Id - if wsID, found := os.LookupEnv("XATA_WORKSPACE_ID"); found { + wsID := getEnvVar("XATA_WORKSPACE_ID", "") + if wsID != "" { region := getRegion() branch := getBranchName() db := databaseConfig{