Skip to content

Commit

Permalink
working with multiple lists (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrechka authored Sep 19, 2022
1 parent 6ebab6e commit e964c62
Showing 1 changed file with 40 additions and 27 deletions.
67 changes: 40 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"fmt"
"log"
"os"
"strconv"
"strings"
"sync"
"time"

Expand All @@ -12,14 +13,13 @@ import (

const TRELLO_KEY_ENV = "TRELLO_KEY"
const TRELLO_TOKEN_ENV = "TRELLO_TOKEN"
const TRELLO_LIST_ENV = "TRELLO_LIST"
const TRELLO_LISTS_ENV = "TRELLO_LISTS"
const CARD_INACTIVITY_ARCHIVAL_THRESHOLD_HOURS_ENV = "CARD_INACTIVITY_ARCHIVAL_THRESHOLD_HOURS"

func extractEnvOrExit(envKey string) string {
data, defined := os.LookupEnv(envKey)
if !defined {
fmt.Printf("ERROR: \"%s\" env var is not defined\n", envKey)
os.Exit(1)
log.Fatalf("ERROR: \"%s\" env var is not defined\n", envKey)
}
return data
}
Expand All @@ -36,7 +36,7 @@ func checkCardForArchive(card *trello.Card, inactivityTimeSpan time.Duration, no
defer wg.Done()
elapsed := now.Sub(*card.DateLastActivity)
if elapsed > inactivityTimeSpan {
fmt.Printf("Card \"%v\" is due to archive as last activity was %v ago\n", card.Name, elapsed)
log.Printf("Card \"%v\" is due to archive as last activity was %v ago\n", card.Name, elapsed)
return card.Archive()
}
return nil
Expand All @@ -46,37 +46,50 @@ func main() {
trelloAppKey := extractEnvOrExit(TRELLO_KEY_ENV)
trelloToken := extractEnvOrExit(TRELLO_TOKEN_ENV)
//trelloAppMemberId := extractEnvOrExit(TRELLO_APP_MEMBER_ID_ENV)
trelloList := extractEnvOrExit(TRELLO_LIST_ENV)
trelloLists := extractEnvOrExit(TRELLO_LISTS_ENV)
cardInactivityArchivalThesholdHoursStr := extractEnvOrDefault(CARD_INACTIVITY_ARCHIVAL_THRESHOLD_HOURS_ENV, "336")
cardInactivityArchivalThesholdHours, err := strconv.ParseFloat(cardInactivityArchivalThesholdHoursStr, 64)
if err != nil {
fmt.Printf("ERROR: can't parse number of card inactivity archival threshold (hours). String: %s \n", cardInactivityArchivalThesholdHoursStr)
log.Fatalf("ERROR: can't parse number of card inactivity archival threshold (hours). String: %s \n", cardInactivityArchivalThesholdHoursStr)
}
var cardInactivityArchivalThreshold time.Duration = time.Duration(cardInactivityArchivalThesholdHours * 60 * 60 * 1e9)

fmt.Printf("Querying cards of the list %v... \n", trelloList)

client := trello.NewClient(trelloAppKey, trelloToken)

list, err := client.GetList(trelloList)
if err != nil {
fmt.Printf("Can't fetch list %v: %v", trelloList, err)
os.Exit(2)
}
cards, err := list.GetCards()
if err != nil {
fmt.Printf("Can't fetch cards for %v: %v", list.Name, err)
os.Exit(2)
}
fmt.Printf("The list %v contains %d cards\n", list.Name, len(cards))
checkList := func(listId string, wg *sync.WaitGroup) {
list, err := client.GetList(listId)
if err != nil {
log.Fatalf("Can't fetch list %v: %v", listId, err)
}
log.Printf("Querying cards of the list %v (%v)... \n", listId, list.Name)
cards, err := list.GetCards()
if err != nil {
log.Fatalf("Can't fetch cards for %v: %v", list.Name, err)
os.Exit(2)
}
log.Printf("The list %v contains %d cards\n", list.Name, len(cards))

now := time.Now()

now := time.Now()
var archivalCheckWg sync.WaitGroup
archivalCheckWg.Add(len(cards))
for _, card := range cards {
go checkCardForArchive(card, cardInactivityArchivalThreshold, now, &archivalCheckWg)
}
archivalCheckWg.Wait()
wg.Done()
log.Printf("List %v processed", list.Name)
}

var archivalCheckWg sync.WaitGroup
archivalCheckWg.Add(len(cards))
for _, card := range cards {
go checkCardForArchive(card, cardInactivityArchivalThreshold, now, &archivalCheckWg)
trelloListsSplit := strings.Split(trelloLists, ",")
N := len(trelloListsSplit)
log.Printf("%d lists to check...\n", N)
var wg sync.WaitGroup
wg.Add(N)
for _, listId := range trelloListsSplit {
go checkList(listId, &wg)
}
archivalCheckWg.Wait()
fmt.Println("Done")
wg.Wait()
log.Println("Done")

}

0 comments on commit e964c62

Please sign in to comment.