Compare commits

...

6 Commits

Author SHA1 Message Date
simonkellet 67dc1775b2 added assets folder 2 years ago
simonkellet 3a5b0135f8 added some example tasks 2 years ago
simonkellet e926e9fcc2 added json example 2 years ago
simonkellet 0504561263 finished main func 2 years ago
simonkellet 84ab0bf369 tasks now run in goroutines 2 years ago
simonkellet acff443905 changed int32 to time.Duration type 2 years ago
  1. 33
      README.md
  2. BIN
      assets/catlol.jpg
  3. 3
      main.go
  4. 59
      run.go
  5. 12
      tasks.go
  6. 26
      tasks.json

@ -1,5 +1,7 @@
# Go Task Reminder # Go Task Reminder
***unfinished*** ***unfinished***
## The Goal ## The Goal
Create a **Task Reminder** application that will: Create a **Task Reminder** application that will:
@ -17,11 +19,32 @@ Of course, handle all the errors that are involved with this:
The JSON structure should look like this: The JSON structure should look like this:
```json ```json
{
"Tasks":
[
{
"taskname": "Example Task",
"taskdesc": "",
"interval": 100,
"cmd": "ls | wc -l" ,
"icon": ""
},
{
"taskname": "Second Example",
"taskdesc": "catlol",
"interval": 15,
"cmd": "",
"icon": "./assets/catlol.jpg"
}
]
}
``` ```
This file can be placed in either: ## TODO
* /home/user/.config/rem/tasks.json * Create sample config
* /home/user/go/src/rem/tasks.json * check these two locations for a config:
* /home/user/.config/rem/tasks.json
* /home/user/go/src/rem/tasks.json
* More!

Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

@ -16,8 +16,5 @@ func main() {
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "Cannot load config file: %s\n\t%s\n", configFlag, err) fmt.Fprintf(os.Stderr, "Cannot load config file: %s\n\t%s\n", configFlag, err)
} }
TaskRun(*config) TaskRun(*config)
//exampleoutput := TaskCMD(*config)
//fmt.Printf("%s", exampleoutput)
} }

@ -4,39 +4,58 @@ import (
"fmt" "fmt"
"os" "os"
"os/exec" "os/exec"
"sync"
"time"
"github.com/gen2brain/beeep" "github.com/gen2brain/beeep"
) )
// Run will take the config file and for each task, run a go routine // Run will take the config file and for each task, run a go routine
func TaskRun(c Config) { func TaskRun(c Config) {
var wg sync.WaitGroup
for i := range c.Tasks { for i := range c.Tasks {
title := c.Tasks[i].TaskName title := c.Tasks[i].TaskName
desc := c.Tasks[i].TaskDesc interval := c.Tasks[i].Interval
err := beeep.Notify(title, desc, "")
if err != nil { var desc string
fmt.Fprintf(os.Stderr, "ERROR: could not display \"%s\":\n\t%s\n", title, err) // 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 // TaskCMD will take in a Task, run the command with bash and return the string output
func TaskCMD(c Config) (output string) { func TaskCMD(t Task) (output string) {
for i := range c.Tasks { command := t.CMD
command := c.Tasks[i].CMD
if command == " " { if command == " " {
fmt.Printf("INFO: Task %s has the cmd section empty. Will just return...\n", c.Tasks[i].TaskName) fmt.Printf("INFO: Task %s has the cmd section empty. Will just return...\n", t.TaskName)
return return
} }
out, err := exec.Command("bash", "-c", command).Output() out, err := exec.Command("bash", "-c", command).Output()
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: could not run command \"%s\":\n\t%s\n", command, err) 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)
} }
return return string(out)
} }

@ -1,11 +1,13 @@
package main package main
import "time"
// TODO: Finish off doc. here // TODO: Finish off doc. here
type Task struct { type Task struct {
TaskName string `json:"taskname"` TaskName string `json:"taskname"`
TaskDesc string `json:"taskdesc"` TaskDesc string `json:"taskdesc"`
Interval int32 `json:"interval"` Interval time.Duration `json:"interval"`
CMD string `json:"cmd"` CMD string `json:"cmd"`
Icon string `json:"icon"` Icon string `json:"icon"`
} }

@ -2,19 +2,27 @@
"Tasks": "Tasks":
[ [
{ {
"taskname": "Example Task", "taskname": "Example ls | wc -l Task",
"taskdesc": "This is placeholder", "taskdesc": "",
"interval": 100, "interval": 6,
"cmd": "we" , "cmd": "ls /home/simon/ | wc -l" ,
"icon": " " "icon": ""
}, },
{ {
"taskname": "Second Example", "taskname": "Example with no cmd",
"taskdesc": "Still Place holder", "taskdesc": "Still Place holder",
"interval": -1, "interval": 8,
"cmd": " ", "cmd": "",
"icon": " " "icon": ""
},
{
"taskname": "Example image task",
"taskdesc": "cat lol",
"interval": 10,
"cmd": "" ,
"icon": "./assets/catlol.jpg"
} }
] ]
} }

Loading…
Cancel
Save