rename config base stuff, + some minor refactorings

This commit is contained in:
Helmut Merz 2023-06-24 10:30:19 +02:00
parent 2141e41241
commit 72abf7cc0f
12 changed files with 56 additions and 54 deletions

View file

@ -14,7 +14,7 @@ import (
) )
type Cfg struct { type Cfg struct {
*config.Base *config.BaseCfg
AppType string AppType string
Home string Home string
Logging *logging.Config Logging *logging.Config

View file

@ -9,7 +9,7 @@ import (
) )
type Cfg struct { type Cfg struct {
*Base *BaseCfg
ConfigFormat string ConfigFormat string
} }
@ -18,7 +18,9 @@ func Start(ctx lib.Context) {
// definitions // definitions
type Base struct { type BaseCfg = base
type base struct {
name string name string
starter lib.StartProc starter lib.StartProc
step lib.StepProc step lib.StepProc
@ -29,60 +31,60 @@ type Base struct {
// lib.Config implementation // lib.Config implementation
func (cfg *Base) Name() string { func (cfg *base) Name() string {
return cfg.name return cfg.name
} }
func (cfg *Base) Starter() lib.StartProc { func (cfg *base) Starter() lib.StartProc {
return cfg.starter return cfg.starter
} }
func (cfg *Base) Step() lib.StepProc { func (cfg *base) Step() lib.StepProc {
if cfg.step == nil { if cfg.step == nil {
return core.Step return core.Step
} }
return cfg.step return cfg.step
} }
func (cfg *Base) MessageHandler() lib.MessageHandler { func (cfg *base) MessageHandler() lib.MessageHandler {
if cfg.msgHandler == nil { if cfg.msgHandler == nil {
return core.HandleMessage return core.HandleMessage
} }
return cfg.msgHandler return cfg.msgHandler
} }
func (cfg *Base) Actions() []lib.ActionConfig { func (cfg *base) Actions() []lib.ActionConfig {
return cfg.actions return cfg.actions
} }
func (cfg *Base) Children() []lib.Config { func (cfg *base) Children() []lib.Config {
return cfg.children return cfg.children
} }
func (cfg *Base) Add(child lib.Config) { func (cfg *base) Add(child lib.Config) {
cfg.children = append(cfg.children, child) cfg.children = append(cfg.children, child)
} }
// implementation-specific methods and functions // implementation-specific methods and functions
func (cfg *Base) WithStep(step lib.StepProc) *Base { func (cfg *base) WithStep(step lib.StepProc) *base {
cfg.step = step cfg.step = step
return cfg return cfg
} }
func (cfg *Base) WithMessageHandler(hdlr lib.MessageHandler) *Base { func (cfg *base) WithMessageHandler(hdlr lib.MessageHandler) *base {
cfg.msgHandler = hdlr cfg.msgHandler = hdlr
return cfg return cfg
} }
func (cfg *Base) AddAction(pattern string, spec lib.ActionSpec) { func (cfg *base) AddAction(pattern string, spec lib.ActionSpec) {
act := ActionConfig(pattern, nil) act := ActionConfig(pattern, nil)
act.specs = append(act.specs, spec) act.specs = append(act.specs, spec)
cfg.actions = append(cfg.actions, act) cfg.actions = append(cfg.actions, act)
} }
func MakeBase(name string, starter lib.StartProc) *Base { func Base(name string, starter lib.StartProc) *base {
return &Base{ return &base{
name: name, name: name,
starter: starter, starter: starter,
actions: nil, actions: nil,

View file

@ -9,9 +9,9 @@ import (
func Config() lib.Config { func Config() lib.Config {
ovr := Overrides().Use ovr := Overrides().Use
b := config.MakeBase b := config.Base
cfg := app.Cfg{ cfg := app.Cfg{
Base: b("dummy", app.Start), BaseCfg: b("dummy", app.Start),
Home: ovr(".", HOME), Home: ovr(".", HOME),
AppType: "standard", AppType: "standard",
Logging: &logging.Config{ Logging: &logging.Config{
@ -19,7 +19,7 @@ func Config() lib.Config {
}, },
} }
cfg.Add(config.Cfg{ cfg.Add(config.Cfg{
Base: b("config", config.Start), BaseCfg: b("config", config.Start),
ConfigFormat: "etc", ConfigFormat: "etc",
}) })
return &cfg return &cfg

View file

@ -5,10 +5,10 @@ go 1.20
require git.sr.ht/~cco/go-scopes v0.1.20 require git.sr.ht/~cco/go-scopes v0.1.20
require ( require (
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
github.com/rs/zerolog v1.29.1 // indirect github.com/rs/zerolog v1.29.1 // indirect
golang.org/x/sys v0.8.0 // indirect golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
) )
replace git.sr.ht/~cco/go-scopes => ../../../go-scopes replace git.sr.ht/~cco/go-scopes => ../../../scopes

View file

@ -1,19 +1,13 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc= github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU= github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View file

@ -7,7 +7,6 @@ import (
"git.sr.ht/~cco/go-scopes" "git.sr.ht/~cco/go-scopes"
"git.sr.ht/~cco/go-scopes/app" "git.sr.ht/~cco/go-scopes/app"
"git.sr.ht/~cco/go-scopes/logging"
) )
func main() { func main() {
@ -15,6 +14,5 @@ func main() {
fmt.Println(cfg.Name()) fmt.Println(cfg.Name())
appCfg := cfg.(*app.Cfg) appCfg := cfg.(*app.Cfg)
fmt.Println(appCfg.Home) fmt.Println(appCfg.Home)
logging.Setup(appCfg.Logging, appCfg.Home)
scopes.RunApp(cfg) scopes.RunApp(cfg)
} }

9
go.mod
View file

@ -2,9 +2,10 @@ module git.sr.ht/~cco/go-scopes
go 1.20 go 1.20
require github.com/rs/zerolog v1.29.1
require ( require (
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect github.com/mattn/go-isatty v0.0.14 // indirect
github.com/rs/zerolog v1.29.1 // indirect golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
golang.org/x/sys v0.8.0 // indirect
) )

12
go.sum
View file

@ -1,19 +1,13 @@
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4= github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94= github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc= github.com/rs/zerolog v1.29.1 h1:cO+d60CHkknCbvzEWxP0S9K6KqyTjrCNUy1LdQLCGPc=
github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU= github.com/rs/zerolog v1.29.1/go.mod h1:Le6ESbR7hc+DP6Lt1THiV8CQSdkkNrd3R0XbEgp3ZBU=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 h1:foEbQz/B0Oz6YIqu/69kfXPYeFQAuuMYFkjaqXzl5Wo=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=

View file

@ -20,7 +20,7 @@ func (spec *baseSpec) Receivers() []string {
return spec.receivers return spec.receivers
} }
func BaseSpec(hdlr lib.ActionHandler, rcvrs []string) *baseSpec { func Base(hdlr lib.ActionHandler, rcvrs []string) *baseSpec {
return &baseSpec{hdlr, rcvrs} return &baseSpec{hdlr, rcvrs}
} }

View file

@ -2,6 +2,7 @@ package logging
import ( import (
"fmt" "fmt"
"path/filepath"
"git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/lib"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -18,9 +19,17 @@ type Config struct {
} }
func Setup(ctx lib.Context, cfg *Config, home string) { func Setup(ctx lib.Context, cfg *Config, home string) {
if cfg != nil { if cfg == nil {
fmt.Println("logging:", home, cfg.Logfile) return // use unchanged predefined logger
} }
fmt.Println("logging:", home, cfg.Logfile)
fn := cfg.Logfile
if fn == "" {
fn = filepath.Join(home, "log", "scopes.log")
} else if !filepath.IsAbs(fn) {
fn = filepath.Join(home, fn)
}
fmt.Println("logfile:", fn)
} }
func New(cfg *Config, home string) *Logger { func New(cfg *Config, home string) *Logger {

View file

@ -1,12 +1,16 @@
package scopes package scopes
import ( import (
"git.sr.ht/~cco/go-scopes/app"
"git.sr.ht/~cco/go-scopes/lib" "git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/context" "git.sr.ht/~cco/go-scopes/lib/context"
"git.sr.ht/~cco/go-scopes/logging"
) )
func RunApp(cfg lib.Config) { func RunApp(cfg lib.Config) {
ctx := context.AppContext(cfg) ctx := context.AppContext(cfg)
appCfg := cfg.(*app.Cfg)
logging.Setup(ctx, appCfg.Logging, appCfg.Home)
cfg.Starter()(ctx) cfg.Starter()(ctx)
ctx.WaitGroup().Wait() ctx.WaitGroup().Wait()
} }

View file

@ -12,10 +12,10 @@ import (
func Config() lib.Config { func Config() lib.Config {
ovr := Overrides().Use ovr := Overrides().Use
b := config.MakeBase b := config.Base
cfg := app.Cfg{ cfg := app.Cfg{
Base: b("testing", testing.Start), BaseCfg: b("testing", testing.Start),
Home: ovr(".", HOME), Home: ovr(".", HOME),
AppType: "standard", AppType: "standard",
Logging: &logging.Config{ Logging: &logging.Config{
@ -23,17 +23,17 @@ func Config() lib.Config {
}, },
} }
cfg.AddAction("demo", cfg.AddAction("demo",
action.BaseSpec(action.Forward, action.Base(action.Forward,
[]string{"test-receiver"})) []string{"test-receiver"}))
cfg_config := config.Cfg{ cfg_config := config.Cfg{
Base: b("config", config.Start), BaseCfg: b("config", config.Start),
ConfigFormat: "etc", ConfigFormat: "etc",
} }
cfg.Add(cfg_config) cfg.Add(cfg_config)
cfg_test_rcvr := b("test-receiver", core.Start) cfg_test_rcvr := b("test-receiver", core.Start)
cfg_test_rcvr.AddAction("demo", action.BaseSpec(AH_Receiver, nil)) cfg_test_rcvr.AddAction("demo", action.Base(AH_Receiver, nil))
cfg.Add(cfg_test_rcvr) cfg.Add(cfg_test_rcvr)
return &cfg return &cfg