2017-07-27 15:27:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2017-07-31 12:00:16 +00:00
|
|
|
"strings"
|
2017-07-27 15:27:10 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.bullercodeworks.com/brian/gime"
|
2017-08-01 20:39:24 +00:00
|
|
|
userConfig "github.com/br0xen/user-config"
|
2017-07-27 15:27:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2017-08-01 20:39:24 +00:00
|
|
|
AppName = "gime"
|
|
|
|
AppVersion = 1
|
|
|
|
DefDBName = "gime.db"
|
|
|
|
DefArchDBName = "gimearch.db"
|
2017-07-27 15:27:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var validOperations map[string][]string
|
2018-01-11 16:52:44 +00:00
|
|
|
var opFuncs map[string]func([]string) int
|
|
|
|
var timeEntries *gime.TimeEntryCollection
|
2017-07-28 11:44:08 +00:00
|
|
|
var gdb *gime.GimeDB
|
2017-08-01 20:39:24 +00:00
|
|
|
var cfg *userConfig.Config
|
2017-07-27 15:27:10 +00:00
|
|
|
|
2018-01-11 16:52:44 +00:00
|
|
|
var fuzzyFormats []string
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2018-01-11 16:52:44 +00:00
|
|
|
if fn, ok := opFuncs[parms[0]]; ok {
|
|
|
|
ret = fn(parms[1:])
|
|
|
|
} else {
|
2017-07-28 11:44:08 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-11 16:52:44 +00:00
|
|
|
func cmdDoArchive(args []string) int {
|
|
|
|
fmt.Println("Not implemented yet.")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
2017-08-01 20:39:24 +00:00
|
|
|
func cmdDoConfig(args []string) int {
|
|
|
|
if len(args) == 0 {
|
|
|
|
fmt.Println("Invalid configuration options passed")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range args {
|
|
|
|
if strings.Index(opt, "=") == -1 {
|
|
|
|
// Single word triggers
|
|
|
|
switch opt {
|
|
|
|
case "reset":
|
|
|
|
fmt.Println("Resetting Configuration...")
|
|
|
|
cfg.Set("dbdir", cfg.GetConfigPath()+"/")
|
|
|
|
cfg.Set("dbname", DefDBName)
|
|
|
|
cfg.Set("dbarchname", DefArchDBName)
|
|
|
|
case "list":
|
|
|
|
fmt.Println("Current " + AppName + " config")
|
|
|
|
for _, v := range cfg.GetKeyList() {
|
|
|
|
fmt.Println(" " + v + ": " + cfg.Get(v))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Key=Value options
|
|
|
|
pts := strings.Split(opt, "=")
|
|
|
|
if len(pts) == 2 {
|
|
|
|
switch pts[0] {
|
|
|
|
case "dbdir":
|
|
|
|
val := pts[1]
|
|
|
|
if val[len(val)-1] != '/' {
|
|
|
|
val = val + "/"
|
|
|
|
}
|
|
|
|
cfg.Set("dbdir", val)
|
|
|
|
case "dbname":
|
|
|
|
cfg.Set("dbname", pts[1])
|
|
|
|
case "dbarchname":
|
|
|
|
cfg.Set("dbarchname", pts[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-01-11 16:52:44 +00:00
|
|
|
func cmdPrintDetail(args []string) int {
|
|
|
|
fmt.Println("Not implemented yet.")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdPrintHelp(args []string) int {
|
|
|
|
if len(args) == 0 {
|
|
|
|
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("")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch args[0] {
|
|
|
|
case "formats":
|
|
|
|
fmt.Println("Supported date/time formats:")
|
|
|
|
for i := range fuzzyFormats {
|
|
|
|
fmt.Println(" ", fuzzyFormats[i])
|
|
|
|
}
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
|
|
|
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() {
|
2018-01-11 16:52:44 +00:00
|
|
|
timeEntries = gdb.LoadTimeEntryCollection(gime.TypeCurrent)
|
2017-07-31 12:00:16 +00:00
|
|
|
}
|
|
|
|
|
2018-01-11 16:52:44 +00:00
|
|
|
func loadActiveAndRecentTimeEntries() {
|
|
|
|
timeEntries = gdb.LoadTimeEntryCollection(gime.TypeNoArchive)
|
|
|
|
}
|
2017-08-01 20:39:24 +00:00
|
|
|
|
2018-01-11 16:52:44 +00:00
|
|
|
func cmdPrintList(args []string) int {
|
|
|
|
loadActiveAndRecentTimeEntries()
|
|
|
|
// By default, list all entries ending today or still running
|
|
|
|
filter := func(t *gime.TimeEntry) bool {
|
|
|
|
return t.EndsToday() || t.IsRunning()
|
2017-08-02 19:14:53 +00:00
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
fmt.Println(TimerCollectionToString(filterTimerCollection(timeEntries, filter)))
|
2017-08-01 20:39:24 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2018-01-11 16:52:44 +00:00
|
|
|
func cmdPrintStatus(args []string) int {
|
2017-07-31 12:00:16 +00:00
|
|
|
loadActiveTimeEntries()
|
|
|
|
curr := time.Now()
|
|
|
|
fmt.Println("Current Time:", curr.Format(time.Stamp))
|
2018-01-11 16:52:44 +00:00
|
|
|
if timeEntries.Length() == 0 {
|
2017-07-27 15:27:10 +00:00
|
|
|
fmt.Println("No timer running")
|
|
|
|
} else {
|
2018-01-11 16:52:44 +00:00
|
|
|
fmt.Print("Active Timers (", timeEntries.Length(), ")\n")
|
|
|
|
// Find the longest start time & longest duration
|
|
|
|
short := true
|
|
|
|
for i := 0; i < timeEntries.Length(); i++ {
|
|
|
|
v := timeEntries.Get(i)
|
|
|
|
if v.GetStart().Day() != curr.Day() {
|
|
|
|
short = false
|
|
|
|
break
|
2017-07-31 12:00:16 +00:00
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < timeEntries.Length(); i++ {
|
|
|
|
v := timeEntries.Get(i)
|
|
|
|
if short {
|
|
|
|
fmt.Printf(" @%d %s\n", i, TimerDetailToString(v))
|
|
|
|
} else {
|
|
|
|
fmt.Printf(" @%d %s\n", i, TimerDetailToLongString(v))
|
2017-07-31 12:00:16 +00:00
|
|
|
}
|
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-08-01 20:39:24 +00:00
|
|
|
func initialize() {
|
2017-07-27 15:27:10 +00:00
|
|
|
var err error
|
2017-08-01 20:39:24 +00:00
|
|
|
validOperations = make(map[string][]string)
|
2018-01-11 16:52:44 +00:00
|
|
|
opFuncs = make(map[string]func([]string) int)
|
|
|
|
|
|
|
|
opFuncs["config"] = cmdDoConfig
|
2017-08-01 20:39:24 +00:00
|
|
|
validOperations["config"] = []string{
|
|
|
|
"config [command] - Perform configuration",
|
|
|
|
" list - List current configuration",
|
|
|
|
" reset - Reset current configuration",
|
|
|
|
" Configuration Options:",
|
|
|
|
" dbdir=[database directory]",
|
|
|
|
" dbname=[database filename]",
|
|
|
|
" dbarchname=[archive database filename]",
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
|
|
|
|
opFuncs["detail"] = cmdPrintDetail
|
2017-08-01 20:39:24 +00:00
|
|
|
validOperations["detail"] = []string{
|
|
|
|
"detail @id - Print details about a timer",
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
|
|
|
|
opFuncs["delete"] = cmdDeleteTimer
|
2017-08-01 20:39:24 +00:00
|
|
|
validOperations["delete"] = []string{
|
|
|
|
"delete @id - Delete a timer",
|
2017-07-28 11:44:08 +00:00
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
|
|
|
|
opFuncs["end"] = cmdStopTimer
|
2017-08-01 20:39:24 +00:00
|
|
|
validOperations["end"] = []string{
|
|
|
|
"end - The same as stop",
|
2017-07-28 11:44:08 +00:00
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
|
|
|
|
opFuncs["help"] = cmdPrintHelp
|
2017-08-01 20:39:24 +00:00
|
|
|
validOperations["help"] = []string{
|
|
|
|
"help - Print this",
|
2017-07-28 11:44:08 +00:00
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
|
|
|
|
opFuncs["list"] = cmdPrintList
|
2017-08-01 20:39:24 +00:00
|
|
|
validOperations["list"] = []string{
|
|
|
|
"list [duration] [+tags] - List time entries",
|
|
|
|
" valid values of [duration] include:",
|
|
|
|
" day - List all entries for the current day",
|
|
|
|
" week - List all entries for the current week",
|
|
|
|
" month - List all entries for the current month",
|
|
|
|
" year - List all entries for the current year",
|
|
|
|
" To list entries by tag, preceed the tags with a +",
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
|
|
|
|
opFuncs["ls"] = cmdPrintList
|
2017-08-01 20:39:24 +00:00
|
|
|
validOperations["ls"] = []string{
|
|
|
|
"ls [duration] [+tags] - The same as list",
|
2017-07-28 11:44:08 +00:00
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
|
|
|
|
opFuncs["remove"] = cmdDeleteTimer
|
|
|
|
validOperations["remove"] = []string{
|
|
|
|
"remove @id - See 'delete'",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["status"] = cmdPrintStatus
|
2017-07-27 15:27:10 +00:00
|
|
|
validOperations["status"] = []string{
|
2017-08-01 20:39:24 +00:00
|
|
|
"status - Print the status of all active timers",
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
|
|
|
|
opFuncs["start"] = cmdStartTimer
|
2017-07-27 15:27:10 +00:00
|
|
|
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",
|
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
|
|
|
|
opFuncs["stop"] = cmdStopTimer
|
2017-07-27 15:27:10 +00:00
|
|
|
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).",
|
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
|
|
|
|
opFuncs["switch"] = cmdSwitchTimer
|
2017-08-02 19:14:53 +00:00
|
|
|
validOperations["switch"] = []string{
|
|
|
|
"switch [tags ...] - Stop all currently running timers and start a new",
|
|
|
|
" one with the given tags",
|
|
|
|
}
|
2017-08-01 20:39:24 +00:00
|
|
|
|
2018-01-11 16:52:44 +00:00
|
|
|
opFuncs["archive"] = cmdDoArchive
|
|
|
|
validOperations["archive"] = []string{
|
|
|
|
"archive - Archive all entries older than the archive date",
|
|
|
|
}
|
|
|
|
|
2017-08-01 20:39:24 +00:00
|
|
|
// Load the Config
|
|
|
|
cfg, err = userConfig.NewConfig(AppName)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
fmt.Println("Creating new config")
|
|
|
|
cfg.Save()
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
2017-08-01 20:39:24 +00:00
|
|
|
// If dbdir isn't set, set it to the config directory
|
|
|
|
if cfg.Get("dbdir") == "" {
|
|
|
|
cfg.Set("dbdir", cfg.GetConfigPath()+"/")
|
|
|
|
}
|
|
|
|
// If dbname isn't set, set it to the default database filename
|
|
|
|
if cfg.Get("dbname") == "" {
|
|
|
|
cfg.Set("dbname", DefDBName)
|
|
|
|
}
|
|
|
|
// If dbarchivename isn't set, set it to the default archive database filename
|
|
|
|
if cfg.Get("dbarchname") == "" {
|
|
|
|
cfg.Set("dbarchname", DefArchDBName)
|
|
|
|
}
|
|
|
|
if gdb, err = gime.LoadDatabase(cfg.Get("dbdir"), cfg.Get("dbname"), cfg.Get("dbarchname")); err != nil {
|
|
|
|
fmt.Println("Error loading the database")
|
|
|
|
os.Exit(1)
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
2018-01-11 16:52:44 +00:00
|
|
|
|
|
|
|
fuzzyFormats = []string{
|
|
|
|
"15:04", // Kitchen, 24hr
|
|
|
|
time.Kitchen,
|
|
|
|
time.RFC3339,
|
|
|
|
"2006-01-02T15:04:05", // RFC3339 without timezone
|
|
|
|
"2006-01-02T15:04", // RFC3339 without seconds or timezone
|
|
|
|
time.Stamp,
|
|
|
|
"02 Jan 06 15:04:05", // RFC822 with second
|
|
|
|
time.RFC822,
|
|
|
|
"01/02/2006 15:04", // U.S. Format
|
|
|
|
"01/02/2006 15:04:05", // U.S. Format with seconds
|
|
|
|
"01/02/06 15:04", // U.S. Format, short year
|
|
|
|
"01/02/06 15:04:05", // U.S. Format, short year, with seconds
|
|
|
|
}
|
2017-07-27 15:27:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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]
|
|
|
|
}
|