diff --git a/config/config.go b/config/config.go index 10a8fcb..c0915ea 100644 --- a/config/config.go +++ b/config/config.go @@ -1,9 +1,16 @@ package config +type Cfg struct { + *BaseConfig + ConfigFormat string +} + +// definitions + type Config interface { Name() string Children() []Config - AddChild(Config) + Add(Config) } type BaseConfig struct { @@ -19,11 +26,11 @@ func (cfg *BaseConfig) Children() []Config { return cfg.children } -func (cfg *BaseConfig) AddChild(child Config) { +func (cfg *BaseConfig) Add(child Config) { cfg.children = append(cfg.children, child) } -func MakeConfig(name string) Config { +func MakeBase(name string) *BaseConfig { return &BaseConfig{name: name} } diff --git a/examples/demo/etc/etc.go b/examples/demo/etc/etc.go index aa8ff3c..06caba4 100644 --- a/examples/demo/etc/etc.go +++ b/examples/demo/etc/etc.go @@ -1,9 +1,18 @@ package etc -import "git.sr.ht/~cco/go-scopes/config" +import ( + "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/config" +) func Config() config.Config { - return config.MakeConfig("dummy") + b := config.MakeBase + cfg := scopes.Cfg{ + BaseConfig: b("dummy"), + AppType: "standard", + } + cfg.Add(config.Cfg{b("config"), "etc"}) + return &cfg } const ( diff --git a/examples/demo/main.go b/examples/demo/main.go index e57bef9..300267e 100644 --- a/examples/demo/main.go +++ b/examples/demo/main.go @@ -10,8 +10,8 @@ import ( ) func main() { - fmt.Println(etc.Config().Name()) fmt.Println(etc.Override()["name"]) cfg := config.Setup(etc.Config()) + fmt.Println(cfg.Name()) scopes.RunApp(cfg) } diff --git a/scopes.go b/scopes.go index 1934eff..43f0b94 100644 --- a/scopes.go +++ b/scopes.go @@ -2,6 +2,11 @@ package scopes import "git.sr.ht/~cco/go-scopes/config" +type Cfg struct { + *config.BaseConfig + AppType string +} + func RunApp(cfg config.Config) { _ = cfg } diff --git a/tests/etc/etc.go b/tests/etc/etc.go index a1b0494..0f4795b 100644 --- a/tests/etc/etc.go +++ b/tests/etc/etc.go @@ -1,9 +1,18 @@ package etc -import "git.sr.ht/~cco/go-scopes/config" +import ( + "git.sr.ht/~cco/go-scopes" + "git.sr.ht/~cco/go-scopes/config" +) func Config() config.Config { - return config.MakeConfig("dummy") + b := config.MakeBase + cfg := scopes.Cfg{ + BaseConfig: b("dummy"), + AppType: "standard", + } + cfg.Add(config.Cfg{b("config"), "etc"}) + return &cfg } // collect here the names of fields that may be overridden via @@ -12,8 +21,8 @@ const ( NAME = "name" ) -// in a production scenario this should be put in separate file `settings.go` -// that should not be stored in code repository. +// in a production scenario this should be put in separate +// file `settings.go` that should not be stored in code repository. func Override() map[string]string { return map[string]string{ NAME: "overridden", diff --git a/tests/scopes_test.go b/tests/scopes_test.go index 882cfad..0152e8e 100644 --- a/tests/scopes_test.go +++ b/tests/scopes_test.go @@ -15,8 +15,8 @@ func TestConfig(tb *tbase.T) { } func ConfigTest(t *testing.T) { - t.AssertEqual(etc.Config().Name(), "dummy") t.AssertEqual(etc.Override()[etc.NAME], "overridden") cfg := config.Setup(etc.Config()) + t.AssertEqual(cfg.Name(), "dummy") scopes.RunApp(cfg) }