/* Copyright © 2022 Brian Buller */ package cmd import ( "fmt" "time" "git.bullercodeworks.com/brian/gime/cli" "git.bullercodeworks.com/brian/gime/util" "git.bullercodeworks.com/brian/go-timertxt" "github.com/spf13/cobra" ) // switchCmd represents the switch command var switchCmd = &cobra.Command{ Use: "switch", Short: "Stop the current timer and start a new one copying the last one's parameters", RunE: opSwitch, } func init() { rootCmd.AddCommand(switchCmd) } func opSwitch(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 timerIds []int end := time.Now() // Stop all running timers and start a new one with the given args for _, v := range p.TimerList.GetActiveTimers().GetTimerSlice() { 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) }