rename config base stuff, + some minor refactorings
This commit is contained in:
		
							parent
							
								
									2141e41241
								
							
						
					
					
						commit
						72abf7cc0f
					
				
					 12 changed files with 56 additions and 54 deletions
				
			
		| 
						 | 
				
			
			@ -14,7 +14,7 @@ import (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
type Cfg struct {
 | 
			
		||||
	*config.Base
 | 
			
		||||
	*config.BaseCfg
 | 
			
		||||
	AppType string
 | 
			
		||||
	Home    string
 | 
			
		||||
	Logging *logging.Config
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,7 +9,7 @@ import (
 | 
			
		|||
)
 | 
			
		||||
 | 
			
		||||
type Cfg struct {
 | 
			
		||||
	*Base
 | 
			
		||||
	*BaseCfg
 | 
			
		||||
	ConfigFormat string
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -18,7 +18,9 @@ func Start(ctx lib.Context) {
 | 
			
		|||
 | 
			
		||||
// definitions
 | 
			
		||||
 | 
			
		||||
type Base struct {
 | 
			
		||||
type BaseCfg = base
 | 
			
		||||
 | 
			
		||||
type base struct {
 | 
			
		||||
	name       string
 | 
			
		||||
	starter    lib.StartProc
 | 
			
		||||
	step       lib.StepProc
 | 
			
		||||
| 
						 | 
				
			
			@ -29,60 +31,60 @@ type Base struct {
 | 
			
		|||
 | 
			
		||||
// lib.Config implementation
 | 
			
		||||
 | 
			
		||||
func (cfg *Base) Name() string {
 | 
			
		||||
func (cfg *base) Name() string {
 | 
			
		||||
	return cfg.name
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (cfg *Base) Starter() lib.StartProc {
 | 
			
		||||
func (cfg *base) Starter() lib.StartProc {
 | 
			
		||||
	return cfg.starter
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (cfg *Base) Step() lib.StepProc {
 | 
			
		||||
func (cfg *base) Step() lib.StepProc {
 | 
			
		||||
	if cfg.step == nil {
 | 
			
		||||
		return core.Step
 | 
			
		||||
	}
 | 
			
		||||
	return cfg.step
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (cfg *Base) MessageHandler() lib.MessageHandler {
 | 
			
		||||
func (cfg *base) MessageHandler() lib.MessageHandler {
 | 
			
		||||
	if cfg.msgHandler == nil {
 | 
			
		||||
		return core.HandleMessage
 | 
			
		||||
	}
 | 
			
		||||
	return cfg.msgHandler
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (cfg *Base) Actions() []lib.ActionConfig {
 | 
			
		||||
func (cfg *base) Actions() []lib.ActionConfig {
 | 
			
		||||
	return cfg.actions
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (cfg *Base) Children() []lib.Config {
 | 
			
		||||
func (cfg *base) Children() []lib.Config {
 | 
			
		||||
	return cfg.children
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (cfg *Base) Add(child lib.Config) {
 | 
			
		||||
func (cfg *base) Add(child lib.Config) {
 | 
			
		||||
	cfg.children = append(cfg.children, child)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// implementation-specific methods and functions
 | 
			
		||||
 | 
			
		||||
func (cfg *Base) WithStep(step lib.StepProc) *Base {
 | 
			
		||||
func (cfg *base) WithStep(step lib.StepProc) *base {
 | 
			
		||||
	cfg.step = step
 | 
			
		||||
	return cfg
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (cfg *Base) WithMessageHandler(hdlr lib.MessageHandler) *Base {
 | 
			
		||||
func (cfg *base) WithMessageHandler(hdlr lib.MessageHandler) *base {
 | 
			
		||||
	cfg.msgHandler = hdlr
 | 
			
		||||
	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.specs = append(act.specs, spec)
 | 
			
		||||
	cfg.actions = append(cfg.actions, act)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func MakeBase(name string, starter lib.StartProc) *Base {
 | 
			
		||||
	return &Base{
 | 
			
		||||
func Base(name string, starter lib.StartProc) *base {
 | 
			
		||||
	return &base{
 | 
			
		||||
		name:    name,
 | 
			
		||||
		starter: starter,
 | 
			
		||||
		actions: nil,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -9,9 +9,9 @@ import (
 | 
			
		|||
 | 
			
		||||
func Config() lib.Config {
 | 
			
		||||
	ovr := Overrides().Use
 | 
			
		||||
	b := config.MakeBase
 | 
			
		||||
	b := config.Base
 | 
			
		||||
	cfg := app.Cfg{
 | 
			
		||||
		Base:    b("dummy", app.Start),
 | 
			
		||||
		BaseCfg: b("dummy", app.Start),
 | 
			
		||||
		Home:    ovr(".", HOME),
 | 
			
		||||
		AppType: "standard",
 | 
			
		||||
		Logging: &logging.Config{
 | 
			
		||||
| 
						 | 
				
			
			@ -19,7 +19,7 @@ func Config() lib.Config {
 | 
			
		|||
		},
 | 
			
		||||
	}
 | 
			
		||||
	cfg.Add(config.Cfg{
 | 
			
		||||
		Base:         b("config", config.Start),
 | 
			
		||||
		BaseCfg:      b("config", config.Start),
 | 
			
		||||
		ConfigFormat: "etc",
 | 
			
		||||
	})
 | 
			
		||||
	return &cfg
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -5,10 +5,10 @@ go 1.20
 | 
			
		|||
require git.sr.ht/~cco/go-scopes v0.1.20
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/mattn/go-colorable v0.1.13 // indirect
 | 
			
		||||
	github.com/mattn/go-isatty v0.0.19 // indirect
 | 
			
		||||
	github.com/mattn/go-colorable v0.1.12 // indirect
 | 
			
		||||
	github.com/mattn/go-isatty v0.0.14 // 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
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,19 +1,13 @@
 | 
			
		|||
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/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.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
 | 
			
		||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
 | 
			
		||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
 | 
			
		||||
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/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/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-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-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=
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -7,7 +7,6 @@ import (
 | 
			
		|||
 | 
			
		||||
	"git.sr.ht/~cco/go-scopes"
 | 
			
		||||
	"git.sr.ht/~cco/go-scopes/app"
 | 
			
		||||
	"git.sr.ht/~cco/go-scopes/logging"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
| 
						 | 
				
			
			@ -15,6 +14,5 @@ func main() {
 | 
			
		|||
	fmt.Println(cfg.Name())
 | 
			
		||||
	appCfg := cfg.(*app.Cfg)
 | 
			
		||||
	fmt.Println(appCfg.Home)
 | 
			
		||||
	logging.Setup(appCfg.Logging, appCfg.Home)
 | 
			
		||||
	scopes.RunApp(cfg)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										9
									
								
								go.mod
									
										
									
									
									
								
							
							
						
						
									
										9
									
								
								go.mod
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -2,9 +2,10 @@ module git.sr.ht/~cco/go-scopes
 | 
			
		|||
 | 
			
		||||
go 1.20
 | 
			
		||||
 | 
			
		||||
require github.com/rs/zerolog v1.29.1
 | 
			
		||||
 | 
			
		||||
require (
 | 
			
		||||
	github.com/mattn/go-colorable v0.1.13 // indirect
 | 
			
		||||
	github.com/mattn/go-isatty v0.0.19 // indirect
 | 
			
		||||
	github.com/rs/zerolog v1.29.1 // indirect
 | 
			
		||||
	golang.org/x/sys v0.8.0 // indirect
 | 
			
		||||
	github.com/mattn/go-colorable v0.1.12 // indirect
 | 
			
		||||
	github.com/mattn/go-isatty v0.0.14 // indirect
 | 
			
		||||
	golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6 // indirect
 | 
			
		||||
)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										12
									
								
								go.sum
									
										
									
									
									
								
							
							
						
						
									
										12
									
								
								go.sum
									
										
									
									
									
								
							| 
						 | 
				
			
			@ -1,19 +1,13 @@
 | 
			
		|||
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/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.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
 | 
			
		||||
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
 | 
			
		||||
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
 | 
			
		||||
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/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/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-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-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=
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -20,7 +20,7 @@ func (spec *baseSpec) Receivers() []string {
 | 
			
		|||
	return spec.receivers
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func BaseSpec(hdlr lib.ActionHandler, rcvrs []string) *baseSpec {
 | 
			
		||||
func Base(hdlr lib.ActionHandler, rcvrs []string) *baseSpec {
 | 
			
		||||
	return &baseSpec{hdlr, rcvrs}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -2,6 +2,7 @@ package logging
 | 
			
		|||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
 | 
			
		||||
	"git.sr.ht/~cco/go-scopes/lib"
 | 
			
		||||
	"github.com/rs/zerolog"
 | 
			
		||||
| 
						 | 
				
			
			@ -18,9 +19,17 @@ type Config struct {
 | 
			
		|||
}
 | 
			
		||||
 | 
			
		||||
func Setup(ctx lib.Context, cfg *Config, home string) {
 | 
			
		||||
	if cfg != nil {
 | 
			
		||||
		fmt.Println("logging:", home, cfg.Logfile)
 | 
			
		||||
	if cfg == nil {
 | 
			
		||||
		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 {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,12 +1,16 @@
 | 
			
		|||
package scopes
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"git.sr.ht/~cco/go-scopes/app"
 | 
			
		||||
	"git.sr.ht/~cco/go-scopes/lib"
 | 
			
		||||
	"git.sr.ht/~cco/go-scopes/lib/context"
 | 
			
		||||
	"git.sr.ht/~cco/go-scopes/logging"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func RunApp(cfg lib.Config) {
 | 
			
		||||
	ctx := context.AppContext(cfg)
 | 
			
		||||
	appCfg := cfg.(*app.Cfg)
 | 
			
		||||
	logging.Setup(ctx, appCfg.Logging, appCfg.Home)
 | 
			
		||||
	cfg.Starter()(ctx)
 | 
			
		||||
	ctx.WaitGroup().Wait()
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -12,10 +12,10 @@ import (
 | 
			
		|||
 | 
			
		||||
func Config() lib.Config {
 | 
			
		||||
	ovr := Overrides().Use
 | 
			
		||||
	b := config.MakeBase
 | 
			
		||||
	b := config.Base
 | 
			
		||||
 | 
			
		||||
	cfg := app.Cfg{
 | 
			
		||||
		Base:    b("testing", testing.Start),
 | 
			
		||||
		BaseCfg: b("testing", testing.Start),
 | 
			
		||||
		Home:    ovr(".", HOME),
 | 
			
		||||
		AppType: "standard",
 | 
			
		||||
		Logging: &logging.Config{
 | 
			
		||||
| 
						 | 
				
			
			@ -23,17 +23,17 @@ func Config() lib.Config {
 | 
			
		|||
		},
 | 
			
		||||
	}
 | 
			
		||||
	cfg.AddAction("demo",
 | 
			
		||||
		action.BaseSpec(action.Forward,
 | 
			
		||||
		action.Base(action.Forward,
 | 
			
		||||
			[]string{"test-receiver"}))
 | 
			
		||||
 | 
			
		||||
	cfg_config := config.Cfg{
 | 
			
		||||
		Base:         b("config", config.Start),
 | 
			
		||||
		BaseCfg:      b("config", config.Start),
 | 
			
		||||
		ConfigFormat: "etc",
 | 
			
		||||
	}
 | 
			
		||||
	cfg.Add(cfg_config)
 | 
			
		||||
 | 
			
		||||
	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)
 | 
			
		||||
 | 
			
		||||
	return &cfg
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue