-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
181 lines (162 loc) · 6.35 KB
/
config.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
package forms
import (
"fmt"
"net/http/cookiejar"
"os"
"time"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
u "github.com/sunshine69/golang-tools/utils"
)
// DateLayout - global
var DateLayout string
// WebNotePassword
var WebNotePassword string
// WebNoteUser
var WebNoteUser string
var CookieJar *cookiejar.Jar
// AppConfig - Application config struct
type AppConfig struct {
gorm.Model
// Section string `gorm:"type:varchar(128);unique_index:section_key"`
Key string `gorm:"type:varchar(128);unique_index:section_key"`
Val string `gorm:"type:text"`
}
// DbConn - Global DB connection
var DbConn *gorm.DB
// SetupConfigDB - SetupDB. This is the initial point of config setup. Note init() does not work if it relies
// on DbConn as at the time the DBPATH is not yet available
func SetupConfigDB() {
var err error
dbPath := os.Getenv("DBPATH")
// fmt.Printf("Use dbpath %v\n", dbPath)
DbConn, err = gorm.Open("sqlite3", dbPath)
if err != nil {
panic("failed to connect database")
}
DbConn.AutoMigrate(&AppConfig{})
DbConn.AutoMigrate(&Note{})
setupSQL := `
CREATE VIRTUAL TABLE IF NOT EXISTS note_fts USING fts5(title, datelog, content, content='notes', content_rowid='id');
CREATE TRIGGER IF NOT EXISTS notes_ai AFTER INSERT ON notes BEGIN
INSERT INTO note_fts(rowid, title, datelog, content) VALUES (new.id, new.title, new.datelog, new.content);
END;
CREATE TRIGGER IF NOT EXISTS notes_ad AFTER DELETE ON notes BEGIN
INSERT INTO note_fts(note_fts, rowid, title, datelog, content) VALUES('delete', old.id, old.title, old.datelog, old.content);
END;
CREATE TRIGGER IF NOT EXISTS notes_au AFTER UPDATE ON notes BEGIN
INSERT INTO note_fts(note_fts, rowid, title, datelog, content) VALUES('delete', old.id, old.title, old.datelog, old.content);
INSERT INTO note_fts(rowid, title, datelog, content) VALUES (new.id, new.title, new.datelog, new.content);
END;
`
DbConn.Exec(setupSQL)
// DbConn.Exec("CREATE INDEX IF NOT EXISTS iTextContent ON notes(content COLLATE NOCASE);")
// Example of loading a key dbpath
// if err = DbConn.Find(&Config, AppConfig{Key: "dbpath"}).Error; err != nil {
// log.Printf("Error can not load config table %v",err)
// }
// value := Config.Val
DateLayout, _ = GetConfig("date_layout")
WebNoteUser, _ = GetConfig("webnote_user")
CreateDataNoteLangFileExt()
CreateDataNoteListOfLanguageSupport()
}
// SetupDefaultConfig - Setup/reset default configuration set
func SetupDefaultConfig() {
DbConn.Unscoped().Exec("DELETE FROM app_configs;")
configSet := map[string]string{
"config_created": "",
"pnmain_win_pos": "2202:54",
"select_limit": "250",
"list_flags": "TODO<|>IMPORTANT<|>URGENT",
"recent_filter_cmd": "",
"window_size": "429x503",
"main_window_size": "300x291",
"date_layout": "02-01-2006 15:04:05 MST",
"webnote_url": "",
"debug": "false",
}
for key, val := range configSet {
fmt.Printf("Inserting %s - %s\n", key, val)
if e := DbConn.Create(&AppConfig{Key: key, Val: val}).Error; e != nil {
fmt.Printf("ERROR %v\n", e)
}
}
}
// GetConfig - by key and return value. Give second arg as default value.
func GetConfig(key ...string) (string, error) {
var cfg = AppConfig{}
err := DbConn.Find(&cfg, AppConfig{Key: key[0]}).Error
if err != nil {
if len(key) == 2 {
return key[1], nil
} else {
return "", err
}
} else {
return cfg.Val, err
}
}
// SetConfig - Set a config key with value
func SetConfig(key, val string) error {
var cfg = AppConfig{}
if e := DbConn.FirstOrInit(&cfg, AppConfig{Key: key}).Error; e != nil {
return e
}
cfg.Val = val
if e := DbConn.Save(&cfg).Error; e != nil {
return e
}
return nil
}
// DeleteConfig - delete the config key
func DeleteConfig(key string) error {
var cfg = AppConfig{}
if e := DbConn.Find(&cfg, AppConfig{Key: key}).Error; e != nil {
return e
}
return DbConn.Unscoped().Delete(&cfg).Error
}
// Populate some notes needed for data lookup - used by other part of the app
// Currently we store the language / file extention data but in the future we might store more
// This note is used to lookup Language => File Extention so we can save the note to file with correct extension in note-pad.go and note-search.go
func CreateDataNoteLangFileExt() {
note := Note{}
if e := DbConn.FirstOrInit(¬e, Note{Title: "CreateDataNoteLangFileExt"}).Error; e != nil {
fmt.Printf("INFO Can not create data note CreateDataNoteLangFileExt %s\n", e.Error())
return
}
defer DbConn.Save(¬e)
if note.Content == "" {
// Fetch it so we do not waste memory by adding this resource to go-bindata
jsonText, err := u.Curl("GET", "https://raw.githubusercontent.com/sunshine69/gnote/gtksourceview/CreateDataNoteLangFileExt.json", "", "", []string{})
if u.CheckErrNonFatal(err, "CreateDataNoteLangFileExt GET") != nil {
fmt.Println("Error fetching CreateDataNoteLangFileExt. You can manually search the note with title CreateDataNoteLangFileExt and insert the content yourself. The content is from the this repo project github")
return
}
note.Content = jsonText
datelog := time.Now().UnixNano()
note.Readonly, note.Datelog, note.Timestamp = 1, datelog, datelog
}
}
// Parse the language string that current gtksourceview support and save it to a note so we can check against it
// The list is created from this command on linux ls /usr/share/gtksourceview-3.0/language-specs/ | sed 's/.lang//'
func CreateDataNoteListOfLanguageSupport() {
note := Note{}
if e := DbConn.FirstOrInit(¬e, Note{Title: "CreateDataNoteListOfLanguageSupport"}).Error; e != nil {
fmt.Printf("INFO Can not create data note CreateDataNoteListOfLanguageSupport %s\n", e.Error())
return
}
defer DbConn.Save(¬e)
if note.Content == "" {
// Fetch it so we do not waste memory by adding this resource to go-bindata
jsonText, err := u.Curl("GET", "https://raw.githubusercontent.com/sunshine69/gnote/gtksourceview/CreateDataNoteListOfLanguageSupport.json", "", "", []string{})
if u.CheckErrNonFatal(err, "CreateDataNoteListOfLanguageSupport GET") != nil {
fmt.Println("Error fetching CreateDataNoteListOfLanguageSupport. You can manually search the note with title CreateDataNoteListOfLanguageSupport and insert the content yourself. The content is from the this repo project github")
return
}
note.Content = jsonText
datelog := time.Now().UnixNano()
note.Readonly, note.Datelog, note.Timestamp = 1, datelog, datelog
}
}