go-scopes/config/config.go

124 lines
2.1 KiB
Go

package config
import (
"os"
"strings"
"git.sr.ht/~cco/go-scopes/lib"
"git.sr.ht/~cco/go-scopes/lib/core"
)
type Cfg struct {
*Base
ConfigFormat string
}
func Start(ctx lib.Context) {
}
// definitions
type Base struct {
name string
starter lib.StartProc
step lib.StepProc
msgHandler lib.MessageHandler
actions []lib.ActionConfig
children []lib.Config
}
// lib.Config implementation
func (cfg *Base) Name() string {
return cfg.name
}
func (cfg *Base) Starter() lib.StartProc {
return cfg.starter
}
func (cfg *Base) Step() lib.StepProc {
if cfg.step == nil {
return core.Step
}
return cfg.step
}
func (cfg *Base) MessageHandler() lib.MessageHandler {
if cfg.msgHandler == nil {
return core.HandleMessage
}
return cfg.msgHandler
}
func (cfg *Base) Actions() []lib.ActionConfig {
return cfg.actions
}
func (cfg *Base) Children() []lib.Config {
return cfg.children
}
func (cfg *Base) Add(child lib.Config) {
cfg.children = append(cfg.children, child)
}
func (cfg *Base) WithAction(pattern string, spec lib.ActionSpec) lib.Config {
act := ActionConfig(pattern, nil)
act.specs = append(act.specs, spec)
cfg.actions = append(cfg.actions, act)
return cfg
}
// implementation-specific methods and functions
func (cfg *Base) WithStep(step lib.StepProc) *Base {
cfg.step = step
return cfg
}
func (cfg *Base) WithMessageHandler(hdlr lib.MessageHandler) *Base {
cfg.msgHandler = hdlr
return cfg
}
func MakeBase(name string, starter lib.StartProc) *Base {
return &Base{
name: name,
starter: starter,
actions: nil,
}
}
// action configuration
type ActBase struct {
pattern string
specs []lib.ActionSpec
}
func (act *ActBase) Pattern() string {
return act.pattern
}
func (act *ActBase) Specs() []lib.ActionSpec {
return act.specs
}
func ActionConfig(pattern string, specs []lib.ActionSpec) *ActBase {
return &ActBase{pattern, specs}
}
// overridable settings
type Settings map[string]string
func (s Settings) Use(value, key string) string {
if v, ok := os.LookupEnv("SCOPES_" + strings.ToUpper(key)); ok {
return v
}
if v, ok := s[key]; ok {
return v
}
return value
}