Skip to content

Commit dc398d1

Browse files
add env vars struct
1 parent fd41452 commit dc398d1

8 files changed

Lines changed: 52 additions & 18 deletions

File tree

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/localstack/lstk/internal/api"
99
"github.com/localstack/lstk/internal/config"
1010
"github.com/localstack/lstk/internal/container"
11+
"github.com/localstack/lstk/internal/env"
1112
"github.com/localstack/lstk/internal/output"
1213
"github.com/localstack/lstk/internal/runtime"
1314
"github.com/spf13/cobra"
@@ -18,6 +19,7 @@ var rootCmd = &cobra.Command{
1819
Short: "LocalStack CLI",
1920
Long: "lstk is the command-line interface for LocalStack.",
2021
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
22+
env.Init()
2123
return config.Init()
2224
},
2325
Run: func(cmd *cobra.Command, args []string) {

env.example

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
# Authentication token (optional - will trigger device flow login if not set)
12
export LOCALSTACK_AUTH_TOKEN=ls-...
23

3-
# Force file-based keyring backend (instead of system keychain)
4-
# export KEYRING=file
5-
#
4+
# API endpoint (defaults to https://api.localstack.cloud)
65
export LOCALSTACK_API_ENDPOINT=https://api.staging.aws.localstack.cloud
6+
7+
# Web app URL (defaults to https://app.localstack.cloud)
78
export LOCALSTACK_WEB_APP_URL=https://app.staging.aws.localstack.cloud
9+
10+
# Force file-based keyring backend instead of system keychain (optional)
11+
# export KEYRING=file

internal/api/client.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import (
77
"fmt"
88
"log"
99
"net/http"
10-
"os"
1110
"time"
11+
12+
"github.com/localstack/lstk/internal/env"
1213
)
1314

1415
type PlatformAPI interface {
@@ -65,12 +66,8 @@ type PlatformClient struct {
6566
}
6667

6768
func NewPlatformClient() *PlatformClient {
68-
baseURL := os.Getenv("LOCALSTACK_API_ENDPOINT")
69-
if baseURL == "" {
70-
baseURL = "https://api.localstack.cloud"
71-
}
7269
return &PlatformClient{
73-
baseURL: baseURL,
70+
baseURL: env.Vars.APIEndpoint,
7471
httpClient: &http.Client{Timeout: 30 * time.Second},
7572
}
7673
}

internal/auth/auth.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"os"
87

98
"github.com/99designs/keyring"
109
"github.com/localstack/lstk/internal/api"
10+
"github.com/localstack/lstk/internal/env"
1111
"github.com/localstack/lstk/internal/output"
1212
)
1313

@@ -35,7 +35,7 @@ func (a *Auth) GetToken(ctx context.Context) (string, error) {
3535
return token, nil
3636
}
3737

38-
if token := os.Getenv("LOCALSTACK_AUTH_TOKEN"); token != "" {
38+
if token := env.Vars.AuthToken; token != "" {
3939
return token, nil
4040
}
4141

internal/auth/login.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ import (
1111
"os"
1212

1313
"github.com/localstack/lstk/internal/api"
14+
"github.com/localstack/lstk/internal/env"
1415
"github.com/localstack/lstk/internal/output"
1516
"github.com/pkg/browser"
1617
)
1718

18-
const webAppURL = "https://app.localstack.cloud"
1919
const loginCallbackURL = "127.0.0.1:45678"
2020

2121
type LoginProvider interface {
@@ -121,11 +121,7 @@ func (b *browserLogin) Login(ctx context.Context) (string, error) {
121121
}
122122

123123
func getWebAppURL() string {
124-
// allows overriding the URL for testing
125-
if url := os.Getenv("LOCALSTACK_WEB_APP_URL"); url != "" {
126-
return url
127-
}
128-
return webAppURL
124+
return env.Vars.WebAppURL
129125
}
130126

131127
func (b *browserLogin) completeDeviceFlow(ctx context.Context, authReq *api.AuthRequest) (string, error) {

internal/auth/token_storage.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/99designs/keyring"
1212
"github.com/localstack/lstk/internal/config"
13+
"github.com/localstack/lstk/internal/env"
1314
)
1415

1516
const (
@@ -45,7 +46,7 @@ func newAuthTokenStorage() (*authTokenStorage, error) {
4546
}
4647

4748
// Force file backend if KEYRING env var is set to "file"
48-
if os.Getenv("KEYRING") == "file" {
49+
if env.Vars.Keyring == "file" {
4950
keyringConfig.AllowedBackends = []keyring.BackendType{keyring.FileBackend}
5051
}
5152

internal/env/env.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package env
2+
3+
import (
4+
"strings"
5+
6+
"github.com/spf13/viper"
7+
)
8+
9+
type Env struct {
10+
AuthToken string
11+
APIEndpoint string
12+
WebAppURL string
13+
Keyring string
14+
}
15+
16+
var Vars *Env
17+
18+
// Init initializes environment variable configuration
19+
func Init() {
20+
viper.SetEnvPrefix("LOCALSTACK")
21+
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
22+
viper.AutomaticEnv()
23+
24+
viper.SetDefault("api_endpoint", "https://api.localstack.cloud")
25+
viper.SetDefault("web_app_url", "https://app.localstack.cloud")
26+
27+
Vars = &Env{
28+
AuthToken: viper.GetString("auth_token"),
29+
APIEndpoint: viper.GetString("api_endpoint"),
30+
WebAppURL: viper.GetString("web_app_url"),
31+
Keyring: viper.GetString("keyring"),
32+
}
33+
}

test/integration/main_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func requireDocker(t *testing.T) {
8181
t.Skip("Docker is not available")
8282
}
8383
// Skip Docker tests on Windows (GitHub Actions doesn't support Linux containers)
84+
// Note: CI env var is not in config.GetEnv() as it's a standard CI environment variable
8485
if runtime.GOOS == "windows" && os.Getenv("CI") != "" {
8586
t.Skip("Docker tests not supported on Windows CI (nested virtualization not available)")
8687
}

0 commit comments

Comments
 (0)