Compare commits

..
7 Commits
Author SHA1 Message Date
simonkellet 449d7a1ff9 i think it works okay 2022-09-16 21:31:03 +01:00
simonkellet 67dc1775b2 added assets folder 2022-09-16 21:07:15 +01:00
simonkellet 3a5b0135f8 added some example tasks 2022-09-16 21:06:57 +01:00
simonkellet e926e9fcc2 added json example 2022-09-16 21:06:34 +01:00
simonkellet 0504561263 finished main func 2022-09-16 21:06:17 +01:00
simonkellet 84ab0bf369 tasks now run in goroutines 2022-09-16 21:05:49 +01:00
simonkellet acff443905 changed int32 to time.Duration type 2022-09-16 21:05:07 +01:00
7 changed files with 122 additions and 44 deletions
+25 -2
View File
@@ -1,5 +1,7 @@
# Go Task Reminder
***unfinished***
## The Goal
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:
```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
* Create sample config
* check these two locations for a config:
* /home/user/.config/rem/tasks.json
* /home/user/go/src/rem/tasks.json
* More!
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 36 KiB

+20
View File
@@ -0,0 +1,20 @@
{
"Tasks":
[
{
"taskname": "Cat",
"taskdesc": "cat lol",
"interval": 1000,
"cmd": "" ,
"icon": "./assets/catlol.jpg"
},
{
"taskname": "",
"taskdesc": "",
"interval": 36,
"cmd": "we",
"icon": ""
}
]
}
+2 -4
View File
@@ -8,16 +8,14 @@ import (
func main() {
var configFlag string
flag.StringVar(&configFlag, "c", "", "path to tasks.json")
flag.StringVar(&configFlag, "c", "", "path to .json file")
flag.Parse()
fmt.Printf("Reading JSON file: %s\n", configFlag)
config, err := LoadConfig(configFlag)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot load config file: %s\n\t%s\n", configFlag, err)
os.Exit(1)
}
TaskRun(*config)
//exampleoutput := TaskCMD(*config)
//fmt.Printf("%s", exampleoutput)
}
+29 -10
View File
@@ -4,30 +4,52 @@ 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, "")
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
}
//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)
fmt.Printf("INFO: Task %s has the cmd section empty. Will just return...\n", t.TaskName)
return
}
@@ -35,8 +57,5 @@ func TaskCMD(c Config) (output string) {
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)
}
return
}
+3 -1
View File
@@ -1,11 +1,13 @@
package main
import "time"
// TODO: Finish off doc. here
type Task struct {
TaskName string `json:"taskname"`
TaskDesc string `json:"taskdesc"`
Interval int32 `json:"interval"`
Interval time.Duration `json:"interval"`
CMD string `json:"cmd"`
Icon string `json:"icon"`
}
+22 -6
View File
@@ -2,19 +2,35 @@
"Tasks":
[
{
"taskname": "Example Task",
"taskdesc": "This is placeholder",
"interval": 100,
"cmd": "we" ,
"taskname": "Example ls | wc -l Task",
"taskdesc": "",
"interval": 60,
"cmd": "ls /home/simon/ | wc -l" ,
"icon": ""
},
{
"taskname": "Second Example",
"taskname": "Example with no cmd",
"taskdesc": "Still Place holder",
"interval": -1,
"interval": 80,
"cmd": "",
"icon": ""
},
{
"taskname": "Example image task",
"taskdesc": "cat lol",
"interval": 100,
"cmd": "" ,
"icon": "./assets/catlol.jpg"
},
{
"taskname": "",
"taskdesc": "",
"interval": 3600,
"cmd": "we",
"icon": ""
}
]
}