Skip to content

Commit 6d7736b

Browse files
author
Yusaku Hatanaka
authored
add add-task command (#52)
add add-task command
1 parent b026950 commit 6d7736b

File tree

4 files changed

+165
-0
lines changed

4 files changed

+165
-0
lines changed

.golangci.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,11 @@ issues:
289289
linters:
290290
- gosec
291291

292+
- path: editor/editor.go
293+
text: "G204: Subprocess launched with variable"
294+
linters:
295+
- gosec
296+
292297
- path: notify
293298
text: "Subprocess launched with variable"
294299
linters:

cmd/addTask.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
}

editor/editor.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Package editor manage EDITOR
2+
package editor
3+
4+
import (
5+
"bufio"
6+
"io/ioutil"
7+
"os"
8+
"os/exec"
9+
)
10+
11+
// GetSliceText get slice text edited with editor
12+
func GetSliceText(initialText string) ([]string, error) {
13+
tmpfile, err := ioutil.TempFile("", "gomodoro")
14+
if err != nil {
15+
return nil, err
16+
}
17+
defer func() {
18+
_ = os.Remove(tmpfile.Name())
19+
}()
20+
defer func() {
21+
_ = tmpfile.Close()
22+
}()
23+
24+
_, err = tmpfile.Write([]byte(initialText))
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
if err = openEditor(tmpfile.Name()); err != nil {
30+
return nil, err
31+
}
32+
33+
f, err := os.Open(tmpfile.Name())
34+
if err != nil {
35+
return nil, err
36+
}
37+
defer func() {
38+
_ = f.Close()
39+
}()
40+
41+
ts := make([]string, 0)
42+
scanner := bufio.NewScanner(f)
43+
for scanner.Scan() {
44+
ts = append(ts, scanner.Text())
45+
}
46+
47+
if err := scanner.Err(); err != nil {
48+
return nil, err
49+
}
50+
51+
return ts, nil
52+
}
53+
54+
func openEditor(filepath string) error {
55+
cmdName := "vi"
56+
if e := os.Getenv("EDITOR"); e != "" {
57+
cmdName = e
58+
}
59+
60+
c := exec.Command(cmdName, filepath)
61+
c.Stdin = os.Stdin
62+
c.Stdout = os.Stdout
63+
c.Stderr = os.Stderr
64+
return c.Run()
65+
}

task/task.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,28 @@ func createTaskName(c screen.Client) (string, error) {
234234
}
235235
}
236236
}
237+
238+
// AddTask save task to file
239+
func AddTask(taskFile, name string) error {
240+
if name == "" {
241+
return xerrors.New("task name is empty")
242+
}
243+
244+
c := &clientImpl{
245+
taskFile: taskFile,
246+
}
247+
248+
tasks, err := c.loadTasks()
249+
if err != nil {
250+
return err
251+
}
252+
253+
tasks = append(tasks, &Task{Name: name})
254+
255+
err = c.saveTasks(tasks)
256+
if err != nil {
257+
return err
258+
}
259+
260+
return nil
261+
}

0 commit comments

Comments
 (0)