Skip to content
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

feat: optionally pull config from a custom file (envvar), default to data path. #557

Merged
merged 3 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ Make sure `git` is installed, then run `soft serve`. That’s it.
This will create a `data` directory that will store all the repos, ssh keys,
and database.

By default, program configuration is stored within the `data` directory. But,
this can be overridden by setting a custom path to a config file with `SOFT_SERVE_CONFIG_LOCATION`
that is pre-created. If a config file pointed to by `SOFT_SERVE_CONFIG_LOCATION`,
the default location within the `data` dir is used for generating a default config.

To change the default data path use `SOFT_SERVE_DATA_PATH` environment variable.

```sh
Expand Down
8 changes: 7 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ func DefaultDataPath() string {

// ConfigPath returns the path to the config file.
func (c *Config) ConfigPath() string { // nolint:revive
// If we have a custom config location set, then use that.
if path := os.Getenv("SOFT_SERVE_CONFIG_LOCATION"); exist(path) {
return path
}

// Otherwise, look in the data path.
return filepath.Join(c.DataPath, "config.yaml")
}

Expand All @@ -301,7 +307,7 @@ func exist(path string) bool {

// Exist returns true if the config file exists.
func (c *Config) Exist() bool {
return exist(filepath.Join(c.DataPath, "config.yaml"))
return exist(c.ConfigPath())
}

// DefaultConfig returns the default Config. All the path values are relative
Expand Down
23 changes: 23 additions & 0 deletions pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,26 @@ func TestValidateInitAdminKeys(t *testing.T) {
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINMwLvyV3ouVrTysUYGoJdl5Vgn5BACKov+n9PlzfPwH",
})
}

func TestCustomConfigLocation(t *testing.T) {
is := is.New(t)
td := t.TempDir()
t.Cleanup(func() {
is.NoErr(os.Unsetenv("SOFT_SERVE_CONFIG_LOCATION"))
})

// Test that we get data from the custom file location, and not from the data dir.
is.NoErr(os.Setenv("SOFT_SERVE_CONFIG_LOCATION", "testdata/config.yaml"))
is.NoErr(os.Setenv("SOFT_SERVE_DATA_PATH", td))
cfg := DefaultConfig()
is.NoErr(cfg.Parse())
is.Equal(cfg.Name, "Test server name")
// If we unset the custom location, then use the default location.
is.NoErr(os.Unsetenv("SOFT_SERVE_CONFIG_LOCATION"))
cfg = DefaultConfig()
is.Equal(cfg.Name, "Soft Serve")
// Test that if the custom config location doesn't exist, default to datapath config.
is.NoErr(os.Setenv("SOFT_SERVE_CONFIG_LOCATION", "testdata/config_nonexistent.yaml"))
cfg = DefaultConfig()
is.Equal(cfg.Name, "Soft Serve")
}
3 changes: 3 additions & 0 deletions pkg/config/testdata/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Soft Serve Server configurations

name: "Test server name"
Loading