aboutsummaryrefslogtreecommitdiff
path: root/cmd/add.go
blob: b8979b02a7a3a31b1326a0541799949ff2a57aa5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package cmd

import (
	"strings"

	"github.com/spf13/cobra"
)

// 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, " ")
		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)
}