package main import ( "fmt" "strings" "time" gime "git.bullercodeworks.com/brian/gime-lib" ) // 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 entry *gime.TimeEntry // By default we start the timer now tm := time.Now() tags, rem := pullTagsFromArgs(args) if len(rem) > 0 { // Check if the first argument looks like a date/time tm, err = parseFuzzyTime(rem[0]) } tc := new(gime.TagCollection) for i := range tags { tc.Push(tags[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("Started:", TimerToString(entry)) return 0 } func cmdAddTimer(args []string) int { var err error var entry *gime.TimeEntry // By default we start the timer now tags, rem := pullTagsFromArgs(args) var beg, end time.Time for _, opt := range rem { var tmpBeg, tmpEnd time.Time if strings.Contains(opt, "-") { pts := strings.Split(opt, "-") if len(pts[0]) > 0 { // This should be the starting date tmpBeg, err = parseFuzzyTime(pts[0]) if err != nil { continue } } if len(pts[1]) > 0 { // This should be the ending date tmpEnd, err = parseFuzzyTime(pts[1]) if err != nil { continue } } } if !tmpBeg.IsZero() || !tmpEnd.IsZero() { beg, end = tmpBeg, tmpEnd } } if end.IsZero() { end = time.Now() } tc := new(gime.TagCollection) for i := range tags { tc.Push(tags[i]) } if entry, err = gime.CreateTimeEntry(beg, end, tc); err != nil { fmt.Println(err) return 1 } if err = gdb.SaveTimeEntry(entry); err != nil { fmt.Println(err) return 1 } fmt.Println("Added Time Entry:", TimerToString(entry)) return 0 } 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) } // switchTimer performs a stop on any currently running timers // and starts a new timer with the given arguments func cmdSwitchTimer(args []string) int { stopId := "@all" stopIdIdx := -1 for i := range args { // see if we have a timer id in the args if args[i][0] == '@' { stopId = args[i] stopIdIdx = i } } if stopIdIdx >= 0 { args = append(args[:stopIdIdx], args[stopIdIdx+1:]...) } if cmdStopTimer([]string{stopId}) != 0 { // Error while stopping timers return 1 } return cmdStartTimer(args) }