Start routine at wakeup end
This commit is contained in:
parent
1def1ff67a
commit
e1f5fbcd6c
7 changed files with 101 additions and 16 deletions
|
@ -1,7 +1,9 @@
|
|||
package reveil
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha512"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
|
@ -9,6 +11,7 @@ import (
|
|||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.nemunai.re/nemunaire/reveil/config"
|
||||
)
|
||||
|
@ -19,6 +22,10 @@ type RoutineStep struct {
|
|||
Args []string `json:"args,omitempty"`
|
||||
}
|
||||
|
||||
func (s *RoutineStep) GetAction(cfg *config.Config) (*Action, error) {
|
||||
return LoadAction(cfg, s.Action)
|
||||
}
|
||||
|
||||
type Routine struct {
|
||||
Id Identifier `json:"id"`
|
||||
Name string `json:"name"`
|
||||
|
@ -72,6 +79,21 @@ func LoadRoutine(path string, cfg *config.Config) ([]RoutineStep, error) {
|
|||
return steps, nil
|
||||
}
|
||||
|
||||
func LoadRoutineFromId(id Identifier, cfg *config.Config) (*Routine, error) {
|
||||
routines, err := LoadRoutines(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, routine := range routines {
|
||||
if bytes.Equal(routine.Id, id) {
|
||||
return routine, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("Unable to find routine %x", id)
|
||||
}
|
||||
|
||||
func LoadRoutines(cfg *config.Config) (routines []*Routine, err error) {
|
||||
err = filepath.Walk(cfg.RoutinesDir, func(path string, d fs.FileInfo, err error) error {
|
||||
if d.IsDir() && path != cfg.RoutinesDir {
|
||||
|
@ -117,3 +139,28 @@ func (r *Routine) Rename(newName string) error {
|
|||
func (a *Routine) Remove() error {
|
||||
return os.Remove(a.Path)
|
||||
}
|
||||
|
||||
func (a *Routine) Launch(cfg *config.Config) error {
|
||||
for _, s := range a.Steps {
|
||||
act, err := s.GetAction(cfg)
|
||||
if err != nil {
|
||||
log.Printf("Unable to get action: %s: %s", s.Action, err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
time.Sleep(time.Duration(s.Delay) * time.Second)
|
||||
|
||||
cmd, err := act.Launch()
|
||||
if err != nil {
|
||||
log.Printf("Unable to launch the action %q: %s", s.Action, err.Error())
|
||||
continue
|
||||
}
|
||||
|
||||
err = cmd.Wait()
|
||||
if err != nil {
|
||||
log.Printf("Something goes wrong when waiting for the action %q's end: %s", s.Action, err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue