Skip to content

Commit

Permalink
Export environment variable constants
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanslade committed Jan 15, 2024
1 parent 76f3edb commit cc238a9
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
7 changes: 3 additions & 4 deletions internal/integration-tests/databases_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import (
"os"
"testing"

"github.com/stretchr/testify/assert"

"github.com/hashicorp/go-retryablehttp"
"github.com/stretchr/testify/assert"
"github.com/xataio/xata-go/xata"
)

func Test_databasesClient(t *testing.T) {
apiKey, found := os.LookupEnv("XATA_API_KEY")
apiKey, found := os.LookupEnv(xata.EnvXataAPIKey)
if !found {
t.Skipf("%s not found in env vars", "XATA_API_KEY")
t.Skipf("%s not found in env vars", xata.EnvXataAPIKey)
}

ctx := context.Background()
Expand Down
10 changes: 4 additions & 6 deletions internal/integration-tests/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"net/http"
"os"

//"strings"

"github.com/hashicorp/go-retryablehttp"
"github.com/xataio/xata-go/xata"
)
Expand All @@ -28,15 +26,15 @@ type config struct {

func setupDatabase() (*config, error) {
ctx := context.Background()
apiKey, found := os.LookupEnv("XATA_API_KEY")
apiKey, found := os.LookupEnv(xata.EnvXataAPIKey)
if !found {
return nil, fmt.Errorf("%s not found in env vars", "XATA_API_KEY")
return nil, fmt.Errorf("%s not found in env vars", xata.EnvXataAPIKey)
}
// require workspace ID to come from the env var
// instead of creating new workspace on each client
wsID, found := os.LookupEnv("XATA_WORKSPACE_ID")
wsID, found := os.LookupEnv(xata.EnvXataWorkspaceID)
if !found {
return nil, fmt.Errorf("%s not found in env vars", "XATA_WORKSPACE_ID")
return nil, fmt.Errorf("%s not found in env vars", xata.EnvXataWorkspaceID)
}

testID := testIdentifier()
Expand Down
4 changes: 2 additions & 2 deletions internal/integration-tests/users_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
)

func Test_usersClient(t *testing.T) {
apiKey, found := os.LookupEnv("XATA_API_KEY")
apiKey, found := os.LookupEnv(xata.EnvXataAPIKey)
if !found {
t.Skipf("%s not found in env vars", "XATA_API_KEY")
t.Skipf("%s not found in env vars", xata.EnvXataAPIKey)
}

t.Run("should get the current user", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions internal/integration-tests/workspaces_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (
)

func Test_workspacesClient(t *testing.T) {
apiKey, found := os.LookupEnv("XATA_API_KEY")
apiKey, found := os.LookupEnv(xata.EnvXataAPIKey)
if !found {
t.Skipf("%s not found in env vars", "XATA_API_KEY")
t.Skipf("%s not found in env vars", xata.EnvXataAPIKey)
}

t.Run("should create, get, list, update and delete workspace", func(t *testing.T) {
Expand Down
28 changes: 16 additions & 12 deletions xata/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,34 @@ import (
"github.com/joho/godotenv"
)

// Environment variables
const (
EnvXataAPIKey = "XATA_API_KEY"
EnvXataWorkspaceID = "XATA_WORKSPACE_ID"
EnvXataBranch = "XATA_BRANCH"
EnvXataRegion = "XATA_REGION"
)

const (
personalAPIKeyLocation = "~/.config/xata/key"
defaultControlPlaneDomain = "api.xata.io"
xataAPIKeyEnvVar = "XATA_API_KEY"
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)
var errAPIKey = fmt.Errorf("no API key found. Searched in `%s` env, %s, and .env", EnvXataAPIKey, personalAPIKeyLocation)

// assignAPIkey add the API key to the ClientOptions by going through the following options in order:
// - In env vars by the xataAPIKeyEnvVar dbName.
// - In .env file with the xataAPIKeyEnvVar dbName.
// - In env vars by the EnvXataAPIKey dbName.
// - In .env file with the EnvXataAPIKey dbName.
// - In .xatarc config file (TODO: not ready!)
//
// See: https://xata.io/docs/python-sdk/overview#authorization
func getAPIKey() (string, error) {
if key, found := os.LookupEnv(xataAPIKeyEnvVar); found {
if key, found := os.LookupEnv(EnvXataAPIKey); found {
return key, nil
}

Expand All @@ -50,7 +54,7 @@ func getAPIKey() (string, error) {
}
}

if key, found := myEnv[xataAPIKeyEnvVar]; found {
if key, found := myEnv[EnvXataAPIKey]; found {
return key, nil
}

Expand Down Expand Up @@ -187,13 +191,13 @@ func getEnvVar(name string, defaultValue string) string {
// getBranchName retrieves the branch name.
// If not found, falls back to defaultBranchName
func getBranchName() string {
return getEnvVar(branchNameEnvVar, defaultBranchName)
return getEnvVar(EnvXataBranch, defaultBranchName)
}

// 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(regionEnvVar, defaultRegion)
return getEnvVar(EnvXataRegion, defaultRegion)
}

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

// Setup with env var
// XATA_WORKSPACE_ID to set the workspace Id
wsID := getEnvVar(xataWsIDEnvVar, "")
wsID := getEnvVar(EnvXataWorkspaceID, "")
if wsID != "" {
region := getRegion()
branch := getBranchName()
Expand Down
24 changes: 12 additions & 12 deletions xata/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import (

func TestClientOptions_getAPIKey(t *testing.T) {
apiKeyFromEnv := "test-API-key-from-env"
err := os.Setenv(xataAPIKeyEnvVar, apiKeyFromEnv)
err := os.Setenv(EnvXataAPIKey, apiKeyFromEnv)
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.Unsetenv(xataAPIKeyEnvVar) })
t.Cleanup(func() { os.Unsetenv(EnvXataAPIKey) })

t.Run("should assign the API key from the env vars", func(t *testing.T) {
apiKey, err := getAPIKey()
Expand All @@ -34,7 +34,7 @@ func Test_getBranchName(t *testing.T) {
})

setBranchName := "feature-042"
err := os.Setenv(branchNameEnvVar, setBranchName)
err := os.Setenv(EnvXataBranch, setBranchName)
if err != nil {
t.Fatal(err)
}
Expand All @@ -45,7 +45,7 @@ func Test_getBranchName(t *testing.T) {
assert.Equal(t, gotBranchName, setBranchName)
})

t.Cleanup(func() { os.Unsetenv(branchNameEnvVar) })
t.Cleanup(func() { os.Unsetenv(EnvXataBranch) })
}

func Test_getRegion(t *testing.T) {
Expand All @@ -56,7 +56,7 @@ func Test_getRegion(t *testing.T) {
})

setRegion := "eu-west-3"
err := os.Setenv(regionEnvVar, setRegion)
err := os.Setenv(EnvXataRegion, 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(regionEnvVar) })
t.Cleanup(func() { os.Unsetenv(EnvXataRegion) })
}

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(xataWsIDEnvVar, setWsId)
err := os.Setenv(EnvXataWorkspaceID, setWsId)
if err != nil {
t.Fatal(err)
}
Expand All @@ -200,12 +200,12 @@ func Test_loadDatabaseConfig_with_envvars(t *testing.T) {
})

setBranch := "branch123"
err2 := os.Setenv(branchNameEnvVar, setBranch)
err2 := os.Setenv(EnvXataBranch, setBranch)
if err2 != nil {
t.Fatal(err2)
}
setRegion := "ap-southeast-16"
err3 := os.Setenv(regionEnvVar, setRegion)
err3 := os.Setenv(EnvXataRegion, setRegion)
if err3 != nil {
t.Fatal(err3)
}
Expand All @@ -229,8 +229,8 @@ func Test_loadDatabaseConfig_with_envvars(t *testing.T) {
})

t.Cleanup(func() {
os.Unsetenv(xataWsIDEnvVar)
os.Unsetenv(branchNameEnvVar)
os.Unsetenv(regionEnvVar)
os.Unsetenv(EnvXataWorkspaceID)
os.Unsetenv(EnvXataBranch)
os.Unsetenv(EnvXataRegion)
})
}

0 comments on commit cc238a9

Please sign in to comment.