2018-02-15 13:45:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2018-05-10 14:44:53 +00:00
|
|
|
"bufio"
|
2018-02-15 13:45:53 +00:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"git.bullercodeworks.com/brian/gime-lib"
|
|
|
|
userConfig "github.com/br0xen/user-config"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
AppName = "gime"
|
2018-07-30 20:47:51 +00:00
|
|
|
AppVersion = 1.1
|
2018-02-15 13:45:53 +00:00
|
|
|
DefDBName = "gime.db"
|
|
|
|
DefArchDBName = "gimearch.db"
|
2018-05-10 14:44:53 +00:00
|
|
|
DefRoundTo = "1m0s"
|
2018-02-15 13:45:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var validOperations map[string][]string
|
|
|
|
var opFuncs map[string]func([]string) int
|
|
|
|
var timeEntries *gime.TimeEntryCollection
|
|
|
|
var gdb *gime.GimeDB
|
|
|
|
var cfg *userConfig.Config
|
2018-05-10 14:44:53 +00:00
|
|
|
var roundTo time.Duration
|
2018-02-15 13:45:53 +00:00
|
|
|
|
|
|
|
var fuzzyFormats []string
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var ret int
|
|
|
|
initialize()
|
|
|
|
var parms []string
|
|
|
|
|
|
|
|
if len(os.Args) > 1 {
|
|
|
|
parms = os.Args[1:]
|
|
|
|
} else {
|
|
|
|
// If no parameters were passed, just print the status
|
|
|
|
parms = append(parms, "status")
|
|
|
|
}
|
|
|
|
|
|
|
|
if fn, ok := opFuncs[parms[0]]; ok {
|
|
|
|
ret = fn(parms[1:])
|
|
|
|
} else {
|
|
|
|
fmt.Println("Unknown command")
|
|
|
|
ret = 1
|
|
|
|
}
|
|
|
|
os.Exit(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadActiveTimeEntries() {
|
|
|
|
timeEntries = gdb.LoadTimeEntryCollection(gime.TypeCurrent)
|
|
|
|
}
|
|
|
|
|
|
|
|
func loadActiveAndRecentTimeEntries() {
|
|
|
|
timeEntries = gdb.LoadTimeEntryCollection(gime.TypeNoArchive)
|
|
|
|
}
|
|
|
|
|
2018-05-10 14:44:53 +00:00
|
|
|
func loadArchiveTimeEntries() {
|
|
|
|
timeEntries = gdb.LoadTimeEntryCollection(gime.TypeArchive)
|
|
|
|
}
|
|
|
|
|
2018-02-15 13:45:53 +00:00
|
|
|
func getMostRecentTimeEntry() (*gime.TimeEntry, error) {
|
|
|
|
return gdb.GetLatestTimeEntry()
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdDoConfig(args []string) int {
|
|
|
|
if len(args) == 0 {
|
|
|
|
fmt.Println("Invalid configuration options passed")
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range args {
|
|
|
|
if !strings.Contains(opt, "=") {
|
|
|
|
// Single word triggers
|
|
|
|
switch opt {
|
|
|
|
case "reset":
|
2018-05-10 14:44:53 +00:00
|
|
|
fmt.Println("Are you sure you want to reset your configuration? (Y/[N])")
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
|
|
conf, _ := reader.ReadString('\n')
|
|
|
|
conf = strings.TrimSpace(conf)
|
|
|
|
if conf == "Y" {
|
|
|
|
fmt.Println("Resetting Configuration...")
|
2018-07-30 20:46:44 +00:00
|
|
|
cfg.Set("dbdir", cfg.GetConfigPath()+string(os.PathSeparator))
|
2018-05-10 14:44:53 +00:00
|
|
|
cfg.Set("dbname", DefDBName)
|
|
|
|
cfg.Set("dbarchname", DefArchDBName)
|
|
|
|
cfg.Set("roundto", DefRoundTo)
|
|
|
|
} else {
|
|
|
|
fmt.Println("Done.")
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
|
2018-02-15 13:45:53 +00:00
|
|
|
case "list":
|
|
|
|
fmt.Println("Current " + AppName + " config")
|
|
|
|
for _, v := range cfg.GetKeyList() {
|
|
|
|
fmt.Println(" " + v + ": " + cfg.Get(v))
|
|
|
|
}
|
|
|
|
case "dbpath":
|
|
|
|
fmt.Println(cfg.Get("dbdir") + cfg.Get("dbname"))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Key=Value options
|
|
|
|
pts := strings.Split(opt, "=")
|
|
|
|
if len(pts) == 2 {
|
|
|
|
switch pts[0] {
|
|
|
|
case "dbdir":
|
|
|
|
val := pts[1]
|
2018-07-30 20:46:44 +00:00
|
|
|
if val[len(val)-1] != os.PathSeparator {
|
|
|
|
val = val + string(os.PathSeparator)
|
2018-02-15 13:45:53 +00:00
|
|
|
}
|
|
|
|
cfg.Set("dbdir", val)
|
|
|
|
case "dbname":
|
|
|
|
cfg.Set("dbname", pts[1])
|
|
|
|
case "dbarchname":
|
|
|
|
cfg.Set("dbarchname", pts[1])
|
2018-05-10 14:44:53 +00:00
|
|
|
case "roundto":
|
|
|
|
// Make sure that we can parse it
|
|
|
|
durStr := pts[1]
|
|
|
|
_, err := time.ParseDuration(durStr)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Unable to parse duration:", durStr)
|
|
|
|
durStr = DefRoundTo
|
|
|
|
}
|
|
|
|
cfg.Set("roundto", durStr)
|
|
|
|
fmt.Println("Rounding set to", durStr)
|
2018-02-15 13:45:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
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])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdListTags(args []string) int {
|
|
|
|
loadActiveAndRecentTimeEntries()
|
|
|
|
var allTags []string
|
|
|
|
for i := 0; i < timeEntries.Length(); i++ {
|
|
|
|
tc := timeEntries.Get(i).GetTags()
|
|
|
|
for j := 0; j < tc.Length(); j++ {
|
|
|
|
tg := tc.Get(j)
|
|
|
|
var found bool
|
|
|
|
for tst := range allTags {
|
|
|
|
if allTags[tst] == tg {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
allTags = append(allTags, tg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sort.StringSlice(allTags))
|
|
|
|
for i := range allTags {
|
|
|
|
fmt.Println(allTags[i])
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdPrintStatus(args []string) int {
|
|
|
|
loadActiveTimeEntries()
|
|
|
|
curr := time.Now()
|
|
|
|
fmt.Println("Current Time:", curr.Format(time.Stamp))
|
|
|
|
if timeEntries.Length() == 0 {
|
|
|
|
fmt.Println("No timer running")
|
|
|
|
} else {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func cmdDoFuzzyParse(args []string) int {
|
|
|
|
if len(args) == 0 {
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
var t time.Time
|
|
|
|
var err error
|
|
|
|
if t, err = parseFuzzyTime(args[0]); err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
fmt.Println(t.Format(time.RFC3339))
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func initialize() {
|
|
|
|
var err error
|
|
|
|
validOperations = make(map[string][]string)
|
|
|
|
opFuncs = make(map[string]func([]string) int)
|
|
|
|
|
2018-04-16 21:27:52 +00:00
|
|
|
opFuncs["add"] = cmdAddTimer
|
|
|
|
validOperations["add"] = []string{
|
|
|
|
"add [duration] [+tags] - Add a timer for the given duration",
|
|
|
|
" with the given tags",
|
|
|
|
}
|
|
|
|
|
2018-02-15 13:45:53 +00:00
|
|
|
opFuncs["cont"] = cmdContinueTimer
|
|
|
|
validOperations["cont"] = []string{
|
|
|
|
"cont [time] - Continue the last stopped timer",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["config"] = cmdDoConfig
|
|
|
|
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]",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["detail"] = cmdPrintDetail
|
|
|
|
validOperations["detail"] = []string{
|
|
|
|
"detail @id - Print details about a timer",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["delete"] = cmdDeleteTimer
|
|
|
|
validOperations["delete"] = []string{
|
|
|
|
"delete @id - Delete a timer",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["end"] = cmdStopTimer
|
|
|
|
validOperations["end"] = []string{
|
|
|
|
"end - The same as stop",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["help"] = cmdPrintHelp
|
|
|
|
validOperations["help"] = []string{
|
|
|
|
"help - Print this",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["list"] = cmdPrintList
|
|
|
|
validOperations["list"] = []string{
|
|
|
|
"list [duration] [+tags] - List time entries",
|
|
|
|
" valid values of [duration] include:",
|
2018-04-12 19:31:39 +00:00
|
|
|
" :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",
|
|
|
|
" Or other date values, we'll try to parse it.",
|
2018-02-15 13:45:53 +00:00
|
|
|
" To list entries by tag, preceed the tags with a +",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["ls"] = cmdPrintList
|
|
|
|
validOperations["ls"] = []string{
|
|
|
|
"ls [duration] [+tags] - The same as list",
|
|
|
|
}
|
|
|
|
|
2018-04-16 22:17:22 +00:00
|
|
|
opFuncs["modify"] = cmdModifyTimer
|
|
|
|
validOperations["modify"] = []string{
|
|
|
|
"modify [+tags] - Modify a timer",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["mod"] = cmdModifyTimer
|
|
|
|
validOperations["mod"] = []string{
|
|
|
|
"mod [+tags] - Modify a timer",
|
|
|
|
}
|
|
|
|
|
2018-02-15 13:45:53 +00:00
|
|
|
opFuncs["remove"] = cmdDeleteTimer
|
|
|
|
validOperations["remove"] = []string{
|
|
|
|
"remove @id - See 'delete'",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["status"] = cmdPrintStatus
|
|
|
|
validOperations["status"] = []string{
|
|
|
|
"status - Print the status of all active timers",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["start"] = cmdStartTimer
|
|
|
|
validOperations["start"] = []string{
|
2018-04-16 21:27:52 +00:00
|
|
|
"start [time] [+tags] - Start a timer with the given tags (space separated)",
|
2018-02-15 13:45:53 +00:00
|
|
|
" 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",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["stop"] = cmdStopTimer
|
|
|
|
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).",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["switch"] = cmdSwitchTimer
|
|
|
|
validOperations["switch"] = []string{
|
2018-04-16 21:27:52 +00:00
|
|
|
"switch [+tags] - Stop all currently running timers and start a new",
|
2018-02-15 13:45:53 +00:00
|
|
|
" one with the given tags",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["archive"] = cmdDoArchive
|
|
|
|
validOperations["archive"] = []string{
|
|
|
|
"archive [date] - Archive all entries older than the given date",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["fuzzyparse"] = cmdDoFuzzyParse
|
|
|
|
validOperations["fuzzyparse"] = []string{
|
|
|
|
"fuzzyparse - Parse the next argument as a date/time and print",
|
|
|
|
" the RFC3339 result. (Basically for testing)",
|
|
|
|
}
|
|
|
|
|
|
|
|
opFuncs["tags"] = cmdListTags
|
|
|
|
validOperations["tags"] = []string{
|
|
|
|
"tags - List all tags that have been used in non-archived",
|
|
|
|
" time entries",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the Config
|
|
|
|
cfg, err = userConfig.NewConfig(AppName)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
fmt.Println("Creating new config")
|
|
|
|
cfg.Save()
|
|
|
|
}
|
|
|
|
// 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)
|
|
|
|
}
|
2018-05-10 14:44:53 +00:00
|
|
|
if _, err := time.ParseDuration(cfg.Get("roundto")); err != nil {
|
|
|
|
cfg.Set("roundto", DefRoundTo)
|
|
|
|
}
|
2018-02-15 13:45:53 +00:00
|
|
|
|
|
|
|
fuzzyFormats = []string{
|
|
|
|
"1504",
|
|
|
|
"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
|
|
|
|
"2006-01-02",
|
|
|
|
"2006-01-02 15:04",
|
|
|
|
"2006-01-02 15:04:05",
|
|
|
|
"20060102",
|
|
|
|
"20060102 15:04",
|
|
|
|
"20060102 15:04:05",
|
|
|
|
"20060102 1504",
|
|
|
|
"20060102 150405",
|
|
|
|
"20060102T15:04",
|
|
|
|
"20060102T15:04:05",
|
|
|
|
"20060102T1504",
|
|
|
|
"20060102T150405",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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]
|
|
|
|
}
|