package main import ( "fmt" "os" "time" "git.bullercodeworks.com/brian/gime" ) const ( AppName = "gime" AppVersion = 1 ) var validOperations map[string][]string var activeTimeEntry *gime.TimeEntry var gdb *gime.GimeDB func main() { var ret int initialize() var parms []string if len(os.Args) > 1 { parms = os.Args[1:] parms[0] = matchParameter(parms[0]) } else { // If no parameters were passed, just print the status parms = append(parms, "status") } switch parms[0] { case "help": ret = printHelp() case "status": ret = printStatus() case "start": ret = startTimer(parms[1:]) case "end", "stop": ret = stopTimer(parms[1:]) default: fmt.Println("Unknown command") ret = 1 } os.Exit(ret) } func printHelp() int { fmt.Println("gime - A simple timekeeping application\n") for _, v := range validOperations { for vi := range v { fmt.Println(" ", v[vi]) } fmt.Println("") } return 0 } func printStatus() int { if activeTimeEntry == nil { fmt.Println("No timer running") } else { fmt.Print("Timer started at ") curr := time.Now() if activeTimeEntry.GetStart().Day() == curr.Day() { fmt.Println(activeTimeEntry.GetStart().Format("15:04")) } else { fmt.Println(activeTimeEntry.GetStart().Format(time.Stamp)) } fmt.Println(time.Since(activeTimeEntry.GetStart()).String()) } return 0 } // startTimer takes a list of arguments and returns the return code // to be passed along to os.Exit func startTimer(args []string) int { var err error tm := time.Now() curr := gdb.LoadTimeEntryCollection(gime.TypeCurrent) if curr.Length() > 0 { // TODO: Stop all Timers in curr } 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:] } fmt.Println(tm) if entry, err = gime.CreateTimeEntry(tm, time.Time{}, timerArgs); err != nil { fmt.Println(err) return 1 } if err = gdb.SaveTimeEntry(entry); err != nil { fmt.Println(err) return 1 } return 0 } func stopTimer(args []string) int { var err error var tm time.Time st := time.Now() 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]) } } _, _ = tm, st return 0 } func initialize() { var err error if gdb, err = gime.LoadDatabase("./", "gime.db", "gimearch.db"); err != nil { fmt.Println("Error loading the database") os.Exit(1) } validOperations = make(map[string][]string) validOperations["status"] = []string{ "status - Print the current status of the timer", } validOperations["start"] = []string{ "start [time] [tags ...] - Start a timer with the given tags (space separated)", " If the first sub-argument given looks like a time,", " the timer will be started then (past or future).", " If a timer is already running it'll be stopped", } validOperations["stop"] = []string{ "stop [time] - Stops the current timer", " If the first sub-argument given looks like a time,", " the timer will be stopped then (past or future).", } validOperations["end"] = []string{ "end - The same as stop", } validOperations["help"] = []string{ "help - Print this", } } func matchParameter(in string) string { var chkParms []string for k := range validOperations { chkParms = append(chkParms, k) } var nextParms []string for i := range in { for _, p := range chkParms { if p[i] == in[i] { nextParms = append(nextParms, p) } } // If we get here and there is only one parameter left, return it chkParms = nextParms if len(nextParms) == 1 { break } // Otherwise, loop nextParms = []string{} } if len(chkParms) == 0 { return "" } return chkParms[0] }