/
main.go
220 lines (194 loc) · 4.4 KB
/
main.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"fmt"
"os"
"time"
"github.com/gummiboll/forgetful/commands"
"github.com/gummiboll/forgetful/storage"
"github.com/urfave/cli"
)
const version string = "1.1"
func printList(notes []string) {
if len(notes) > 0 {
fmt.Println(fmt.Sprintf("Found %d matching note(s):", len(notes)))
for _, n := range notes {
fmt.Println(n)
}
} else {
fmt.Println("No matches")
}
}
func printInfo(n storage.Note) {
fmt.Println(fmt.Sprintf("%s:", n.Name))
fmt.Println(fmt.Sprintf("Created at: %s, last updated at: %s", n.CreatedAt.Format("2006-01-02 15:04"), n.UpdatedAt.Format("2006-01-02 15:04")))
if n.Temporary == true {
validTo := n.UpdatedAt.Add(24 * time.Hour)
dur := validTo.Sub(time.Now())
fmt.Println(fmt.Sprintf("Is temporary, will expire in %s", dur-(dur%time.Second)))
}
}
func main() {
// Init
i := storage.Impl{}
if err := i.InitDB(); err != nil {
panic(err)
}
i.InitSchema()
if err := i.RemoveExpiredNotes(); err != nil {
panic(err)
}
app := cli.NewApp()
app.Name = "forgetful"
app.Usage = "For your notes/cheat sheets"
app.Version = version
app.Commands = []cli.Command{
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add a note",
Flags: []cli.Flag{
cli.BoolFlag{
Name: "t",
Usage: "Mark as temporary (expires after 24 hours)",
},
cli.BoolFlag{
Name: "p",
Usage: "Create note with contents from clipboard",
},
},
Action: func(c *cli.Context) error {
n, err := commands.AddCommand(c, i)
if err != nil {
return err
}
fmt.Println(fmt.Sprintf("Added note: %s", n.Name))
return nil
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "Delete a note",
Action: func(c *cli.Context) error {
n, err := commands.DeleteCommand(c, i)
if err != nil {
return err
}
fmt.Println(fmt.Sprintf("Deleted note: %s", n.Name))
return nil
},
},
{
Name: "edit",
Aliases: []string{"e"},
Usage: "Edit/read a note",
Action: func(c *cli.Context) error {
n, err := commands.EditCommand(c, i)
if err != nil {
return err
}
fmt.Println(fmt.Sprintf("Updated note: %s", n.Name))
return nil
},
},
{
Name: "info",
Aliases: []string{"i"},
Usage: "Prints information about a note",
Action: func(c *cli.Context) error {
n, err := commands.InfoCommand(c, i)
if err != nil {
return err
}
printInfo(n)
return nil
},
},
{
Name: "read",
Aliases: []string{"r"},
Usage: "Read a note",
Action: func(c *cli.Context) error {
if err := commands.ReadCommand(c, i); err != nil {
return err
}
return nil
},
},
{
Name: "rename",
Aliases: []string{"mv"},
Usage: "Rename a note",
Action: func(c *cli.Context) error {
nName, newName, err := commands.RenameCommand(c, i)
if err != nil {
return err
}
fmt.Println(fmt.Sprintf("Renamed %s to %s", nName, newName))
return nil
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "List all notes, filter result if argument i present",
Action: func(c *cli.Context) error {
notes := commands.ListCommand(c, i)
printList(notes)
return nil
},
},
{
Name: "search",
Aliases: []string{"s"},
Usage: "Search notes for argument",
Action: func(c *cli.Context) error {
notes, err := commands.SearchCommand(c, i)
if err != nil {
return err
}
printList(notes)
return nil
},
},
{
Name: "share",
Usage: "Share a note (publicly) on hastebin.com",
Action: func(c *cli.Context) error {
n, url, err := commands.ShareCommand(c, i)
if err != nil {
return err
}
fmt.Println(fmt.Sprintf("Shared note '%s': %s", n.Name, url))
return nil
},
},
{
Name: "keep",
Aliases: []string{"k"},
Usage: "Sets a temporary note as permanent",
Action: func(c *cli.Context) error {
n, err := commands.KeepCommand(c, i, true)
if err != nil {
return err
}
fmt.Println(fmt.Sprintf("Keeping note: %s", n.Name))
return nil
},
},
{
Name: "unkeep",
Aliases: []string{"u"},
Usage: "Sets a permanent note as temporary",
Action: func(c *cli.Context) error {
n, err := commands.KeepCommand(c, i, false)
if err != nil {
return err
}
fmt.Println(fmt.Sprintf("Unkeeping note: %s", n.Name))
return nil
},
},
}
app.Run(os.Args)
}