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"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// stopCmd represents the stop command
|
|
|
|
var stopCmd = &cobra.Command{
|
|
|
|
Use: "stop",
|
2022-01-21 14:40:44 +00:00
|
|
|
Short: "Stop a timer",
|
|
|
|
RunE: opStop,
|
2022-01-19 18:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(stopCmd)
|
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 opStop(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 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)
|
|
|
|
}
|
2022-01-19 21:30:30 +00:00
|
|
|
return nil
|
2022-01-19 18:56:31 +00:00
|
|
|
}
|