-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
146 lines (108 loc) · 2.88 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
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"os/user"
"path/filepath"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
var Config TFConfig
var DB *sql.DB
const ConfigDirName = ".tf_data"
type TFConfig struct {
DBPath string
}
func GetHomeDirPath() (string, error) {
currentUser, err := user.Current()
if err != nil {
return "", err
}
return currentUser.HomeDir, nil
}
func LoadConfig() error {
homeDir, err := GetHomeDirPath()
if err != nil {
return err
}
configDirPath := filepath.Join(homeDir, ConfigDirName)
//Create folder if needed
if _, err = os.Stat(configDirPath); os.IsNotExist(err) {
fmt.Println("No config folder detected...\nCreated config folder.")
os.Mkdir(configDirPath, 0770)
}
configFilePath := filepath.Join(configDirPath, "config.json")
//create config if needed
if _, err = os.Stat(configFilePath); os.IsNotExist(err) {
fmt.Println("No config file detected...\nCreating config file.")
err = CreateConfig(configFilePath)
if err != nil {
return err
}
}
//Load config
configFile, err := os.Open(configFilePath)
if err != nil {
fmt.Println("Error loading config file!")
return err
}
defer configFile.Close()
configBufferedReader := bufio.NewReader(configFile)
json.NewDecoder(configBufferedReader).Decode(&Config)
//Load DB
dbFilePath := filepath.Join(configDirPath, Config.DBPath)
err = LoadDB(dbFilePath)
return err
}
func CreateConfig(path string) error {
baseConfig := TFConfig{DBPath: "db.sqlite3"}
configFile, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0660)
if err != nil {
fmt.Println("Error creating config.")
return err
}
defer configFile.Close()
configBufferedWriter := bufio.NewWriter(configFile)
defer configBufferedWriter.Flush()
json.NewEncoder(configBufferedWriter).Encode(&baseConfig)
return nil
}
func LoadDB(path string) error {
var err error
DB, err = sql.Open("sqlite3", path)
if err != nil {
fmt.Println("Error loading database.")
return err
}
statement, err := DB.Prepare("CREATE TABLE IF NOT EXISTS files (id INTEGER PRIMARY KEY, filepath TEXT NOT NULL)")
if err != nil {
fmt.Println("SQL error.")
return err
}
statement.Exec()
statement.Close()
statement, err = DB.Prepare("CREATE TABLE IF NOT EXISTS tags (id INTEGER PRIMARY KEY, tag TEXT NOT NULL)")
if err != nil {
fmt.Println("SQL error.")
return err
}
statement.Exec()
statement.Close()
statement, err = DB.Prepare("CREATE TABLE IF NOT EXISTS filetags (tag_id INTEGER NOT NULL, file_id INTEGER NOT NULL, PRIMARY KEY (tag_id, file_id))")
if err != nil {
fmt.Println("SQL error.")
return err
}
statement.Exec()
statement.Close()
statement, err = DB.Prepare("CREATE TABLE IF NOT EXISTS tagchildren (tag_id INTEGER NOT NULL, child_id INTEGER NOT NULL, PRIMARY KEY (tag_id, child_id))")
if err != nil {
fmt.Println("SQL error.")
return err
}
statement.Exec()
statement.Close()
return nil
}