Skip to content

Commit

Permalink
make env var names consts
Browse files Browse the repository at this point in the history
  • Loading branch information
philkra committed Jan 15, 2024
1 parent 13b2685 commit 52291e0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
7 changes: 4 additions & 3 deletions xata/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@ const (
personalAPIKeyLocation = "~/.config/xata/key"
defaultControlPlaneDomain = "api.xata.io"
xataAPIKeyEnvVar = "XATA_API_KEY"
xataWsIDEnvVar = "XATA_WORKSPACE_ID" // TODO: not in use yet
xataWsIDEnvVar = "XATA_WORKSPACE_ID"
dbURLFormat = "https://{workspace_id}.{region}.xata.sh/db/{db_name}:{branch_name}"
defaultBranchName = "main"
configFileName = ".xatarc"
branchNameEnvVar = "XATA_BRANCH"
defaultDataPlaneDomain = "xata.sh"
defaultRegion = "us-east-1"
regionEnvVar = "XATA_REGION"
)

var errAPIKey = fmt.Errorf("no API key found. Searched in `%s` env, %s, and .env", xataAPIKeyEnvVar, personalAPIKeyLocation)
Expand Down Expand Up @@ -192,7 +193,7 @@ func getBranchName() string {
// Get the region if the corresponding env var `XATA_REGION` is set
// otherwise return the default region: us-east-1
func getRegion() string {
return getEnvVar("XATA_REGION", defaultRegion)
return getEnvVar(regionEnvVar, defaultRegion)
}

// loadDatabaseConfig will return config with defaults if the error is not nil.
Expand All @@ -205,7 +206,7 @@ func loadDatabaseConfig() (databaseConfig, error) {

// Setup with env var
// XATA_WORKSPACE_ID to set the workspace Id
wsID := getEnvVar("XATA_WORKSPACE_ID", "")
wsID := getEnvVar(xataWsIDEnvVar, "")
if wsID != "" {
region := getRegion()
branch := getBranchName()
Expand Down
16 changes: 8 additions & 8 deletions xata/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Test_getRegion(t *testing.T) {
})

setRegion := "eu-west-3"
err := os.Setenv("XATA_REGION", setRegion)
err := os.Setenv(regionEnvVar, setRegion)
if err != nil {
t.Fatal(err)
}
Expand All @@ -67,7 +67,7 @@ func Test_getRegion(t *testing.T) {
assert.Equal(t, gotRegion, setRegion)
})

t.Cleanup(func() { os.Unsetenv("XATA_REGION") })
t.Cleanup(func() { os.Unsetenv(regionEnvVar) })
}

func Test_parseDatabaseURL(t *testing.T) {
Expand Down Expand Up @@ -176,7 +176,7 @@ func Test_loadConfig(t *testing.T) {

func Test_loadDatabaseConfig_with_envvars(t *testing.T) {
setWsId := "workspace-0lac00"
err := os.Setenv("XATA_WORKSPACE_ID", setWsId)
err := os.Setenv(xataWsIDEnvVar, setWsId)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -205,13 +205,13 @@ func Test_loadDatabaseConfig_with_envvars(t *testing.T) {
t.Fatal(err2)
}
setRegion := "ap-southeast-16"
err3 := os.Setenv("XATA_REGION", setRegion)
err3 := os.Setenv(regionEnvVar, setRegion)
if err3 != nil {
t.Fatal(err3)
}

// with branch and region env vars
t.Run("load config from XATA_WORKSPACE_ID, XATA_REGION and XATA_BRANCH env vars", func(t *testing.T) {
t.Run("load config from XATA_WORKSPACE_ID, regionEnvVar and XATA_BRANCH env vars", func(t *testing.T) {
dbCfg, err := loadDatabaseConfig()
if err != nil {
t.Fatalf("Error loading config: %v", err)
Expand All @@ -229,8 +229,8 @@ func Test_loadDatabaseConfig_with_envvars(t *testing.T) {
})

t.Cleanup(func() {
os.Unsetenv("XATA_WORKSPACE_ID")
os.Unsetenv("XATA_BRANCH")
os.Unsetenv("XATA_REGION")
os.Unsetenv(xataWsIDEnvVar)
os.Unsetenv(branchNameEnvVar)
os.Unsetenv(regionEnvVar)
})
}

0 comments on commit 52291e0

Please sign in to comment.