gime-lib/cmd/gime/main.go

148 lines
3.2 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
func main() {
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":
printHelp()
case "status":
printStatus()
case "start":
startTimer(parms[1:])
case "end", "stop":
stopTimer(parms[1:])
}
}
func printHelp() {
fmt.Println("gime - A simple timekeeping application\n")
for _, v := range validOperations {
for vi := range v {
fmt.Println(" ", v[vi])
}
fmt.Println("")
}
}
func printStatus() {
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())
}
}
func startTimer(args []string) {
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])
}
}
if err != nil {
// Just start it now
}
_, _ = tm, st
}
func stopTimer(args []string) {
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
}
func initialize() {
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]
}