wip: ready for initial test
This commit is contained in:
parent
bd4ba67ad2
commit
2971f5c709
60
cmd/wingmate/bridge.go
Normal file
60
cmd/wingmate/bridge.go
Normal file
@ -0,0 +1,60 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"gitea.suyono.dev/suyono/wingmate/config"
|
||||
wminit "gitea.suyono.dev/suyono/wingmate/init"
|
||||
)
|
||||
|
||||
type wPath struct {
|
||||
path string
|
||||
}
|
||||
|
||||
func (p wPath) Path() string {
|
||||
return p.path
|
||||
}
|
||||
|
||||
type wConfig struct {
|
||||
services []wminit.Path
|
||||
cron []wminit.Cron
|
||||
}
|
||||
|
||||
func (c wConfig) Services() []wminit.Path {
|
||||
return c.services
|
||||
}
|
||||
|
||||
func (c wConfig) Cron() []wminit.Cron {
|
||||
return c.cron
|
||||
}
|
||||
|
||||
type wCron struct {
|
||||
iCron *config.Cron
|
||||
}
|
||||
|
||||
func (c wCron) TimeToRun(now time.Time) bool {
|
||||
return c.iCron.TimeToRun(now)
|
||||
}
|
||||
|
||||
func (c wCron) Command() wminit.Path {
|
||||
return wPath{
|
||||
path: c.iCron.Command(),
|
||||
}
|
||||
}
|
||||
|
||||
func convert(cfg *config.Config) wConfig {
|
||||
retval := wConfig{
|
||||
services: make([]wminit.Path, 0, len(cfg.ServicePaths)),
|
||||
cron: make([]wminit.Cron, 0, len(cfg.Cron)),
|
||||
}
|
||||
|
||||
for _, s := range cfg.ServicePaths {
|
||||
retval.services = append(retval.services, wPath{path: s})
|
||||
}
|
||||
|
||||
for _, c := range cfg.Cron {
|
||||
retval.cron = append(retval.cron, wCron{iCron: c})
|
||||
}
|
||||
|
||||
return retval
|
||||
}
|
||||
@ -1,5 +1,24 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
import (
|
||||
"os"
|
||||
|
||||
"gitea.suyono.dev/suyono/wingmate"
|
||||
"gitea.suyono.dev/suyono/wingmate/config"
|
||||
wminit "gitea.suyono.dev/suyono/wingmate/init"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
err error
|
||||
cfg *config.Config
|
||||
)
|
||||
|
||||
_ = wingmate.NewLog(os.Stderr)
|
||||
if cfg, err = config.Read(); err != nil {
|
||||
wingmate.Log().Error().Msgf("failed to read config %#v", err)
|
||||
}
|
||||
|
||||
initCfg := convert(cfg)
|
||||
wminit.NewInit(initCfg).Start()
|
||||
}
|
||||
|
||||
@ -18,8 +18,8 @@ const (
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
servicePaths []string
|
||||
cron []*cron
|
||||
ServicePaths []string
|
||||
Cron []*Cron
|
||||
}
|
||||
|
||||
func Read() (*Config, error) {
|
||||
@ -33,14 +33,14 @@ func Read() (*Config, error) {
|
||||
svcdir string
|
||||
serviceAvailable bool
|
||||
cronAvailable bool
|
||||
cron []*cron
|
||||
cron []*Cron
|
||||
crontabfile string
|
||||
)
|
||||
|
||||
serviceAvailable = false
|
||||
cronAvailable = false
|
||||
outConfig := &Config{
|
||||
servicePaths: make([]string, 0),
|
||||
ServicePaths: make([]string, 0),
|
||||
}
|
||||
configPath := viper.GetString(EnvConfigPath)
|
||||
svcdir = filepath.Join(configPath, ServiceDirName)
|
||||
@ -49,7 +49,7 @@ func Read() (*Config, error) {
|
||||
for _, d := range dirent {
|
||||
if d.Type().IsRegular() {
|
||||
serviceAvailable = true
|
||||
outConfig.servicePaths = append(outConfig.servicePaths, filepath.Join(svcdir, d.Name()))
|
||||
outConfig.ServicePaths = append(outConfig.ServicePaths, filepath.Join(svcdir, d.Name()))
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -60,7 +60,7 @@ func Read() (*Config, error) {
|
||||
crontabfile = filepath.Join(configPath, CrontabFileName)
|
||||
cron, err = readCrontab(crontabfile)
|
||||
if len(cron) > 0 {
|
||||
outConfig.cron = cron
|
||||
outConfig.Cron = cron
|
||||
cronAvailable = true
|
||||
}
|
||||
if err != nil {
|
||||
|
||||
@ -36,7 +36,7 @@ type CronTimeSpec interface {
|
||||
// DayOfWeek() CronTimeSpec
|
||||
// }
|
||||
|
||||
type cron struct {
|
||||
type Cron struct {
|
||||
minute CronTimeSpec
|
||||
hour CronTimeSpec
|
||||
dom CronTimeSpec
|
||||
@ -60,7 +60,7 @@ const (
|
||||
dow
|
||||
)
|
||||
|
||||
func readCrontab(path string) ([]*cron, error) {
|
||||
func readCrontab(path string) ([]*Cron, error) {
|
||||
var (
|
||||
file *os.File
|
||||
err error
|
||||
@ -68,7 +68,7 @@ func readCrontab(path string) ([]*cron, error) {
|
||||
line string
|
||||
re *regexp.Regexp
|
||||
parts []string
|
||||
retval []*cron
|
||||
retval []*Cron
|
||||
)
|
||||
|
||||
if re, err = regexp.Compile(CrontabEntryRegex); err != nil {
|
||||
@ -82,7 +82,7 @@ func readCrontab(path string) ([]*cron, error) {
|
||||
_ = file.Close()
|
||||
}()
|
||||
|
||||
retval = make([]*cron, 0)
|
||||
retval = make([]*Cron, 0)
|
||||
scanner = bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
line = scanner.Text()
|
||||
@ -93,7 +93,7 @@ func readCrontab(path string) ([]*cron, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
c := &cron{
|
||||
c := &Cron{
|
||||
hasRun: false,
|
||||
}
|
||||
if err = c.setField(minute, parts[1]); err != nil {
|
||||
@ -129,7 +129,11 @@ func readCrontab(path string) ([]*cron, error) {
|
||||
return retval, nil
|
||||
}
|
||||
|
||||
func (c *cron) TimeToRun(now time.Time) bool {
|
||||
func (c *Cron) Command() string {
|
||||
return c.command
|
||||
}
|
||||
|
||||
func (c *Cron) TimeToRun(now time.Time) bool {
|
||||
if !c.hasRun {
|
||||
c.lastRun = now
|
||||
c.hasRun = true
|
||||
@ -169,7 +173,7 @@ func (f *fieldRange) valid(u uint8) bool {
|
||||
return i >= f.min && i <= f.max
|
||||
}
|
||||
|
||||
func (c *cron) setField(field cronField, input string) error {
|
||||
func (c *Cron) setField(field cronField, input string) error {
|
||||
var (
|
||||
fr *fieldRange
|
||||
cField *CronTimeSpec
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user