trello-bot/reset-daily-tasks.go

126 lines
2.6 KiB
Go

package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/adlio/trello"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
"os"
"time"
)
type archivCard struct {
Name string
List string
}
const DATAPATH = "/home/pi/dailyhabbit"
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
listnames := make(map[string]string)
for _, list := range lists {
listnames[list.ID] = list.Name
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")
var dailyCards []archivCard
for _, card := range cards {
log.Debug().Str("name", card.Name).Msg("Check")
foundLabel := false
for _, label := range card.Labels {
if label.ID == dailyLabel.ID {
foundLabel = true
log.Debug().Interface("card", card).Msg("Found daily card")
break
}
}
if !foundLabel {
log.Debug().Str("name", card.Name).Msg("Not match")
continue
}
cd := archivCard{
Name: card.Name,
List: listnames[card.IDList],
}
dailyCards = append(dailyCards, cd)
err = card.MoveToList(todoList.ID, trello.Defaults())
if err != nil {
return fmt.Errorf("cant move card to todo list: %w", err)
}
}
data, err := json.Marshal(dailyCards)
if err != nil {
return fmt.Errorf("cant marshal data for archiov: %w", err)
}
err = os.WriteFile(fmt.Sprintf("%v/%v.json", DATAPATH, time.Now().Add(-24*time.Hour).Format(time.DateOnly)), data, 0744)
if err != nil {
return fmt.Errorf("cant wrote daily habbit file: %w", err)
}
fmt.Println(string(data))
return nil
}