104 lines
2.3 KiB
Go
104 lines
2.3 KiB
Go
/*
|
|
Copyright © 2022 Brian Buller <brian@bullercodeworks.com>
|
|
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.bullercodeworks.com/brian/gime/cli"
|
|
"git.bullercodeworks.com/brian/gime/util"
|
|
"git.bullercodeworks.com/brian/go-timertxt"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// modCmd represents the mod command
|
|
var modCmd = &cobra.Command{
|
|
Use: "mod",
|
|
Short: "Modify a timer",
|
|
RunE: opMod,
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(modCmd)
|
|
}
|
|
|
|
func opMod(cmd *cobra.Command, args []string) error {
|
|
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
|
|
active := p.TimerList.GetActiveTimers().GetTimerSlice()
|
|
if len(active) > 0 {
|
|
timer = active[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)
|
|
}
|
|
return nil
|
|
}
|