gime/cmd/stop.go

74 lines
1.5 KiB
Go

/*
Copyright © 2022 Brian Buller <brian@bullercodeworks.com>
*/
package cmd
import (
"fmt"
"strconv"
"time"
"git.bullercodeworks.com/brian/gime/cli"
"git.bullercodeworks.com/brian/gime/util"
"git.bullercodeworks.com/brian/go-timertxt"
"github.com/spf13/cobra"
)
// stopCmd represents the stop command
var stopCmd = &cobra.Command{
Use: "stop",
Short: "Stop a timer",
RunE: opStop,
}
func init() {
rootCmd.AddCommand(stopCmd)
}
func opStop(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 wrk time.Time
end := time.Now()
id := -1
if len(args) > 0 {
if wrk, err = util.ParseFuzzyTime(args[0]); err != nil {
id, err = strconv.Atoi(args[0])
} else {
end = wrk
args = args[1:]
}
}
fmt.Println("Stopping at : " + end.Format(time.RFC3339))
var timerIds []int
if id == -1 {
for _, v := range *p.TimerList.GetActiveTimers() {
timerIds = append(timerIds, v.Id)
}
} else {
timerIds = append(timerIds, id)
}
for _, v := range timerIds {
var stopped *timertxt.Timer
if stopped, err = p.TimerList.GetTimer(v); err != nil {
fmt.Println(err.Error())
}
stopped.FinishDate = end
stopped.Finished = true
fmt.Println("Stopped Timer:", util.TimerToFriendlyString(stopped))
}
if err = p.WriteTimerList(); err != nil {
return fmt.Errorf("Error writing timer list: %w", err)
}
return nil
}