package config import ( "os" "strings" "git.sr.ht/~cco/go-scopes/lib" ) type Cfg struct { *BaseCfg ConfigFormat string } func Start(ctx lib.Context) { } // definitions type BaseCfg = base type base struct { name string starter lib.StartProc step lib.StepProc msgHandler lib.MessageHandler doneHandler lib.StepProc 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 { return cfg.step } func (cfg *base) MessageHandler() lib.MessageHandler { return cfg.msgHandler } func (cfg *base) DoneHandler() lib.StepProc { return cfg.doneHandler } func (cfg *base) Actions() []lib.ActionConfig { return cfg.actions } func (cfg *base) Children() []lib.Config { return cfg.children } func (cfg *base) Add(c ...lib.Config) lib.Config { cfg.children = append(cfg.children, c...) 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 (cfg *base) WithDoneHandler(hdlr lib.StepProc) *base { cfg.doneHandler = hdlr return cfg } func (cfg *base) AddAction(pattern string, specs ...lib.ActionSpec) lib.Config { act := ActionConfig(pattern, specs) cfg.actions = append(cfg.actions, act) return cfg } func Base(name string, starter lib.StartProc) *base { return &base{ name: name, starter: starter, step: Step, msgHandler: MsgHandler, doneHandler: DoneHandler, actions: nil, } } // will be set by core.init() var Step lib.StepProc var MsgHandler lib.MessageHandler var DoneHandler lib.StepProc // action configuration type action struct { pattern string specs []lib.ActionSpec } func (act *action) Pattern() string { return act.pattern } func (act *action) Specs() []lib.ActionSpec { return act.specs } func ActionConfig(pattern string, specs []lib.ActionSpec) *action { return &action{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 }