gime/cmd/toggle.go

61 lines
1.2 KiB
Go
Raw Permalink 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"
"git.bullercodeworks.com/brian/gime/cli"
2022-01-19 18:56:31 +00:00
"github.com/spf13/cobra"
"github.com/spf13/viper"
2022-01-19 18:56:31 +00:00
)
// toggleCmd represents the toggle command
var toggleCmd = &cobra.Command{
Use: "toggle",
Short: "Toggle the most recent timer on and off",
RunE: opToggle,
2022-01-19 18:56:31 +00:00
}
func init() {
rootCmd.AddCommand(toggleCmd)
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 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{})
}
2022-01-19 18:56:31 +00:00
}