2017-07-27 15:27:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-07-31 12:00:16 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2017-07-27 15:27:10 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.bullercodeworks.com/brian/gime"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
AppName = "gime"
|
|
|
|
AppVersion = 1
|
|
|
|
)
|
|
|
|
|
|
|
|
var validOperations map[string][]string
|
2017-07-31 12:00:16 +00:00
|
|
|
var activeTimeEntries *gime.TimeEntryCollection
|
2017-07-28 11:44:08 +00:00
|
|
|
var gdb *gime.GimeDB
|
2017-07-27 15:27:10 +00:00
|
|
|
|
|
|
|
func main() {
|
2017-07-28 11:44:08 +00:00
|
|
|
var ret int
|
2017-07-27 15:27:10 +00:00
|
|
|
initialize()
|
|
|
|
var parms []string
|
2017-07-28 11:44:08 +00:00
|
|
|
|
2017-07-27 15:27:10 +00:00
|
|
|
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":
|
2017-07-28 11:44:08 +00:00
|
|
|
ret = printHelp()
|
2017-07-27 15:27:10 +00:00
|
|
|
case "status":
|
2017-07-28 11:44:08 +00:00
|
|
|
ret = printStatus()
|
2017-07-27 15:27:10 +00:00
|
|
|
case "start":
|
2017-07-28 11:44:08 +00:00
|
|
|
ret = startTimer(parms[1:])
|
2017-07-27 15:27:10 +00:00
|
|
|
case "end", "stop":
|
2017-07-28 11:44:08 +00:00
|
|
|
ret = stopTimer(parms[1:])
|
|
|
|
default:
|
|
|
|
fmt.Println("Unknown command")
|
|
|
|
ret = 1
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
2017-07-28 11:44:08 +00:00
|
|
|
os.Exit(ret)
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 11:44:08 +00:00
|
|
|
func printHelp() int {
|
2017-07-27 15:27:10 +00:00
|
|
|
fmt.Println("gime - A simple timekeeping application\n")
|
2017-07-31 12:00:16 +00:00
|
|
|
fmt.Println("Usage: gime [@timerID] [operation] [tags...]")
|
2017-07-27 15:27:10 +00:00
|
|
|
for _, v := range validOperations {
|
|
|
|
for vi := range v {
|
|
|
|
fmt.Println(" ", v[vi])
|
|
|
|
}
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
2017-07-28 11:44:08 +00:00
|
|
|
return 0
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 12:00:16 +00:00
|
|
|
func loadActiveTimeEntries() {
|
|
|
|
activeTimeEntries = gdb.LoadTimeEntryCollection(gime.TypeCurrent)
|
|
|
|
}
|
|
|
|
|
2017-07-28 11:44:08 +00:00
|
|
|
func printStatus() int {
|
2017-07-31 12:00:16 +00:00
|
|
|
loadActiveTimeEntries()
|
|
|
|
curr := time.Now()
|
|
|
|
fmt.Println("Current Time:", curr.Format(time.Stamp))
|
|
|
|
if activeTimeEntries.Length() == 0 {
|
2017-07-27 15:27:10 +00:00
|
|
|
fmt.Println("No timer running")
|
|
|
|
} else {
|
2017-07-31 12:00:16 +00:00
|
|
|
fmt.Print("Currently Active Timers\n")
|
|
|
|
for i := 0; i < activeTimeEntries.Length(); i++ {
|
|
|
|
v := activeTimeEntries.Get(i)
|
|
|
|
if v.GetStart().Day() == curr.Day() {
|
|
|
|
fmt.Print("@"+strconv.Itoa(i)+" ", v.GetStart().Format("15:04"), " ")
|
|
|
|
} else {
|
|
|
|
fmt.Print("@"+strconv.Itoa(i)+" ", v.GetStart().Format(time.Stamp), " ")
|
|
|
|
}
|
|
|
|
since := time.Since(v.GetStart()).String()
|
|
|
|
since = strings.Split(since, ".")[0]
|
|
|
|
fmt.Print("(" + since + "s) ")
|
|
|
|
fmt.Print(v.GetTags().Length())
|
|
|
|
if v.GetTags().Length() > 0 {
|
|
|
|
fmt.Print(" [ ")
|
|
|
|
for j := 0; j < v.GetTags().Length(); j++ {
|
|
|
|
fmt.Print(v.GetTags().Get(j))
|
|
|
|
if j < v.GetTags().Length()-1 {
|
|
|
|
fmt.Print(", ")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Print(" ] ")
|
|
|
|
}
|
|
|
|
fmt.Println("")
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-28 11:44:08 +00:00
|
|
|
return 0
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 12:00:16 +00:00
|
|
|
// switchTimer performs a stop on any currently running timers
|
|
|
|
// and starts a new timer with the given arguments
|
|
|
|
func switchTimer(args []string) int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2017-07-28 11:44:08 +00:00
|
|
|
// startTimer takes a list of arguments and returns the return code
|
|
|
|
// to be passed along to os.Exit
|
|
|
|
func startTimer(args []string) int {
|
2017-07-27 15:27:10 +00:00
|
|
|
var err error
|
2017-07-28 11:44:08 +00:00
|
|
|
tm := time.Now()
|
|
|
|
tagStart := 0
|
2017-07-27 15:27:10 +00:00
|
|
|
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
|
2017-07-28 11:44:08 +00:00
|
|
|
tm = time.Now()
|
|
|
|
} else {
|
|
|
|
tagStart = 1
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
2017-07-28 11:44:08 +00:00
|
|
|
var entry *gime.TimeEntry
|
|
|
|
|
|
|
|
var timerArgs []string
|
|
|
|
if tagStart < len(args) {
|
|
|
|
timerArgs = args[tagStart:]
|
|
|
|
}
|
|
|
|
fmt.Println(tm)
|
2017-07-31 12:00:16 +00:00
|
|
|
tc := new(gime.TagCollection)
|
|
|
|
for i := range timerArgs {
|
|
|
|
tc.Push(timerArgs[i])
|
|
|
|
}
|
|
|
|
if entry, err = gime.CreateTimeEntry(tm, time.Time{}, tc); err != nil {
|
2017-07-28 11:44:08 +00:00
|
|
|
fmt.Println(err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if err = gdb.SaveTimeEntry(entry); err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
return 0
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
|
|
|
|
2017-07-28 11:44:08 +00:00
|
|
|
func stopTimer(args []string) int {
|
2017-07-27 15:27:10 +00:00
|
|
|
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
|
2017-07-28 11:44:08 +00:00
|
|
|
return 0
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func initialize() {
|
2017-07-28 11:44:08 +00:00
|
|
|
var err error
|
|
|
|
if gdb, err = gime.LoadDatabase("./", "gime.db", "gimearch.db"); err != nil {
|
|
|
|
fmt.Println("Error loading the database")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-07-27 15:27:10 +00:00
|
|
|
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]
|
|
|
|
}
|