Add 'Add' Function

This commit is contained in:
Brian Buller 2018-04-16 16:27:52 -05:00
parent acc6d135e5
commit 0e7eea999f
2 changed files with 56 additions and 3 deletions

11
main.go
View File

@ -33,7 +33,6 @@ func main() {
if len(os.Args) > 1 { if len(os.Args) > 1 {
parms = os.Args[1:] parms = os.Args[1:]
parms[0] = matchParameter(parms[0])
} else { } else {
// If no parameters were passed, just print the status // If no parameters were passed, just print the status
parms = append(parms, "status") parms = append(parms, "status")
@ -204,6 +203,12 @@ func initialize() {
validOperations = make(map[string][]string) validOperations = make(map[string][]string)
opFuncs = make(map[string]func([]string) int) opFuncs = make(map[string]func([]string) int)
opFuncs["add"] = cmdAddTimer
validOperations["add"] = []string{
"add [duration] [+tags] - Add a timer for the given duration",
" with the given tags",
}
opFuncs["cont"] = cmdContinueTimer opFuncs["cont"] = cmdContinueTimer
validOperations["cont"] = []string{ validOperations["cont"] = []string{
"cont [time] - Continue the last stopped timer", "cont [time] - Continue the last stopped timer",
@ -269,7 +274,7 @@ func initialize() {
opFuncs["start"] = cmdStartTimer opFuncs["start"] = cmdStartTimer
validOperations["start"] = []string{ validOperations["start"] = []string{
"start [time] [tags ...] - Start a timer with the given tags (space separated)", "start [time] [+tags] - Start a timer with the given tags (space separated)",
" If the first sub-argument given looks like a time,", " If the first sub-argument given looks like a time,",
" the timer will be started then (past or future).", " the timer will be started then (past or future).",
" If a timer is already running it'll be stopped", " If a timer is already running it'll be stopped",
@ -284,7 +289,7 @@ func initialize() {
opFuncs["switch"] = cmdSwitchTimer opFuncs["switch"] = cmdSwitchTimer
validOperations["switch"] = []string{ validOperations["switch"] = []string{
"switch [tags ...] - Stop all currently running timers and start a new", "switch [+tags] - Stop all currently running timers and start a new",
" one with the given tags", " one with the given tags",
} }

View File

@ -42,6 +42,54 @@ func cmdStartTimer(args []string) int {
return 0 return 0
} }
func cmdAddTimer(args []string) int {
var err error
var entry *gime.TimeEntry
// By default we start the timer now
tags, rem := pullTagsFromArgs(args)
var beg, end time.Time
for _, opt := range rem {
var tmpBeg, tmpEnd time.Time
if strings.Contains(opt, "-") {
pts := strings.Split(opt, "-")
if len(pts[0]) > 0 {
// This should be the starting date
tmpBeg, err = parseFuzzyTime(pts[0])
if err != nil {
continue
}
}
if len(pts[1]) > 0 {
// This should be the ending date
tmpEnd, err = parseFuzzyTime(pts[1])
if err != nil {
continue
}
}
}
if !tmpBeg.IsZero() || !tmpEnd.IsZero() {
beg, end = tmpBeg, tmpEnd
}
}
if end.IsZero() {
end = time.Now()
}
tc := new(gime.TagCollection)
for i := range tags {
tc.Push(tags[i])
}
if entry, err = gime.CreateTimeEntry(beg, end, tc); err != nil {
fmt.Println(err)
return 1
}
if err = gdb.SaveTimeEntry(entry); err != nil {
fmt.Println(err)
return 1
}
fmt.Println("Added Time Entry:", TimerToString(entry))
return 0
}
func cmdContinueTimer(args []string) int { func cmdContinueTimer(args []string) int {
// Get the last running timer and start a new one with the same tags // Get the last running timer and start a new one with the same tags
te, err := getMostRecentTimeEntry() te, err := getMostRecentTimeEntry()