2021-12-05 17:46:53 +03:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
|
|
|
// addCmd represents the add command
|
|
|
|
var addCmd = &cobra.Command{
|
|
|
|
Use: "add",
|
|
|
|
Short: "Add activity",
|
|
|
|
Long: `Add new activity that we can track`,
|
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
titles := []string{}
|
|
|
|
tags := []string{}
|
|
|
|
contexts := []string{}
|
|
|
|
for _, s := range args {
|
|
|
|
if len(s) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if s[0:1] == "#" {
|
|
|
|
if len(s[1:]) >= 1 {
|
|
|
|
tags = append(tags, s[1:])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if s[0:1] == "@" {
|
|
|
|
if len(s[1:]) >= 1 {
|
|
|
|
contexts = append(contexts, s[1:])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
titles = append(titles, s)
|
|
|
|
}
|
|
|
|
title := strings.Join(titles, " ")
|
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
|
|
|
activityID, err := tr.Add(title, tags, contexts)
|
|
|
|
if err != nil {
|
|
|
|
cmd.PrintErr(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
cmd.Printf("Activity #%d added! Now you can start it.\n", activityID)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(addCmd)
|
|
|
|
}
|