gime/cmd/switch.go

57 lines
1.3 KiB
Go
Raw Normal View History

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"
"time"
2022-01-19 18:56:31 +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",
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 {
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() {
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
}