feat: optionally pull config from a custom file (envvar), default to data path. (#557)
Kendall Tauser
created
* feat: optionally pull config from a custom file (envvar), default to data path.
* docs: add docs on SOFT_SERVE_CONFIG_LOCATION
* feat: add tests for SOFT_SERVE_CONFIG_LOCATION
@@ -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
@@ -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")
+}