|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/spf13/cobra" |
| 8 | + |
| 9 | + "github.com/hatappi/gomodoro/config" |
| 10 | + "github.com/hatappi/gomodoro/editor" |
| 11 | + "github.com/hatappi/gomodoro/task" |
| 12 | +) |
| 13 | + |
| 14 | +const initialText = `# Please write one task per line` |
| 15 | + |
| 16 | +// addTaskCmd represents the addTask command |
| 17 | +var addTaskCmd = &cobra.Command{ |
| 18 | + Use: "add-task TASK_NAME", |
| 19 | + Short: "add task", |
| 20 | + Long: `This command adds a task. |
| 21 | +Please specify the task name in the argument. |
| 22 | +if you doesn't specify task name, editor starts up. |
| 23 | +And add a task using the editor. |
| 24 | +`, |
| 25 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 26 | + config, err := config.GetConfig() |
| 27 | + if err != nil { |
| 28 | + return err |
| 29 | + } |
| 30 | + taskPath, err := config.ExpandTaskFile() |
| 31 | + if err != nil { |
| 32 | + return err |
| 33 | + } |
| 34 | + |
| 35 | + name := strings.Join(args, " ") |
| 36 | + if name != "" { |
| 37 | + err = task.AddTask(taskPath, name) |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + fmt.Printf("add %s\n", name) |
| 42 | + return nil |
| 43 | + } |
| 44 | + |
| 45 | + ts, err := editor.GetSliceText(initialText) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + |
| 50 | + for _, t := range ts { |
| 51 | + if t == "" { |
| 52 | + continue |
| 53 | + } |
| 54 | + if strings.HasPrefix(t, "#") { |
| 55 | + continue |
| 56 | + } |
| 57 | + err = task.AddTask(taskPath, t) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + fmt.Printf("add %s\n", t) |
| 62 | + } |
| 63 | + |
| 64 | + return nil |
| 65 | + }, |
| 66 | +} |
| 67 | + |
| 68 | +func init() { |
| 69 | + rootCmd.AddCommand(addTaskCmd) |
| 70 | +} |
0 commit comments