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
|
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 {
|
type Config struct {
|
||||||
servicePaths []string
|
ServicePaths []string
|
||||||
cron []*cron
|
Cron []*Cron
|
||||||
}
|
}
|
||||||
|
|
||||||
func Read() (*Config, error) {
|
func Read() (*Config, error) {
|
||||||
@ -33,14 +33,14 @@ func Read() (*Config, error) {
|
|||||||
svcdir string
|
svcdir string
|
||||||
serviceAvailable bool
|
serviceAvailable bool
|
||||||
cronAvailable bool
|
cronAvailable bool
|
||||||
cron []*cron
|
cron []*Cron
|
||||||
crontabfile string
|
crontabfile string
|
||||||
)
|
)
|
||||||
|
|
||||||
serviceAvailable = false
|
serviceAvailable = false
|
||||||
cronAvailable = false
|
cronAvailable = false
|
||||||
outConfig := &Config{
|
outConfig := &Config{
|
||||||
servicePaths: make([]string, 0),
|
ServicePaths: make([]string, 0),
|
||||||
}
|
}
|
||||||
configPath := viper.GetString(EnvConfigPath)
|
configPath := viper.GetString(EnvConfigPath)
|
||||||
svcdir = filepath.Join(configPath, ServiceDirName)
|
svcdir = filepath.Join(configPath, ServiceDirName)
|
||||||
@ -49,7 +49,7 @@ func Read() (*Config, error) {
|
|||||||
for _, d := range dirent {
|
for _, d := range dirent {
|
||||||
if d.Type().IsRegular() {
|
if d.Type().IsRegular() {
|
||||||
serviceAvailable = true
|
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)
|
crontabfile = filepath.Join(configPath, CrontabFileName)
|
||||||
cron, err = readCrontab(crontabfile)
|
cron, err = readCrontab(crontabfile)
|
||||||
if len(cron) > 0 {
|
if len(cron) > 0 {
|
||||||
outConfig.cron = cron
|
outConfig.Cron = cron
|
||||||
cronAvailable = true
|
cronAvailable = true
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@ -36,7 +36,7 @@ type CronTimeSpec interface {
|
|||||||
// DayOfWeek() CronTimeSpec
|
// DayOfWeek() CronTimeSpec
|
||||||
// }
|
// }
|
||||||
|
|
||||||
type cron struct {
|
type Cron struct {
|
||||||
minute CronTimeSpec
|
minute CronTimeSpec
|
||||||
hour CronTimeSpec
|
hour CronTimeSpec
|
||||||
dom CronTimeSpec
|
dom CronTimeSpec
|
||||||
@ -60,7 +60,7 @@ const (
|
|||||||
dow
|
dow
|
||||||
)
|
)
|
||||||
|
|
||||||
func readCrontab(path string) ([]*cron, error) {
|
func readCrontab(path string) ([]*Cron, error) {
|
||||||
var (
|
var (
|
||||||
file *os.File
|
file *os.File
|
||||||
err error
|
err error
|
||||||
@ -68,7 +68,7 @@ func readCrontab(path string) ([]*cron, error) {
|
|||||||
line string
|
line string
|
||||||
re *regexp.Regexp
|
re *regexp.Regexp
|
||||||
parts []string
|
parts []string
|
||||||
retval []*cron
|
retval []*Cron
|
||||||
)
|
)
|
||||||
|
|
||||||
if re, err = regexp.Compile(CrontabEntryRegex); err != nil {
|
if re, err = regexp.Compile(CrontabEntryRegex); err != nil {
|
||||||
@ -82,7 +82,7 @@ func readCrontab(path string) ([]*cron, error) {
|
|||||||
_ = file.Close()
|
_ = file.Close()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
retval = make([]*cron, 0)
|
retval = make([]*Cron, 0)
|
||||||
scanner = bufio.NewScanner(file)
|
scanner = bufio.NewScanner(file)
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
line = scanner.Text()
|
line = scanner.Text()
|
||||||
@ -93,7 +93,7 @@ func readCrontab(path string) ([]*cron, error) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
c := &cron{
|
c := &Cron{
|
||||||
hasRun: false,
|
hasRun: false,
|
||||||
}
|
}
|
||||||
if err = c.setField(minute, parts[1]); err != nil {
|
if err = c.setField(minute, parts[1]); err != nil {
|
||||||
@ -129,7 +129,11 @@ func readCrontab(path string) ([]*cron, error) {
|
|||||||
return retval, nil
|
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 {
|
if !c.hasRun {
|
||||||
c.lastRun = now
|
c.lastRun = now
|
||||||
c.hasRun = true
|
c.hasRun = true
|
||||||
@ -169,7 +173,7 @@ func (f *fieldRange) valid(u uint8) bool {
|
|||||||
return i >= f.min && i <= f.max
|
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 (
|
var (
|
||||||
fr *fieldRange
|
fr *fieldRange
|
||||||
cField *CronTimeSpec
|
cField *CronTimeSpec
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user