gime-lib/cmd/gime/timer_operations.go

170 lines
3.9 KiB
Go
Raw Normal View History

2017-08-01 20:39:24 +00:00
package main
import (
"fmt"
"strconv"
"time"
"git.bullercodeworks.com/brian/gime"
)
2018-01-18 15:54:12 +00:00
func cmdContinueTimer(args []string) int {
// Get the last running timer and start a new one with the same tags
te, err := getMostRecentTimeEntry()
if err != nil {
fmt.Println(err.Error())
return 1
}
tagColl := te.GetTags()
var tags []string
for i := 0; i < tagColl.Length(); i++ {
tags = append(tags, tagColl.Get(i))
}
args = append(args, tags...)
return cmdStartTimer(args)
}
2017-08-01 20:39:24 +00:00
// switchTimer performs a stop on any currently running timers
// and starts a new timer with the given arguments
2017-08-02 19:14:53 +00:00
func cmdSwitchTimer(args []string) int {
2017-08-01 20:39:24 +00:00
loadActiveTimeEntries()
2017-08-02 19:14:53 +00:00
tm := time.Now()
2018-01-11 16:52:44 +00:00
if timeEntries.Length() > 0 {
2017-08-02 19:14:53 +00:00
fmt.Println("Stopped Timers:")
}
2018-01-11 16:52:44 +00:00
for i := 0; i < timeEntries.Length(); i++ {
tmr := timeEntries.Get(i)
2017-08-02 19:14:53 +00:00
tmr.SetEnd(tm)
if err := gdb.UpdateTimeEntry(tmr); err != nil {
fmt.Println(err.Error())
return 1
}
fmt.Println(" " + TimerToString(tmr))
}
fmt.Println("Started Timer:")
return cmdStartTimer(args)
2017-08-01 20:39:24 +00:00
}
// cmdStartTimer takes a list of arguments and returns the return code
// to be passed along to os.Exit
func cmdStartTimer(args []string) int {
var err error
2018-01-11 16:52:44 +00:00
var tm time.Time
2017-08-01 20:39:24 +00:00
tagStart := 0
if len(args) > 0 {
// Check if the first argument looks like a date/time
2018-01-11 16:52:44 +00:00
tm, err = parseFuzzyTime(args[0])
2017-08-01 20:39:24 +00:00
}
2018-01-11 16:52:44 +00:00
if len(args) == 0 || err != nil {
2017-08-01 20:39:24 +00:00
// Just start it now
tm = time.Now()
} else {
tagStart = 1
}
var entry *gime.TimeEntry
var timerArgs []string
if tagStart < len(args) {
timerArgs = args[tagStart:]
}
tc := new(gime.TagCollection)
for i := range timerArgs {
tc.Push(timerArgs[i])
}
if entry, err = gime.CreateTimeEntry(tm, time.Time{}, tc); err != nil {
fmt.Println(err)
return 1
}
if err = gdb.SaveTimeEntry(entry); err != nil {
fmt.Println(err)
return 1
}
2017-08-02 19:14:53 +00:00
fmt.Println(" " + TimerToString(entry))
2017-08-01 20:39:24 +00:00
return 0
}
// cmdStopTimer takes parameters that describe which times to stop
func cmdStopTimer(args []string) int {
2018-01-11 16:52:44 +00:00
// args[0] should either be a timer id (starting with '@') or 'all'
2017-08-01 20:39:24 +00:00
var err error
tm := time.Now()
actTimers := gdb.LoadTimeEntryCollection(gime.TypeCurrent)
var tmr *gime.TimeEntry
2018-01-18 15:54:12 +00:00
stopId := "@0" // By default, stop the first timer
for i := range args {
if args[i] == "all" || args[i][0] == '@' {
stopId = args[i]
continue
2017-08-01 20:39:24 +00:00
}
2018-01-18 15:54:12 +00:00
tmpTm, err := parseFuzzyTime(args[i])
if err == nil {
// We found a time
tm = tmpTm
continue
2018-01-11 16:52:44 +00:00
}
}
2018-01-18 15:54:12 +00:00
if stopId != "all" {
// Find the timer that we're stopping
timerId, err := strconv.Atoi(stopId[1:])
if err != nil {
fmt.Println("Error parsing timer id:", err.Error())
return 1
}
tmr, _, err = findTimerById(timerId)
2017-08-01 20:39:24 +00:00
if err != nil {
fmt.Println(err.Error())
return 1
}
}
2018-01-11 16:52:44 +00:00
stopTimer := func(tmr *gime.TimeEntry, at time.Time) int {
tmr.SetEnd(at)
if err = gdb.UpdateTimeEntry(tmr); err != nil {
fmt.Println(err.Error())
return 1
2017-08-01 20:39:24 +00:00
}
2018-01-11 16:52:44 +00:00
fmt.Println("Stopped:", InferTimerDetailString(tmr))
return 0
2017-08-01 20:39:24 +00:00
}
2018-01-18 15:54:12 +00:00
if stopId == "all" {
2018-01-11 16:52:44 +00:00
var ret int
for i := 0; i < actTimers.Length(); i++ {
ret += stopTimer(actTimers.Get(i), tm)
}
if ret > 0 {
return 1 // One or more stop operations failed
}
return 0
2017-08-01 20:39:24 +00:00
}
2018-01-18 15:54:12 +00:00
// Just stop the one timer
2018-01-11 16:52:44 +00:00
return stopTimer(tmr, tm)
2017-08-01 20:39:24 +00:00
}
// cmdDeleteTimer takes parameters that describe the timers to be deleted.
func cmdDeleteTimer(args []string) int {
var err error
if len(args) < 1 || args[0][0] != '@' {
fmt.Println("Couldn't determine which timer(s) to delete")
return 1
}
// We've got a timer id to delete
timerId, err := strconv.Atoi(args[0][1:])
if err != nil {
fmt.Println("Error parsing timer id: " + err.Error())
return 1
}
tmr, tp, err := findTimerById(timerId)
if err != nil {
fmt.Println(err.Error())
return 1
}
if gdb.RemoveTimeEntry(tmr.GetUUID()) != nil {
2018-01-11 16:52:44 +00:00
fmt.Println("Error removing entry " + gime.TypeToString(tp) + "." + tmr.GetUUID())
2017-08-01 20:39:24 +00:00
return 1
}
fmt.Println("Deleted Time Entry: " + TimerToString(tmr))
return 0
}