-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
94 lines (74 loc) · 1.61 KB
/
Copy pathapp.go
File metadata and controls
94 lines (74 loc) · 1.61 KB
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
package main
import (
"context"
"database/sql"
"encoding/json"
"os"
"runtime"
_ "github.com/mattn/go-sqlite3"
)
type App struct {
ctx context.Context
db *sql.DB
version string
}
func check(e error) {
if e != nil {
panic(e)
}
}
func NewApp() *App {
return &App{}
}
func (a *App) startup(ctx context.Context) {
home, _ := os.UserHomeDir()
a.version = "v0.2.3" + " - " + runtime.GOOS + " - " + runtime.GOARCH
a.ctx = ctx
if _, err := os.Stat(home + string(os.PathSeparator) + ".cheat_sheets.db"); os.IsNotExist(err) {
a.db = nil
} else {
db, err := sql.Open("sqlite3", home+string(os.PathSeparator)+".cheat_sheets.db")
check(err)
a.db = db
}
}
func (a *App) GetPrograms() string {
tables := []string{}
if a.db != nil {
dbtables, err := a.db.Query("select name from sqlite_master where type = 'table'")
check(err)
for dbtables.Next() {
var name string
_ = dbtables.Scan(&name)
tables = append(tables, name)
}
}
jsonData, err := json.Marshal(tables)
check(err)
return string(jsonData)
}
func (a *App) GetVersion() string {
return a.version
}
func (a *App) GetCheatSheet(program string) string {
if a.db == nil {
return "{}"
}
rows, err := a.db.Query("SELECT * FROM " + program)
check(err)
data := make(map[string][]map[string]string)
for rows.Next() {
var command string
var about string
var session string
_ = rows.Scan(&command, &about, &session)
commands := map[string]string{
"command": command,
"about": about,
}
data[session] = append(data[session], commands)
}
jsonData, err := json.Marshal(data)
check(err)
return string(jsonData)
}