2022-01-19 18:56:31 +00:00
|
|
|
/*
|
2022-01-19 21:30:30 +00:00
|
|
|
Copyright © 2022 Brian Buller <brian@bullercodeworks.com>
|
2022-01-19 18:56:31 +00:00
|
|
|
|
|
|
|
*/
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-01-21 14:40:44 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2022-01-19 18:56:31 +00:00
|
|
|
|
2022-01-21 14:40:44 +00:00
|
|
|
"git.bullercodeworks.com/brian/gime/cli"
|
|
|
|
"git.bullercodeworks.com/brian/gime/util"
|
|
|
|
"git.bullercodeworks.com/brian/go-timertxt"
|
2022-01-19 18:56:31 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
// modCmd represents the mod command
|
|
|
|
var modCmd = &cobra.Command{
|
|
|
|
Use: "mod",
|
2022-01-21 14:40:44 +00:00
|
|
|
Short: "Modify a timer",
|
|
|
|
RunE: opMod,
|
2022-01-19 18:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(modCmd)
|
2022-01-19 21:30:30 +00:00
|
|
|
}
|
2022-01-19 18:56:31 +00:00
|
|
|
|
2022-01-19 21:30:30 +00:00
|
|
|
func opMod(cmd *cobra.Command, args []string) error {
|
2022-01-21 14:40:44 +00:00
|
|
|
var err error
|
|
|
|
p := cli.Program{}
|
|
|
|
err = p.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = p.LoadTimerList(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var timer *timertxt.Timer
|
|
|
|
var contexts, projects []string
|
|
|
|
id, err := strconv.Atoi(args[0])
|
|
|
|
if err != nil {
|
|
|
|
// We didn't have a timer id, so try to modify the first active timer
|
|
|
|
if len(*p.TimerList.GetActiveTimers()) > 0 {
|
|
|
|
timer = (*p.TimerList.GetActiveTimers())[0]
|
|
|
|
} else {
|
|
|
|
// And we don't have any active timers
|
|
|
|
return fmt.Errorf("No active timers, 'id' must be provided: %w", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
args = args[1:]
|
|
|
|
if timer, err = p.TimerList.GetTimer(id); err != nil {
|
|
|
|
return fmt.Errorf("Error getting timer %d: %w", id, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
var start, end time.Time
|
|
|
|
|
|
|
|
for _, v := range args {
|
|
|
|
pts := strings.Split(v, "=")
|
|
|
|
switch pts[0] {
|
|
|
|
case "beginning", "start":
|
|
|
|
if start, err = util.ParseFuzzyTime(pts[1]); err != nil {
|
|
|
|
return fmt.Errorf("Error parsing start time: %w", err)
|
|
|
|
}
|
|
|
|
case "stop", "finish", "end":
|
|
|
|
if end, err = util.ParseFuzzyTime(pts[1]); err != nil {
|
|
|
|
return fmt.Errorf("Error parsing end time: %w", err)
|
|
|
|
}
|
|
|
|
case "project", "projects":
|
|
|
|
projects = strings.Split(pts[1], ",")
|
|
|
|
case "context", "contexts":
|
|
|
|
contexts = strings.Split(pts[1], ",")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(contexts) > 0 {
|
|
|
|
for k := range contexts {
|
|
|
|
contexts[k] = strings.TrimPrefix(contexts[k], "@")
|
|
|
|
}
|
|
|
|
timer.Contexts = contexts
|
|
|
|
}
|
|
|
|
if len(projects) > 0 {
|
|
|
|
for k := range projects {
|
|
|
|
projects[k] = strings.TrimPrefix(projects[k], "+")
|
|
|
|
}
|
|
|
|
timer.Projects = projects
|
|
|
|
}
|
|
|
|
if !start.IsZero() {
|
|
|
|
timer.StartDate = start
|
|
|
|
}
|
|
|
|
if !end.IsZero() {
|
|
|
|
timer.FinishDate = end
|
|
|
|
timer.Finished = true
|
|
|
|
}
|
|
|
|
fmt.Println("Modified Timer:")
|
|
|
|
fmt.Println(util.TimerToFriendlyString(timer))
|
|
|
|
|
|
|
|
if err := p.WriteTimerList(); err != nil {
|
|
|
|
return fmt.Errorf("Error writing timer list: %w", err)
|
|
|
|
}
|
2022-01-19 21:30:30 +00:00
|
|
|
return nil
|
2022-01-19 18:56:31 +00:00
|
|
|
}
|