set up config structure in etc

This commit is contained in:
Helmut Merz 2023-06-01 15:54:27 +02:00
parent 82723c8c80
commit 035090bb04
6 changed files with 41 additions and 11 deletions

View file

@ -1,9 +1,16 @@
package config package config
type Cfg struct {
*BaseConfig
ConfigFormat string
}
// definitions
type Config interface { type Config interface {
Name() string Name() string
Children() []Config Children() []Config
AddChild(Config) Add(Config)
} }
type BaseConfig struct { type BaseConfig struct {
@ -19,11 +26,11 @@ func (cfg *BaseConfig) Children() []Config {
return cfg.children return cfg.children
} }
func (cfg *BaseConfig) AddChild(child Config) { func (cfg *BaseConfig) Add(child Config) {
cfg.children = append(cfg.children, child) cfg.children = append(cfg.children, child)
} }
func MakeConfig(name string) Config { func MakeBase(name string) *BaseConfig {
return &BaseConfig{name: name} return &BaseConfig{name: name}
} }

View file

@ -1,9 +1,18 @@
package etc 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 { 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 ( const (

View file

@ -10,8 +10,8 @@ import (
) )
func main() { func main() {
fmt.Println(etc.Config().Name())
fmt.Println(etc.Override()["name"]) fmt.Println(etc.Override()["name"])
cfg := config.Setup(etc.Config()) cfg := config.Setup(etc.Config())
fmt.Println(cfg.Name())
scopes.RunApp(cfg) scopes.RunApp(cfg)
} }

View file

@ -2,6 +2,11 @@ package scopes
import "git.sr.ht/~cco/go-scopes/config" import "git.sr.ht/~cco/go-scopes/config"
type Cfg struct {
*config.BaseConfig
AppType string
}
func RunApp(cfg config.Config) { func RunApp(cfg config.Config) {
_ = cfg _ = cfg
} }

View file

@ -1,9 +1,18 @@
package etc 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 { 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 // collect here the names of fields that may be overridden via
@ -12,8 +21,8 @@ const (
NAME = "name" NAME = "name"
) )
// in a production scenario this should be put in separate file `settings.go` // in a production scenario this should be put in separate
// that should not be stored in code repository. // file `settings.go` that should not be stored in code repository.
func Override() map[string]string { func Override() map[string]string {
return map[string]string{ return map[string]string{
NAME: "overridden", NAME: "overridden",

View file

@ -15,8 +15,8 @@ func TestConfig(tb *tbase.T) {
} }
func ConfigTest(t *testing.T) { func ConfigTest(t *testing.T) {
t.AssertEqual(etc.Config().Name(), "dummy")
t.AssertEqual(etc.Override()[etc.NAME], "overridden") t.AssertEqual(etc.Override()[etc.NAME], "overridden")
cfg := config.Setup(etc.Config()) cfg := config.Setup(etc.Config())
t.AssertEqual(cfg.Name(), "dummy")
scopes.RunApp(cfg) scopes.RunApp(cfg)
} }