-
Notifications
You must be signed in to change notification settings - Fork 1
/
note.go
156 lines (146 loc) · 3.13 KB
/
note.go
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package note
import (
"strings"
note "github.com/espinosajuanma/note/pkg"
Z "github.com/rwxrob/bonzai/z"
"github.com/rwxrob/conf"
"github.com/rwxrob/help"
"github.com/rwxrob/term"
)
var Cmd = &Z.Cmd{
Name: `note`,
Summary: `taking quick notes`,
Version: `v0.0.2`,
Commands: []*Z.Cmd{help.Cmd, conf.Cmd, List, New, Edit, Remove, Latest, Push},
}
var New = &Z.Cmd{
Name: `new`,
Aliases: []string{"add"},
Summary: `add a new note to the current directory`,
Usage: `[title]`,
Commands: []*Z.Cmd{help.Cmd},
Call: func(_ *Z.Cmd, args ...string) error {
title := "Title's note"
if len(args) != 0 {
title = strings.Join(args, " ")
}
n, err := note.New(title)
if err != nil {
return err
}
n.Init()
n.Edit()
if yesOrNo("Commit?") {
n, err = note.GetById(n.Id)
Z.Exec("git", "add", n.Id)
Z.Exec("git", "commit", "-m", n.Title)
Z.Exec("git", "push")
}
return nil
},
}
var List = &Z.Cmd{
Name: `list`,
Aliases: []string{"ls"},
Summary: `list all valid notes in the current directory`,
Commands: []*Z.Cmd{help.Cmd},
Call: func(_ *Z.Cmd, _ ...string) error {
list, err := note.List()
if err != nil {
return err
}
for _, v := range list {
v.Print()
}
return nil
},
}
var Edit = &Z.Cmd{
Name: `edit`,
Aliases: []string{"e", "ed"},
Usage: `<id>`,
Summary: `open a specific note in your editor`,
Commands: []*Z.Cmd{help.Cmd},
Call: func(_ *Z.Cmd, args ...string) error {
n, err := note.Latest()
if len(args) != 0 {
n, err = note.GetById(args[0])
}
if err != nil {
return err
}
n.Edit()
if yesOrNo("Commit?") {
n, err = note.GetById(n.Id)
Z.Exec("git", "add", n.Id)
Z.Exec("git", "commit", "-m", n.Title)
Z.Exec("git", "push")
}
return nil
},
}
var Push = &Z.Cmd{
Name: `push`,
Usage: `<id>`,
Aliases: []string{"commit"},
Summary: `commit and push to git`,
Commands: []*Z.Cmd{help.Cmd},
Call: func(_ *Z.Cmd, args ...string) error {
n, err := note.Latest()
if len(args) != 0 {
n, err = note.GetById(args[0])
}
if err != nil {
return err
}
Z.Exec("git", "add", n.Path)
Z.Exec("git", "commit", "-m", n.Title)
Z.Exec("git", "push")
return nil
},
}
var Remove = &Z.Cmd{
Name: `remove`,
Aliases: []string{"rm"},
Usage: `<id>`,
Summary: `remove a specific note`,
Commands: []*Z.Cmd{help.Cmd},
Call: func(_ *Z.Cmd, args ...string) error {
n, err := note.Latest()
if len(args) != 0 {
n, err = note.GetById(args[0])
}
if err != nil {
return err
}
n.Print()
if yesOrNo("Are you sure you want to remove this note?") {
err := n.Remove()
if err != nil {
return err
}
Z.Exec("git", "add", n.Id)
Z.Exec("git", "commit", "-m", "`Removed` "+n.Title)
Z.Exec("git", "push")
}
return nil
},
}
var Latest = &Z.Cmd{
Name: `latest`,
Aliases: []string{"last"},
Summary: `prints latest note`,
Commands: []*Z.Cmd{help.Cmd},
Call: func(_ *Z.Cmd, args ...string) error {
n, err := note.Latest()
if err != nil {
return err
}
n.Print()
return nil
},
}
func yesOrNo(q string) bool {
y := term.Prompt(q + " (y/N) ")
return y == "Y" || y == "y" || y == "yes"
}