-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.go
87 lines (79 loc) · 2 KB
/
db.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
package main
import (
"container/list"
"fmt"
"github.com/tidwall/buntdb"
"github.com/webview/webview"
)
func setupDB(w webview.WebView) (*buntdb.DB, error) {
db, err := buntdb.Open("influxConfigs.db")
if err != nil {
createAlertDialog(w, "could not open db", err.Error())
}
return db, err
}
func getExists(connections *list.List, host string) bool {
exists := false
for connection := connections.Front(); connection != nil; connection = connection.Next() {
if host == connection.Value {
exists = true
}
}
return exists
}
func addInfluxDBConfig(w webview.WebView, db *buntdb.DB, host string) error {
connections, _ := getInfluxDBConfigs(w, db)
exists := false
for connection := connections.Front(); connection != nil; connection = connection.Next() {
if host == connection.Value {
exists = true
}
}
if !exists {
err := db.Update(func(tx *buntdb.Tx) error {
_, _, err := tx.Set(host, host, nil)
return err
})
if err != nil {
createAlertDialog(w, "Could not save to database", err.Error())
} else {
if host != "http://localhost:8086" {
createAlertDialog(w, "Config saved", host)
}
}
return err
} else {
if host != "http://localhost:8086" {
createAlertDialog(w, "Hostname exists already", host)
}
}
return nil
}
func deleteInfluxDBConfig(w webview.WebView, db *buntdb.DB, host string) error {
connections, _ := getInfluxDBConfigs(w, db)
exists := getExists(connections, host)
if exists {
err := db.Update(func(tx *buntdb.Tx) error {
_, err := tx.Delete(host)
return err
})
if err == nil {
createAlertDialog(w, "Config deleted", host)
} else {
createAlertDialog(w, "Could not delete", err.Error())
}
}
return nil
}
func getInfluxDBConfigs(w webview.WebView, db *buntdb.DB) (*list.List, error) {
connections := list.New()
err := db.View(func(tx *buntdb.Tx) error {
tx.Ascend("", func(key, val string) bool {
connections.PushBack(val)
fmt.Printf("%s %s\n", key, val)
return true
})
return nil
})
return connections, err
}