From cc75725d66c85e5a586aa945908d8c39f7ccd7eb Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Wed, 1 May 2019 08:09:17 -0500 Subject: [PATCH] Archive by context/project --- timer_ops.go | 65 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/timer_ops.go b/timer_ops.go index 87028d3..1d76ccd 100644 --- a/timer_ops.go +++ b/timer_ops.go @@ -239,19 +239,60 @@ func (a *AppState) opArchiveTimer(args []string) int { var id int var timer *timertxt.Timer var err error - if id, err = strconv.Atoi(v); err != nil { - fmt.Printf("Invalid id given: %s\n", v) - return 1 + if strings.HasPrefix(v, "@") { + // We're archiving by context + v = strings.TrimPrefix(v, "@") + fmt.Println("Archiving by context:", v) + timers := a.TimerList.GetTimersWithContext(v) + fmt.Println("Found timers: ", len(*timers)) + for _, tmr := range *timers { + if tmr.Finished { + if err = a.archiveTimer(tmr.Id); err != nil { + fmt.Printf("Error archiving timer %d\n", tmr.Id) + continue + } + fmt.Println(a.getDoneTimerString(tmr)) + } else { + fmt.Println("Refusing to archive running timer:", tmr.Id) + } + } + } else if strings.HasPrefix(v, "+") { + // We're archiving by projcet + v = strings.TrimPrefix(v, "+") + fmt.Println("Archiving by project:", v) + timers := a.TimerList.GetTimersWithProject(v) + fmt.Println("Found timers: ", len(*timers)) + for _, tmr := range *timers { + if tmr.Finished { + if err = a.archiveTimer(tmr.Id); err != nil { + fmt.Printf("Error archiving timer %d\n", tmr.Id) + continue + } + fmt.Println(a.getDoneTimerString(tmr)) + } else { + fmt.Println("Refusing to archive running timer:", tmr.Id) + } + } + } else { + // I guess we're archiving by timer id + if id, err = strconv.Atoi(v); err != nil { + fmt.Printf("Invalid id given: %s\n", v) + return 1 + } + if timer, err = a.TimerList.GetTimer(id); err != nil { + fmt.Printf("Error getting timer %d\n", id) + return 1 + } + if timer.Finished { + if err = a.archiveTimer(id); err != nil { + fmt.Printf("Error archiving timer %d\n", id) + return 1 + } + fmt.Println(a.getDoneTimerString(*timer)) + } else { + fmt.Println("Refusing to archive running timer:", timer.Id) + } } - if timer, err = a.TimerList.GetTimer(id); err != nil { - fmt.Printf("Error getting timer %d\n", id) - return 1 - } - if err = a.archiveTimer(id); err != nil { - fmt.Printf("Error archiving timer %d\n", id) - return 1 - } - fmt.Println(a.getDoneTimerString(*timer)) } } else { for _, v := range *a.TimerList {