gime/cmd/toggle.go

61 lines
1.2 KiB
Go

/*
Copyright © 2022 Brian Buller <brian@bullercodeworks.com>
*/
package cmd
import (
"fmt"
"git.bullercodeworks.com/brian/gime/cli"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// toggleCmd represents the toggle command
var toggleCmd = &cobra.Command{
Use: "toggle",
Short: "Toggle the most recent timer on and off",
RunE: opToggle,
}
func init() {
rootCmd.AddCommand(toggleCmd)
}
func opToggle(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
}
wrk, err := p.GetMostRecentTimer()
if err != nil {
fmt.Print("{\"icon\":\"time\",\"state\":\"Critical\", \"text\": \"Error loading timer entry\"}")
return nil
}
var startArgs []string
if wrk.Finished {
// Start a new timer with the same data
for _, v := range wrk.Contexts {
startArgs = append(startArgs, "@"+v)
}
for _, v := range wrk.Projects {
startArgs = append(startArgs, "+"+v)
}
if viper.GetBool("copytags") {
for k, v := range wrk.AdditionalTags {
startArgs = append(startArgs, k+":"+v)
}
}
return opStart(cmd, startArgs)
} else {
// Stop the active timer
return opStop(cmd, []string{})
}
}