From 6acf56cde9fde972cf7c78f15d00ade262752c19 Mon Sep 17 00:00:00 2001 From: kim Date: Tue, 6 May 2025 15:51:45 +0000 Subject: [feature] support nested configuration files, and setting ALL configuration variables by CLI and env (#4109) This updates our configuration code generator to now also include map marshal and unmarshalers. So we now have much more control over how things get read from pflags, and stored / read from viper configuration. This allows us to set ALL configuration variables by CLI and environment now, AND support nested configuration files. e.g. ```yaml advanced: scraper-deterrence = true http-client: allow-ips = ["127.0.0.1"] ``` is the same as ```yaml advanced-scraper-deterrence = true http-client-allow-ips = ["127.0.0.1"] ``` This also starts cleaning up of our jumbled Configuration{} type by moving the advanced configuration options into their own nested structs, also as a way to show what it's capable of. It's worth noting however that nesting only works if the Go types are nested too (as this is how we hint to our code generator to generate the necessary flattening code :p). closes #3195 Reviewed-on: https://codeberg.org/superseriousbusiness/gotosocial/pulls/4109 Co-authored-by: kim Co-committed-by: kim --- internal/config/config_test.go | 52 ++++++++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 22 deletions(-) (limited to 'internal/config/config_test.go') diff --git a/internal/config/config_test.go b/internal/config/config_test.go index 006d4a449..48a138aa8 100644 --- a/internal/config/config_test.go +++ b/internal/config/config_test.go @@ -24,19 +24,18 @@ import ( "testing" "code.superseriousbusiness.org/gotosocial/internal/config" + "codeberg.org/gruf/go-kv" "github.com/spf13/cobra" "github.com/spf13/viper" "github.com/stretchr/testify/assert" "gopkg.in/yaml.v3" ) -func expectedKV(kvpairs ...string) map[string]interface{} { - ret := make(map[string]interface{}, len(kvpairs)/2) - - for i := 0; i < len(kvpairs)-1; i += 2 { - ret[kvpairs[i]] = kvpairs[i+1] +func expectedKV(kvs ...kv.Field) map[string]interface{} { + ret := make(map[string]interface{}, len(kvs)) + for _, kv := range kvs { + ret[kv.K] = kv.V } - return ret } @@ -61,7 +60,7 @@ func TestCLIParsing(t *testing.T) { expected map[string]interface{} } - defaults, _ := config.Defaults.MarshalMap() + defaults := config.Defaults.MarshalMap() testcases := map[string]testcase{ "Make sure defaults are set correctly": { @@ -73,7 +72,7 @@ func TestCLIParsing(t *testing.T) { "--db-address", "some.db.address", }, expected: expectedKV( - "db-address", "some.db.address", + kv.Field{"db-address", "some.db.address"}, ), }, @@ -82,7 +81,7 @@ func TestCLIParsing(t *testing.T) { "GTS_DB_ADDRESS=some.db.address", }, expected: expectedKV( - "db-address", "some.db.address", + kv.Field{"db-address", "some.db.address"}, ), }, @@ -94,7 +93,7 @@ func TestCLIParsing(t *testing.T) { "GTS_DB_ADDRESS=some.other.db.address", }, expected: expectedKV( - "db-address", "some.db.address", + kv.Field{"db-address", "some.db.address"}, ), }, @@ -119,8 +118,8 @@ func TestCLIParsing(t *testing.T) { }, // only checking our overridden one and one non-default from the config file here instead of including all of test.yaml expected: expectedKV( - "account-domain", "my.test.domain", - "host", "gts.example.org", + kv.Field{"account-domain", "my.test.domain"}, + kv.Field{"host", "gts.example.org"}, ), }, @@ -133,8 +132,8 @@ func TestCLIParsing(t *testing.T) { }, // only checking our overridden one and one non-default from the config file here instead of including all of test.yaml expected: expectedKV( - "account-domain", "my.test.domain", - "host", "gts.example.org", + kv.Field{"account-domain", "my.test.domain"}, + kv.Field{"host", "gts.example.org"}, ), }, @@ -148,8 +147,8 @@ func TestCLIParsing(t *testing.T) { }, // only checking our overridden one and one non-default from the config file here instead of including all of test.yaml expected: expectedKV( - "account-domain", "my.test.domain", - "host", "gts.example.org", + kv.Field{"account-domain", "my.test.domain"}, + kv.Field{"host", "gts.example.org"}, ), }, @@ -165,9 +164,19 @@ func TestCLIParsing(t *testing.T) { "--config-path", "testdata/test2.yaml", }, expected: expectedKV( - "log-level", "trace", - "account-domain", "peepee.poopoo", - "application-name", "gotosocial", + kv.Field{"log-level", "trace"}, + kv.Field{"account-domain", "peepee.poopoo"}, + kv.Field{"application-name", "gotosocial"}, + ), + }, + + "Loading nested config file. This should also work the same": { + cli: []string{ + "--config-path", "testdata/test3.yaml", + }, + expected: expectedKV( + kv.Field{"advanced-scraper-deterrence", true}, + kv.Field{"advanced-rate-limit-requests", 5000}, ), }, } @@ -185,8 +194,7 @@ func TestCLIParsing(t *testing.T) { state := config.NewState() cmd := cobra.Command{} - state.AddGlobalFlags(&cmd) - state.AddServerFlags(&cmd) + config.RegisterGlobalFlags(&cmd) if data.cli != nil { cmd.ParseFlags(data.cli) @@ -194,7 +202,7 @@ func TestCLIParsing(t *testing.T) { state.BindFlags(&cmd) - state.Reload() + state.LoadConfigFile() state.Viper(func(v *viper.Viper) { for k, ev := range data.expected { -- cgit v1.2.3