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
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// switchCmd represents the switch command
|
|
|
|
var switchCmd = &cobra.Command{
|
|
|
|
Use: "switch",
|
2022-01-21 14:40:44 +00:00
|
|
|
Short: "Stop the current timer and start a new one copying the last one's parameters",
|
|
|
|
RunE: opSwitch,
|
2022-01-19 18:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(switchCmd)
|
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 opSwitch(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 timerIds []int
|
|
|
|
end := time.Now()
|
|
|
|
// Stop all running timers and start a new one with the given args
|
2023-01-12 12:04:17 +00:00
|
|
|
for _, v := range p.TimerList.GetActiveTimers().GetTimerSlice() {
|
2022-01-21 14:40:44 +00:00
|
|
|
timerIds = append(timerIds, v.Id)
|
|
|
|
}
|
|
|
|
fmt.Print("Stopping ", timerIds, "\n")
|
|
|
|
for _, v := range timerIds {
|
|
|
|
var stopped *timertxt.Timer
|
|
|
|
if stopped, err = p.TimerList.GetTimer(v); err != nil {
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
stopped.FinishDate = end
|
|
|
|
stopped.Finished = true
|
|
|
|
fmt.Println("Stopped Timer:", util.TimerToFriendlyString(stopped))
|
|
|
|
}
|
|
|
|
return opStart(cmd, args)
|
2022-01-19 18:56:31 +00:00
|
|
|
}
|