-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (111 loc) · 2.22 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
package main
import (
"fmt"
"os"
"path"
"path/filepath"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/urfave/cli/v2"
_ "modernc.org/sqlite"
)
var (
verbose bool
verbosePrefix = text.FgBlue.Sprint("V:")
errorPrefix = text.FgHiRed.Sprint("ERROR:")
)
// GetDBPath returns the full path to the database file
func GetDBPath() (string, error) {
hd, err := os.UserHomeDir()
if err != nil {
return "", err
}
dbPath := path.Join(hd, ".ggg", "notes.db")
LogOnVerbose(fmt.Sprint("Using DB at", dbPath))
return dbPath, nil
}
// InitDB creates and initializes a new database
func InitDB(c *cli.Context) error {
path, err := GetDBPath()
if err != nil {
return err
}
appDir := filepath.Dir(path)
err = os.MkdirAll(appDir, 0700)
if err != nil {
return err
}
err = CreateDB(path)
if err != nil {
return ExitWithError(err, "Could not create DB")
}
fmt.Println("Created", path)
return nil
}
func FindAndOpenDB() error {
path, err := GetDBPath()
if err != nil {
return err
}
_, err = os.Stat(path)
if err != nil {
return err
}
return OpenDB(path)
}
func main() {
app := &cli.App{
Usage: "GoGoGadget notes!",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "verbose",
Aliases: []string{"v"},
Value: false,
Usage: "Verbose output",
Destination: &verbose,
},
},
Commands: []*cli.Command{
{
Name: "init",
Aliases: []string{"i"},
Usage: "Initialize a new database",
Action: InitDB,
},
{
Name: "new",
Aliases: []string{"n"},
Usage: "Add a new note",
Action: NewNote,
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "List notes containing the search term. if no option is passed, all notes are shown",
Action: ListNotes,
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "Delete note by id or title",
Action: DeleteNote,
},
{
Name: "open",
Aliases: []string{"o"},
Usage: "Open note by id or title",
Action: OpenNote,
},
{
Name: "find",
Aliases: []string{"f"},
Usage: "Find matches on notes",
Action: FindMatches,
},
},
}
err := app.Run(os.Args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}