143 lines
3.3 KiB
Go
143 lines
3.3 KiB
Go
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 activeTimeEntries.Length() > 0 {
|
|
fmt.Println("Stopped Timers:")
|
|
}
|
|
for i := 0; i < activeTimeEntries.Length(); i++ {
|
|
tmr := activeTimeEntries.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
|
|
tm := time.Now()
|
|
tagStart := 0
|
|
if len(args) > 0 {
|
|
// Check if the first argument looks like a date/time
|
|
tm, err = time.Parse("15:04", args[0])
|
|
if err != nil {
|
|
tm, err = time.Parse(time.Kitchen, args[0])
|
|
}
|
|
}
|
|
if 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 {
|
|
var err error
|
|
tm := time.Now()
|
|
actTimers := gdb.LoadTimeEntryCollection(gime.TypeCurrent)
|
|
if actTimers.Length() != 1 && (len(args) < 1 || args[0][0] != '@') {
|
|
fmt.Println("Couldn't determine which timer(s) to stop")
|
|
return 1
|
|
}
|
|
var tmr *gime.TimeEntry
|
|
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) > 0 {
|
|
// Check if the first argument looks like a date/time
|
|
tm, err = time.Parse("15:04", args[0])
|
|
if err != nil {
|
|
tm, err = time.Parse(time.Kitchen, args[0])
|
|
}
|
|
}
|
|
|
|
tmr.SetEnd(tm)
|
|
if err = gdb.UpdateTimeEntry(tmr); err != nil {
|
|
fmt.Println(err.Error())
|
|
return 1
|
|
}
|
|
fmt.Println(TimerToString(tmr))
|
|
return 0
|
|
}
|
|
|
|
// 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 " + tp + "." + tmr.GetUUID())
|
|
return 1
|
|
}
|
|
fmt.Println("Deleted Time Entry: " + TimerToString(tmr))
|
|
return 0
|
|
}
|