package main import ( "fmt" "strconv" "time" "git.bullercodeworks.com/brian/gime" ) // switchTimer performs a stop on any currently running timers // and starts a new timer with the given arguments func cmdSwitchTimer(args []string) int { loadActiveTimeEntries() tm := time.Now() if timeEntries.Length() > 0 { fmt.Println("Stopped Timers:") } for i := 0; i < timeEntries.Length(); i++ { tmr := timeEntries.Get(i) 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) } // 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 var tm time.Time tagStart := 0 if len(args) > 0 { // Check if the first argument looks like a date/time tm, err = parseFuzzyTime(args[0]) } if len(args) == 0 || err != nil { // 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 } fmt.Println(" " + TimerToString(entry)) return 0 } // cmdStopTimer takes parameters that describe which times to stop func cmdStopTimer(args []string) int { // args[0] should either be a timer id (starting with '@') or 'all' var err error tm := time.Now() actTimers := gdb.LoadTimeEntryCollection(gime.TypeCurrent) var tmr *gime.TimeEntry stopAll := len(args) > 0 && args[0] == "all" if !stopAll { if actTimers.Length() != 1 && (len(args) < 1 || args[0][0] != '@') { fmt.Println("Couldn't determine which timer(s) to stop") return 1 } if actTimers.Length() == 1 { // only one timer running tmr = actTimers.Get(0) } else { // 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, _, err = findTimerById(timerId) if err != nil { fmt.Println(err.Error()) return 1 } } } if len(args) > 1 { // Check if the next argument looks like a date/time tm, err = parseFuzzyTime(args[1]) if err != nil { fmt.Println(err.Error()) return 1 } } 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 } fmt.Println("Stopped:", InferTimerDetailString(tmr)) return 0 } if stopAll { 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 } return stopTimer(tmr, tm) } // 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 { fmt.Println("Error removing entry " + gime.TypeToString(tp) + "." + tmr.GetUUID()) return 1 } fmt.Println("Deleted Time Entry: " + TimerToString(tmr)) return 0 }