-
Notifications
You must be signed in to change notification settings - Fork 0
/
notes.go
199 lines (163 loc) · 5.08 KB
/
notes.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
package main
import(
"database/sql"
"fmt"
_"github.com/mattn/go-sqlite3"
"os"
"os/user"
"strings"
)
func main(){
db, err := sql.Open("sqlite3", user.HomeDir + "/gonotes/notes.db")
checkErr(err)
count := len(os.Args)
if count > 1{
firstArg := os.Args[1]
switch firstArg{
case "-list":
//fmt.Println("List all notes")
if count == 2 {
rows, err := db.Query("SELECT * FROM lists")
checkErr(err)
fmt.Println("\n Existing lists are: ")
for rows.Next(){
var id int
var listName string
err := rows.Scan(&id, &listName)
checkErr(err)
fmt.Println("\n " + listName)
}
fmt.Println("\n ``````````````````````````````")
fmt.Println("\n For notes in the list, type: ")
fmt.Println("gonotes -list <list name> \n")
} else{
list := os.Args[2]
processList(list, db)
}
default:
processCrap(os.Args[1:], db)
}
}else{
helpText()
}
db.Close()
}
func processCrap(args []string, db *sql.DB){
c := len(args)
firstId := 0
arg := args[firstId]
var name string
var Lid int
if string(arg[0]) == "@"{
if c > 2 {
listTag := arg
fmt.Println("\n Added to list " + listTag)
rows, err := db.Query("SELECT uid FROM lists where name =?", string(listTag))
checkErr(err)
for rows.Next(){
err := rows.Scan(&Lid)
//fmt.Println(Lid)
checkErr(err)
}
if Lid == 0{
//fmt.Println("not found...creating")
stmt, err := db.Prepare("INSERT INTO lists('name') values (?)")
checkErr(err)
_, err = stmt.Exec(string(listTag))
checkErr(err)
}else{
//fmt.Println("List found.. ID: " + string(Lid))
}
firstId += 1
name = args[firstId]
} else {
helpText()
}
}else{
name = args[firstId]
}
if c>1{
msg := args[firstId +1 :]
messagewa := strings.Join(msg," ")
ps := name + ": " + messagewa
fmt.Println("\n Added: \n " + ps)
//check if exists, if yes, update
stmt, err := db.Prepare("update notes set message=?,created=CURRENT_TIMESTAMP,listid=? where name=?")
checkErr(err)
res,err := stmt.Exec(messagewa, Lid, name)
checkErr(err)
affect, err := res.RowsAffected()
checkErr(err)
if affect == 0{
stmt, err := db.Prepare("INSERT INTO notes(name, message, created, listid) values(?,?,CURRENT_TIMESTAMP, ?)")
checkErr(err)
_, err = stmt.Exec(name, messagewa, Lid)
checkErr(err)
//id, err := res.LastInsertId()
//checkErr(err)
//fmt.Println(id)
}
}else{
//fmt.Println("Check db for msg.. if not found open new multiline")
rows, err := db.Query("SELECT * FROM notes where name =?", name)
checkErr(err)
for rows.Next(){
var uid int
var name string
var message string
var created string
var listid int
err = rows.Scan(&uid, &name, &message, &created, &listid)
checkErr(err)
fmt.Println("\n " + name + " , " + created)
fmt.Println("\n `````````````````````````````````")
fmt.Println("\n " + message + "\n ")
}
}
}
func processList(list string, db *sql.DB){
listTag:= list
fmt.Println("\n " + listTag)
fmt.Println("\n `````````````````````````\n")
var Lid int
rows, err := db.Query("SELECT uid FROM lists where name =?", listTag)
checkErr(err)
for rows.Next(){
err = rows.Scan(&Lid)
//fmt.Println(Lid)
checkErr(err)
}
notesRows, err:= db.Query("SELECT * FROM notes where listid =?", Lid)
checkErr(err)
for notesRows.Next(){
var uid int
var name string
var message string
var created string
var listid int
err = notesRows.Scan(&uid, &name, &message, &created, &listid)
checkErr(err)
var messagePrompt string
if len(message) < 10 {
messagePrompt = message
}else{
messagePrompt = message[:10]
}
fmt.Println("\n " + name + ": " + messagePrompt + "... ")
}
fmt.Println("\n")
}
func checkErr(err error){
if err!= nil{
panic(err)
}
}
func helpText(){
fmt.Println("\n GoNotes - Random notes and stuff \n")
fmt.Println("``````````````````````````````````` \n")
fmt.Println(" Usage: \n")
fmt.Println(" gonotes <list tag> (optional) <name of the note> <note/message> \n")
fmt.Println(" gonotes <name of the note> to display the note \n")
fmt.Println(" gonotes -list <tag> to list all notes under tag \n")
fmt.Println(" ```````````````````````````````````````````````` \n")
}