219 lines
5.0 KiB
Go
219 lines
5.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.bullercodeworks.com/brian/gime"
|
|
)
|
|
|
|
const (
|
|
AppName = "gime"
|
|
AppVersion = 1
|
|
)
|
|
|
|
var validOperations map[string][]string
|
|
var activeTimeEntries *gime.TimeEntryCollection
|
|
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")
|
|
fmt.Println("Usage: gime [@timerID] [operation] [tags...]")
|
|
for _, v := range validOperations {
|
|
for vi := range v {
|
|
fmt.Println(" ", v[vi])
|
|
}
|
|
fmt.Println("")
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func loadActiveTimeEntries() {
|
|
activeTimeEntries = gdb.LoadTimeEntryCollection(gime.TypeCurrent)
|
|
}
|
|
|
|
func printStatus() int {
|
|
loadActiveTimeEntries()
|
|
curr := time.Now()
|
|
fmt.Println("Current Time:", curr.Format(time.Stamp))
|
|
if activeTimeEntries.Length() == 0 {
|
|
fmt.Println("No timer running")
|
|
} else {
|
|
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("")
|
|
}
|
|
}
|
|
return 0
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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()
|
|
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)
|
|
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
|
|
}
|
|
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]
|
|
}
|