trello-bot/reset-daily-tasks.go

126 lines
2.6 KiB
Go
Raw Permalink Normal View History

2024-03-04 02:11:29 +00:00
package main
import (
2024-03-09 01:30:53 +00:00
"encoding/json"
2024-03-04 02:11:29 +00:00
"errors"
"fmt"
"github.com/adlio/trello"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
2024-03-09 01:30:53 +00:00
"os"
"time"
2024-03-04 02:11:29 +00:00
)
2024-03-09 01:30:53 +00:00
type archivCard struct {
Name string
List string
}
const DATAPATH = "/home/pi/dailyhabbit"
2024-03-04 02:11:29 +00:00
func resetDailyTasks(cCtx *cli.Context) error {
client := getTrelloClient()
board, err := client.GetBoard(trelloBordID, trello.Defaults())
if err != nil {
return fmt.Errorf("cant get bord: %w", err)
}
log.Debug().Interface("bord", board).Msg("get bord")
labels, err := board.GetLabels()
if err != nil {
return fmt.Errorf("cant get labels for bord: %w", err)
}
log.Debug().Interface("labels", labels).Msg("got labels")
var dailyLabel *trello.Label
for _, l := range labels {
if l.Name == "Daily" {
dailyLabel = l
break
}
}
if dailyLabel == nil {
return errors.New("cant find label with the name \"Daily\"")
}
log.Debug().Interface("daily label", dailyLabel).Msg("got daily label")
lists, err := board.GetLists(trello.Defaults())
if err != nil {
return fmt.Errorf("cant get losts for board: %w", err)
}
log.Debug().Interface("lists", lists).Msg("got lists")
var todoList *trello.List
2024-03-09 01:30:53 +00:00
listnames := make(map[string]string)
2024-03-04 02:11:29 +00:00
for _, list := range lists {
2024-03-09 01:30:53 +00:00
listnames[list.ID] = list.Name
2024-03-04 02:11:29 +00:00
if list.Name == "ToDo" {
todoList = list
}
}
if todoList == nil {
return errors.New("cant find list with the text \"ToDo\"")
}
cards, err := board.GetCards()
if err != nil {
return fmt.Errorf("cant get cards for bord: %w", err)
}
log.Debug().Int("number of cards", len(cards)).Msg("Get Cards")
2024-03-09 01:30:53 +00:00
var dailyCards []archivCard
2024-03-04 02:11:29 +00:00
for _, card := range cards {
2024-03-07 10:39:08 +00:00
log.Debug().Str("name", card.Name).Msg("Check")
2024-03-04 02:11:29 +00:00
foundLabel := false
for _, label := range card.Labels {
if label.ID == dailyLabel.ID {
foundLabel = true
2024-03-07 10:39:08 +00:00
log.Debug().Interface("card", card).Msg("Found daily card")
2024-03-04 02:11:29 +00:00
break
}
}
if !foundLabel {
2024-03-07 10:39:08 +00:00
log.Debug().Str("name", card.Name).Msg("Not match")
continue
2024-03-04 02:11:29 +00:00
}
2024-03-09 01:30:53 +00:00
cd := archivCard{
Name: card.Name,
List: listnames[card.IDList],
}
dailyCards = append(dailyCards, cd)
2024-03-04 02:11:29 +00:00
err = card.MoveToList(todoList.ID, trello.Defaults())
if err != nil {
return fmt.Errorf("cant move card to todo list: %w", err)
}
}
2024-03-09 01:30:53 +00:00
data, err := json.Marshal(dailyCards)
if err != nil {
return fmt.Errorf("cant marshal data for archiov: %w", err)
}
2024-03-09 01:49:10 +00:00
err = os.WriteFile(fmt.Sprintf("%v/%v.json", DATAPATH, time.Now().Add(-24*time.Hour).Format(time.DateOnly)), data, 0744)
2024-03-09 01:30:53 +00:00
if err != nil {
return fmt.Errorf("cant wrote daily habbit file: %w", err)
}
fmt.Println(string(data))
2024-03-04 02:11:29 +00:00
return nil
}