gime/cmd/rm.go

58 lines
1.2 KiB
Go

/*
Copyright © 2022 Brian Buller <brian@bullercodeworks.com>
*/
package cmd
import (
"fmt"
"strconv"
"git.bullercodeworks.com/brian/gime/cli"
"git.bullercodeworks.com/brian/gime/util"
"github.com/spf13/cobra"
)
// rmCmd represents the rm command
var rmCmd = &cobra.Command{
Use: "rm",
Short: "Remove a timer",
RunE: opRemove,
}
func init() {
rootCmd.AddCommand(rmCmd)
}
func opRemove(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
}
if len(args) == 0 {
return fmt.Errorf("No timer id given")
}
id, err := strconv.Atoi(args[0])
if err != nil {
return fmt.Errorf("Invalid timer id given %s: %w", args[0], err)
}
t, err := p.TimerList.GetTimer(id)
if err != nil {
return fmt.Errorf("Error getting timer with id %s: %w", args[0], err)
}
if err = p.TimerList.RemoveTimerById(id); err != nil {
return fmt.Errorf("Error Removing Timer: %w", err)
}
fmt.Println("Timer removed")
fmt.Println(util.TimerToString(t))
if err := p.WriteTimerList(); err != nil {
return fmt.Errorf("Error writing timer list: %w", err)
}
return nil
}