Skip to content

Commit

Permalink
allow ASTRO_DOMAIN env var to override context (#1451)
Browse files Browse the repository at this point in the history
  • Loading branch information
melugoyal committed Nov 17, 2023
1 parent 8b07c6b commit 16d72df
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
12 changes: 6 additions & 6 deletions config/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"errors"
"fmt"
"os"
"strings"
"time"
)
Expand Down Expand Up @@ -39,14 +40,13 @@ type Context struct {
// GetCurrentContext looks up current context and gets corresponding Context struct
func GetCurrentContext() (Context, error) {
c := Context{}

domain := CFG.Context.GetHomeString()
if domain == "" {
return Context{}, ErrGetHomeString
var domain string
if domain = os.Getenv("ASTRO_DOMAIN"); domain == "" {
if domain = CFG.Context.GetHomeString(); domain == "" {
return Context{}, ErrGetHomeString
}
}

c.Domain = domain

return c.GetContext()
}

Expand Down
33 changes: 33 additions & 0 deletions config/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,39 @@ contexts:
assert.Equal(t, "ck05r3bor07h40d02y2hw4n4v", ctx.Workspace)
}

func TestGetCurrentContext_WithDomainOverride(t *testing.T) {
fs := afero.NewMemMapFs()
configRaw := []byte(`cloud:
api:
port: "443"
protocol: https
ws_protocol: wss
local:
enabled: true
host: http://example.com:8871/v1
context: example_com
contexts:
example_com:
domain: example.com
token: token
last_used_workspace: ck05r3bor07h40d02y2hw4n4v
workspace: ck05r3bor07h40d02y2hw4n4v
stage_example_com:
domain: stage.example.com
token: token
last_used_workspace: ck05r3bor07h40d02y2hw4n4w
workspace: ck05r3bor07h40d02y2hw4n4w
`)
err = afero.WriteFile(fs, HomeConfigFile, configRaw, 0o777)
InitConfig(fs)
t.Setenv("ASTRO_DOMAIN", "stage.example.com")
ctx, err := GetCurrentContext()
assert.NoError(t, err)
assert.Equal(t, "stage.example.com", ctx.Domain)
assert.Equal(t, "token", ctx.Token)
assert.Equal(t, "ck05r3bor07h40d02y2hw4n4w", ctx.Workspace)
}

func TestDeleteContext(t *testing.T) {
fs := afero.NewMemMapFs()
configRaw := []byte(`
Expand Down

0 comments on commit 16d72df

Please sign in to comment.