config settings, overridable by settings.go or env (SCOPES_...) settings
This commit is contained in:
parent
968bdc925d
commit
f896adfb8c
7 changed files with 40 additions and 7 deletions
|
@ -1,6 +1,11 @@
|
||||||
package config
|
package config
|
||||||
|
|
||||||
import "git.sr.ht/~cco/go-scopes/common"
|
import (
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"git.sr.ht/~cco/go-scopes/common"
|
||||||
|
)
|
||||||
|
|
||||||
type Cfg struct {
|
type Cfg struct {
|
||||||
*Base
|
*Base
|
||||||
|
@ -51,3 +56,17 @@ func MakeBase(name string, starter common.StartFct) *Base {
|
||||||
func Setup(cfg Config) Config {
|
func Setup(cfg Config) Config {
|
||||||
return cfg
|
return cfg
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// overridable settings
|
||||||
|
|
||||||
|
type Settings map[string]string
|
||||||
|
|
||||||
|
func (s Settings) Get(key, def string) string {
|
||||||
|
if v, ok := os.LookupEnv("SCOPES_" + strings.ToUpper(key)); ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
if v, ok := s[key]; ok {
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
return def
|
||||||
|
}
|
||||||
|
|
|
@ -6,9 +6,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Config() config.Config {
|
func Config() config.Config {
|
||||||
|
ovr := Overrides().Get
|
||||||
b := config.MakeBase
|
b := config.MakeBase
|
||||||
cfg := scopes.Cfg{
|
cfg := scopes.Cfg{
|
||||||
Base: b("dummy", scopes.Start),
|
Base: b("dummy", scopes.Start),
|
||||||
|
Home: ovr(HOME, "."),
|
||||||
AppType: "standard",
|
AppType: "standard",
|
||||||
}
|
}
|
||||||
cfg.Add(config.Cfg{b("config", config.Start), "etc"})
|
cfg.Add(config.Cfg{b("config", config.Start), "etc"})
|
||||||
|
@ -17,4 +19,5 @@ func Config() config.Config {
|
||||||
|
|
||||||
const (
|
const (
|
||||||
NAME = "name"
|
NAME = "name"
|
||||||
|
HOME = "home"
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
package etc
|
package etc
|
||||||
|
|
||||||
func Override() map[string]string {
|
import "git.sr.ht/~cco/go-scopes/config"
|
||||||
return map[string]string{
|
|
||||||
|
func Overrides() config.Settings {
|
||||||
|
return config.Settings{
|
||||||
NAME: "overridden",
|
NAME: "overridden",
|
||||||
|
HOME: "/home/scopes/app",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,8 +10,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println(etc.Override()["name"])
|
|
||||||
cfg := config.Setup(etc.Config())
|
cfg := config.Setup(etc.Config())
|
||||||
fmt.Println(cfg.Name())
|
fmt.Println(cfg.Name())
|
||||||
|
appCfg := cfg.(*scopes.Cfg)
|
||||||
|
fmt.Println(appCfg.Home)
|
||||||
scopes.RunApp(cfg)
|
scopes.RunApp(cfg)
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
type Cfg struct {
|
type Cfg struct {
|
||||||
*config.Base
|
*config.Base
|
||||||
AppType string
|
AppType string
|
||||||
|
Home string
|
||||||
}
|
}
|
||||||
|
|
||||||
func Start(ctx common.Context) {
|
func Start(ctx common.Context) {
|
||||||
|
|
|
@ -6,9 +6,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Config() config.Config {
|
func Config() config.Config {
|
||||||
|
ovr := Overrides().Get
|
||||||
b := config.MakeBase
|
b := config.MakeBase
|
||||||
cfg := scopes.Cfg{
|
cfg := scopes.Cfg{
|
||||||
Base: b("dummy", scopes.Start),
|
Base: b("dummy", scopes.Start),
|
||||||
|
Home: ovr(HOME, "."),
|
||||||
AppType: "standard",
|
AppType: "standard",
|
||||||
}
|
}
|
||||||
cfg.Add(config.Cfg{b("config", config.Start), "etc"})
|
cfg.Add(config.Cfg{b("config", config.Start), "etc"})
|
||||||
|
@ -19,12 +21,14 @@ func Config() config.Config {
|
||||||
// explicit Override() or SCOPES_* environment settings.
|
// explicit Override() or SCOPES_* environment settings.
|
||||||
const (
|
const (
|
||||||
NAME = "name"
|
NAME = "name"
|
||||||
|
HOME = "home"
|
||||||
)
|
)
|
||||||
|
|
||||||
// in a production scenario this should be put in separate
|
// in a production scenario this should be put in separate
|
||||||
// file `settings.go` 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 Overrides() config.Settings {
|
||||||
return map[string]string{
|
return config.Settings{
|
||||||
NAME: "overridden",
|
NAME: "overridden",
|
||||||
|
HOME: "/home/scopes",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,8 +15,10 @@ func TestConfig(tb *tbase.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func ConfigTest(t *testing.T) {
|
func ConfigTest(t *testing.T) {
|
||||||
t.AssertEqual(etc.Override()[etc.NAME], "overridden")
|
t.AssertEqual(etc.Overrides()[etc.NAME], "overridden")
|
||||||
cfg := config.Setup(etc.Config())
|
cfg := config.Setup(etc.Config())
|
||||||
t.AssertEqual(cfg.Name(), "dummy")
|
t.AssertEqual(cfg.Name(), "dummy")
|
||||||
|
appCfg := cfg.(*scopes.Cfg)
|
||||||
|
t.AssertEqual(appCfg.Home, "/home/scopes")
|
||||||
scopes.RunApp(cfg)
|
scopes.RunApp(cfg)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue