nerdtrack/cmd/start.go

50 lines
970 B
Go
Raw Permalink Normal View History

2021-12-05 17:46:53 +03:00
package cmd
import (
"strconv"
"strings"
2021-12-18 02:29:55 +03:00
"github.com/spf13/afero"
2021-12-05 17:46:53 +03:00
"github.com/spf13/cobra"
2021-12-18 02:29:55 +03:00
"github.com/neonxp/track/internal/tracker"
2021-12-05 17:46:53 +03:00
)
// startCmd represents the start command
var startCmd = &cobra.Command{
Use: "start",
Short: "Start activity",
Long: `Start new timespan on activity`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
cmd.PrintErr("First argument must be activity id\n")
return
}
id, err := strconv.Atoi(args[0])
if err != nil {
cmd.PrintErr("First argument must be activity id, got %s\n", args[0])
return
}
comment := strings.Join(args[1:], " ")
2021-12-18 02:29:55 +03:00
fs := afero.NewOsFs()
tr, err := tracker.New(fs)
if err != nil {
cmd.PrintErr(err)
return
}
2021-12-05 17:46:53 +03:00
if err := tr.Start(id, comment); err != nil {
cmd.PrintErr(err)
return
}
2021-12-18 02:29:55 +03:00
2021-12-05 17:46:53 +03:00
activity := tr.Activity(id)
2021-12-18 02:29:55 +03:00
2021-12-05 17:46:53 +03:00
cmd.Printf("Started new span for activity \"%s\".\n", activity.Title)
},
}
func init() {
rootCmd.AddCommand(startCmd)
}