-
Notifications
You must be signed in to change notification settings - Fork 0
/
logbook.go
62 lines (51 loc) · 1.15 KB
/
logbook.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
package main
import (
"database/sql"
"time"
_ "github.com/mattn/go-sqlite3"
)
type logbookEntry struct {
dbId int
commandName string
historyId int
exitCode int
uuid string
execTime time.Time
}
func logbookRetrieveLastEntry(db *sql.DB) logbookEntry {
var l logbookEntry
row := db.QueryRow("SELECT * FROM command ORDER BY ID DESC LIMIT 1")
err := row.Scan(&l.dbId, &l.commandName, &l.historyId, &l.exitCode, &l.uuid, &l.execTime)
if err != nil {
l.historyId = -1
l.commandName = ""
}
return l
}
func logbookEntryFromRow(rows *sql.Rows) logbookEntry {
var l logbookEntry
rows.Scan(&l.dbId, &l.commandName, &l.historyId, &l.exitCode, &l.uuid, &l.execTime)
return l
}
type logBook struct {
rows *sql.Rows
}
func (l logBook) Next() bool {
return l.rows.Next()
}
func (l logBook) Value() logbookEntry {
return logbookEntryFromRow(l.rows)
}
func initLogBook(rows *sql.Rows) logBook {
return logBook{rows}
}
func (l logBook) String() string {
return l.Value().commandName
}
func logBookToEntryList(l logBook) []logbookEntry {
entries := []logbookEntry{}
for l.Next() {
entries = append(entries, l.Value())
}
return entries
}