gime-lib/cmd/gime/main.go

188 lines
4.1 KiB
Go
Raw Normal View History

2017-07-27 15:27:10 +00:00
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
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")
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-28 11:44:08 +00:00
func printStatus() int {
2017-07-27 15:27:10 +00:00
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())
}
2017-07-28 11:44:08 +00:00
return 0
2017-07-27 15:27:10 +00:00
}
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()
curr := gdb.LoadTimeEntryCollection(gime.TypeCurrent)
if curr.Length() > 0 {
// TODO: Stop all Timers in curr
}
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)
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
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]
}