I believe that all functionality is implemented
This commit is contained in:
87
cmd/mod.go
87
cmd/mod.go
@@ -6,21 +6,21 @@ 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: "A brief description of your command",
|
||||
Long: `A longer description that spans multiple lines and likely contains examples
|
||||
and usage of using your command. For example:
|
||||
|
||||
Cobra is a CLI library for Go that empowers applications.
|
||||
This application is a tool to generate the needed files
|
||||
to quickly create a Cobra application.`,
|
||||
RunE: opMod,
|
||||
Short: "Modify a timer",
|
||||
RunE: opMod,
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -28,6 +28,75 @@ func init() {
|
||||
}
|
||||
|
||||
func opMod(cmd *cobra.Command, args []string) error {
|
||||
fmt.Println("mod called")
|
||||
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)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user