summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Schlachter <t480-debian-git@schlachter.ca>2025-11-10 00:24:57 -0500
committerDavid Schlachter <t480-debian-git@schlachter.ca>2025-11-10 00:24:57 -0500
commitf45e4b78478f23450b26f3e45a8461a79c73242c (patch)
treeee364791005b64c134d18b84271c40e6fabaeaad
parentd62e3ba8fa65ab5731060250974d9e216b448103 (diff)
Actually create tasks
-rw-r--r--main.go54
1 files changed, 53 insertions, 1 deletions
diff --git a/main.go b/main.go
index a2109e0..dcf5812 100644
--- a/main.go
+++ b/main.go
@@ -2,11 +2,15 @@ package main
import (
"bufio"
+ "bytes"
"fmt"
+ "io"
"log"
+ "net/http"
"os"
"path/filepath"
"regexp"
+ "strconv"
"strings"
"time"
@@ -19,8 +23,16 @@ type inputLine struct {
}
var lastSeenInput = map[inputLine]struct{}{}
+var todoistToken string
+var httpClient = http.Client{
+ Timeout: 10 * time.Second,
+}
func main() {
+ if err := setToken(); err != nil {
+ log.Fatalf("setting token: %s", err)
+ }
+
p, err := inputFilePath()
if err != nil {
log.Fatalf("getting input file: %s", err)
@@ -47,6 +59,16 @@ func main() {
}
+func setToken() error {
+ token := os.Getenv("TODOIST_TOKEN")
+ if token == "" {
+ return fmt.Errorf("TODOIST_TOKEN env var must be present and non-empty")
+ }
+ todoistToken = token
+
+ return nil
+}
+
func inputFilePath() (string, error) {
args := os.Args
if len(args) != 2 {
@@ -108,7 +130,37 @@ func inputChanged(old, new map[inputLine]struct{}) bool {
return false
}
-func createTask(task string) {}
+func createTask(task string) {
+ jsonBody := []byte(fmt.Sprintf(`{"content": %s}`, strconv.Quote(task)))
+ req, err := http.NewRequest(
+ http.MethodPost,
+ "https://api.todoist.com/api/v1/tasks",
+ bytes.NewBuffer(jsonBody),
+ )
+ if err != nil {
+ log.Printf("Failed to create request: %s", err)
+ return
+ }
+ req.Header.Add("Authorization", "Bearer "+todoistToken)
+ req.Header.Set("Content-Type", "application/json")
+
+ resp, err := httpClient.Do(req)
+ if err != nil {
+ log.Printf("Failed to Do request: %s", err)
+ return
+ }
+ defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ log.Printf("Got status code %d, expected 200 when creating task", resp.StatusCode)
+ bodyBytes, err := io.ReadAll(resp.Body)
+ if err != nil {
+ log.Printf("Failed to read body: %s", err)
+ }
+ log.Printf("Got body: %s", string(bodyBytes))
+ } else {
+ log.Printf("Created task '%s'", task)
+ }
+}
func addJobs(c *cron.Cron, tasks map[inputLine]struct{}) {
for task := range tasks {