You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
go-taskrem/run.go

62 lines
1.3 KiB

2 years ago
package main
2 years ago
import (
"fmt"
"os"
"os/exec"
"sync"
"time"
2 years ago
"github.com/gen2brain/beeep"
)
2 years ago
// Run will take the config file and for each task, run a go routine
func TaskRun(c Config) {
var wg sync.WaitGroup
2 years ago
for i := range c.Tasks {
title := c.Tasks[i].TaskName
interval := c.Tasks[i].Interval
asset := c.Tasks[i].Icon
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
2 years ago
}
2 years ago
//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()
2 years ago
}
2 years ago
// 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
2 years ago
if command == " " {
fmt.Printf("INFO: Task %s has the cmd section empty. Will just return...\n", t.TaskName)
return
}
2 years ago
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)
2 years ago
}
return string(out)
2 years ago
}