set up config structure in etc
This commit is contained in:
parent
82723c8c80
commit
035090bb04
6 changed files with 41 additions and 11 deletions
|
@ -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}
|
||||
}
|
||||
|
||||
|
|
|
@ -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 (
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue