67 lines
1.5 KiB
Go
67 lines
1.5 KiB
Go
/*
|
|
Copyright © 2022 Brian Buller <brian@bullercodeworks.com>
|
|
|
|
*/
|
|
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.bullercodeworks.com/brian/gime/cli"
|
|
"git.bullercodeworks.com/brian/gime/util"
|
|
"git.bullercodeworks.com/brian/go-timertxt"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// startCmd represents the start command
|
|
var startCmd = &cobra.Command{
|
|
Use: "start",
|
|
Short: "Start a timer",
|
|
RunE: opStart,
|
|
}
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(startCmd)
|
|
}
|
|
|
|
func opStart(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 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))
|
|
return nil
|
|
}
|