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"
|
2022-01-21 14:40:44 +00:00
|
|
|
"strings"
|
2022-01-19 18:56:31 +00:00
|
|
|
|
2022-01-21 14:40:44 +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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// startCmd represents the start command
|
|
|
|
var startCmd = &cobra.Command{
|
|
|
|
Use: "start",
|
2022-01-21 14:40:44 +00:00
|
|
|
Short: "Start a timer",
|
|
|
|
RunE: opStart,
|
2022-01-19 18:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(startCmd)
|
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 opStart(cmd *cobra.Command, args []string) error {
|
2022-01-21 14:40:44 +00:00
|
|
|
var err error
|
|
|
|
p := cli.Program{}
|
|
|
|
err = p.Initialize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = p.LoadTimerList(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var contexts, projects, strTags []string
|
|
|
|
t := timertxt.NewTimer()
|
|
|
|
if len(args) > 0 {
|
|
|
|
if start, err := util.ParseFuzzyTime(args[0]); err == nil {
|
|
|
|
t.StartDate = start
|
|
|
|
args = args[1:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
contexts, args = util.GetContextsFromSlice(args)
|
|
|
|
projects, args = util.GetProjectsFromSlice(args)
|
|
|
|
strTags, args = util.GetAdditionalTagsFromSlice(args)
|
|
|
|
for _, v := range contexts {
|
|
|
|
t.Contexts = append(t.Contexts, strings.TrimPrefix(v, "@"))
|
|
|
|
}
|
|
|
|
for _, v := range projects {
|
|
|
|
t.Projects = append(t.Projects, strings.TrimPrefix(v, "+"))
|
|
|
|
}
|
|
|
|
for _, v := range strTags {
|
|
|
|
tgPts := strings.Split(v, ":")
|
|
|
|
t.AdditionalTags[tgPts[0]] = tgPts[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
p.TimerList.AddTimer(t)
|
|
|
|
if err := p.WriteTimerList(); err != nil {
|
|
|
|
return fmt.Errorf("Error writing timer list: %w", err)
|
|
|
|
}
|
|
|
|
fmt.Println("Started: ", util.TimerToString(t))
|
2022-01-19 21:30:30 +00:00
|
|
|
return nil
|
2022-01-19 18:56:31 +00:00
|
|
|
}
|