From 84ab0bf3691c3b6ac84e3ba6a92f93b77078c33d Mon Sep 17 00:00:00 2001 From: simonkellet Date: Fri, 16 Sep 2022 21:05:49 +0100 Subject: [PATCH] tasks now run in goroutines --- run.go | 59 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 20 deletions(-) diff --git a/run.go b/run.go index 2753c83..1ea4523 100644 --- a/run.go +++ b/run.go @@ -4,39 +4,58 @@ import ( "fmt" "os" "os/exec" + "sync" + "time" "github.com/gen2brain/beeep" ) // Run will take the config file and for each task, run a go routine func TaskRun(c Config) { + var wg sync.WaitGroup + for i := range c.Tasks { title := c.Tasks[i].TaskName - desc := c.Tasks[i].TaskDesc - err := beeep.Notify(title, desc, "") - if err != nil { - fmt.Fprintf(os.Stderr, "ERROR: could not display \"%s\":\n\t%s\n", title, err) + interval := c.Tasks[i].Interval + + var desc string + // check if desc is empty and use the cmd output, if not just get the desc + if c.Tasks[i].TaskDesc == "" { + desc = TaskCMD(c.Tasks[i]) + } else { + desc = c.Tasks[i].TaskDesc } - } + asset := c.Tasks[i].Icon + //run the commands with go func()! + wg.Add(1) + go func() { + fmt.Printf("%s has time interval %d\n", title, interval) + for range time.Tick(time.Second * interval) { + err := beeep.Notify(title, desc, asset) + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: could not display \"%s\":\n\t%s\n", title, err) + } + } + + wg.Done() + }() + } + wg.Wait() } -// TaskCMD will take the config struct and run all the "CMD" fields with "bash -c [command]" and return the output -func TaskCMD(c Config) (output string) { - for i := range c.Tasks { - command := c.Tasks[i].CMD +// TaskCMD will take in a Task, run the command with bash and return the string output +func TaskCMD(t Task) (output string) { + command := t.CMD - if command == " " { - fmt.Printf("INFO: Task %s has the cmd section empty. Will just return...\n", c.Tasks[i].TaskName) - return - } + if command == " " { + fmt.Printf("INFO: Task %s has the cmd section empty. Will just return...\n", t.TaskName) + return + } - out, err := exec.Command("bash", "-c", command).Output() - if err != nil { - fmt.Fprintf(os.Stderr, "ERROR: could not run command \"%s\":\n\t%s\n", command, err) - } - //fmt.Printf("Output:\n%s\n", out) - return string(out) + out, err := exec.Command("bash", "-c", command).Output() + if err != nil { + fmt.Fprintf(os.Stderr, "ERROR: could not run command \"%s\":\n\t%s\n", command, err) } - return + return string(out) }