-
Notifications
You must be signed in to change notification settings - Fork 0
Add configuration handling with viper #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
silv-io
merged 9 commits into
main
from
flc-323-implement-license-and-credentials-configuration
Feb 4, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d4e60a5
Add configuration handling with viper
silv-io 45ad350
Add support for env vars
silv-io 4f207b6
Adapt config values
silv-io 1d7bc33
Move to toml
silv-io dd4a31b
Move to toml, create file on startup
silv-io 1fcd4bd
Fix indentation
silv-io 1eebdec
Add configuration integration test
silv-io 9791027
Flatten config writing if file is missing
silv-io 98e8d3e
Make health path hard-coded
silv-io File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| package config | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
|
|
||
| "github.com/spf13/viper" | ||
| ) | ||
|
|
||
| type EmulatorType string | ||
|
|
||
| const ( | ||
| EmulatorAWS EmulatorType = "aws" | ||
| EmulatorSnowflake EmulatorType = "snowflake" | ||
| EmulatorAzure EmulatorType = "azure" | ||
| ) | ||
|
|
||
| var emulatorImages = map[EmulatorType]string{ | ||
| EmulatorAWS: "localstack/localstack-pro", | ||
| } | ||
|
|
||
| var emulatorHealthPaths = map[EmulatorType]string{ | ||
| EmulatorAWS: "/_localstack/health", | ||
| } | ||
|
|
||
| type Config struct { | ||
| Containers []ContainerConfig `mapstructure:"containers"` | ||
| } | ||
|
|
||
| type ContainerConfig struct { | ||
| Type EmulatorType `mapstructure:"type"` | ||
| Tag string `mapstructure:"tag"` | ||
| Port string `mapstructure:"port"` | ||
| Env []string `mapstructure:"env"` | ||
| } | ||
|
|
||
| func (c *ContainerConfig) Image() (string, error) { | ||
| baseImage, ok := emulatorImages[c.Type] | ||
| if !ok { | ||
| return "", fmt.Errorf("%s emulator not supported yet by lstk", c.Type) | ||
| } | ||
| tag := c.Tag | ||
| if tag == "" { | ||
| tag = "latest" | ||
| } | ||
| return fmt.Sprintf("%s:%s", baseImage, tag), nil | ||
| } | ||
|
|
||
| // Name returns the container name: "localstack-{type}" or "localstack-{type}-{tag}" if tag != latest | ||
| func (c *ContainerConfig) Name() string { | ||
| tag := c.Tag | ||
| if tag == "" || tag == "latest" { | ||
| return fmt.Sprintf("localstack-%s", c.Type) | ||
| } | ||
| return fmt.Sprintf("localstack-%s-%s", c.Type, tag) | ||
| } | ||
|
|
||
| func (c *ContainerConfig) HealthPath() (string, error) { | ||
| path, ok := emulatorHealthPaths[c.Type] | ||
| if !ok { | ||
| return "", fmt.Errorf("%s emulator not supported yet by lstk", c.Type) | ||
| } | ||
| return path, nil | ||
| } | ||
|
|
||
| func configDir() (string, error) { | ||
| configHome, err := os.UserConfigDir() | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed to get user config directory: %w", err) | ||
| } | ||
| return filepath.Join(configHome, "lstk"), nil | ||
| } | ||
|
|
||
| func ConfigDir() (string, error) { | ||
| return configDir() | ||
| } | ||
|
|
||
| func Init() error { | ||
| dir, err := configDir() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if err := os.MkdirAll(dir, 0755); err != nil { | ||
| return fmt.Errorf("failed to create config directory: %w", err) | ||
| } | ||
|
|
||
| viper.SetConfigName("config") | ||
| viper.SetConfigType("toml") | ||
| viper.AddConfigPath(dir) | ||
|
|
||
| viper.SetDefault("containers", []map[string]any{ | ||
| { | ||
| "type": "aws", | ||
| "tag": "latest", | ||
| "port": "4566", | ||
| }, | ||
| }) | ||
|
|
||
| if err := viper.ReadInConfig(); err != nil { | ||
| if _, ok := err.(viper.ConfigFileNotFoundError); ok { | ||
| if err := viper.SafeWriteConfig(); err != nil { | ||
| return fmt.Errorf("failed to write config file: %w", err) | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| return fmt.Errorf("failed to read config file: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func Get() (*Config, error) { | ||
| var cfg Config | ||
| if err := viper.Unmarshal(&cfg); err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal config: %w", err) | ||
| } | ||
| return &cfg, nil | ||
| } | ||
|
|
||
| func ConfigFilePath() (string, error) { | ||
| dir, err := configDir() | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
| return filepath.Join(dir, "config.toml"), nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package integration_test | ||
|
|
||
| import ( | ||
| "os" | ||
| "os/exec" | ||
| "path/filepath" | ||
| "runtime" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestConfigFileCreatedOnStartup(t *testing.T) { | ||
| tmpHome := t.TempDir() | ||
|
|
||
| var configDir string | ||
| var env []string | ||
|
|
||
| switch runtime.GOOS { | ||
| case "darwin": | ||
| configDir = filepath.Join(tmpHome, "Library", "Application Support", "lstk") | ||
| env = append(os.Environ(), "HOME="+tmpHome) | ||
| case "linux": | ||
| configDir = filepath.Join(tmpHome, ".config", "lstk") | ||
| env = append(os.Environ(), "HOME="+tmpHome, "XDG_CONFIG_HOME=") | ||
| case "windows": | ||
| configDir = filepath.Join(tmpHome, "AppData", "Roaming", "lstk") | ||
| env = append(os.Environ(), "APPDATA="+filepath.Join(tmpHome, "AppData", "Roaming")) | ||
| default: | ||
| t.Skipf("unsupported OS: %s", runtime.GOOS) | ||
| } | ||
|
|
||
| configFile := filepath.Join(configDir, "config.toml") | ||
|
|
||
| cmd := exec.Command("../../bin/lstk", "start") | ||
| cmd.Env = env | ||
| cmd.Start() | ||
| defer cmd.Process.Kill() | ||
|
|
||
| // Poll for config file creation - check every 50ms, timeout after 2s | ||
| require.Eventually(t, func() bool { | ||
| _, err := os.Stat(configFile) | ||
| return err == nil | ||
| }, 2*time.Second, 20*time.Millisecond, "config.toml should be created") | ||
|
|
||
| assert.DirExists(t, configDir, "config directory should be created at OS-specific location") | ||
|
|
||
| content, err := os.ReadFile(configFile) | ||
| require.NoError(t, err) | ||
|
|
||
| configStr := string(content) | ||
| assert.Contains(t, configStr, `type = 'aws'`) | ||
| assert.Contains(t, configStr, `tag = 'latest'`) | ||
| assert.Contains(t, configStr, `port = '4566'`) | ||
| } | ||
silv-io marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.