-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
67 lines (59 loc) · 1.45 KB
/
store.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
package store
import (
"database/sql"
"encoding/json"
"fmt"
"log"
"os"
"path"
_ "github.com/mattn/go-sqlite3"
)
type Store struct {
historyDb *sql.DB
aliasDb *sql.DB
}
func getSqliteFilepath(name string) string {
storePath, ok := os.LookupEnv("HOME")
if !ok {
log.Fatal("Cannot determine home directory, please set the HOME envionment variable")
}
return path.Join(storePath, fmt.Sprintf(".autoalias.%s.sqlite", name))
}
func New() (*Store, error) {
historyDb, err := sql.Open("sqlite3", "file:"+getSqliteFilepath("history"))
if err != nil {
return nil, err
}
aliasDb, err := sql.Open("sqlite3", "file:"+getSqliteFilepath("alias"))
if err != nil {
return nil, err
}
if _, err := historyDb.Exec(`CREATE TABLE IF NOT EXISTS history
(id INTEGER PRIMARY KEY AUTOINCREMENT, command_array TEXT, used TEXT)
`); err != nil {
return nil, err
}
if _, err := aliasDb.Exec(`CREATE TABLE IF NOT EXISTS aliases
(id INTEGER PRIMARY KEY AUTOINCREMENT, alias TEXT, command TEXT, created TEXT, last_used TEXT)
`); err != nil {
return nil, err
}
return &Store{
historyDb: historyDb,
aliasDb: aliasDb,
}, nil
}
func JsonArrayToArray(jsonArr string) ([]string, error) {
var arr []string
if err := json.Unmarshal([]byte(jsonArr), &arr); err != nil {
return nil, err
}
return arr, nil
}
func ArrayToJsonArray(arr []string) (string, error) {
content, err := json.Marshal(arr)
if err != nil {
return "", err
}
return string(content[:]), nil
}