forked from tjhowse/cacheodon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatastore.go
More file actions
69 lines (60 loc) · 1.24 KB
/
datastore.go
File metadata and controls
69 lines (60 loc) · 1.24 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
package main
import (
"os"
"github.com/pelletier/go-toml/v2"
)
type searchTerms struct {
Latitude float32
Longitude float32
RadiusMeters int
AreaName string
IgnorePremium bool
}
type APIConfig struct {
// The URL of the Geocaching API.
GeocachingAPIURL string
HTTPProxyURL string
UnThrottle bool // Should we disable rate-limiting for this API?
}
type configStore struct {
Configuration APIConfig
SearchTerms searchTerms
DBFilename string
}
type config struct {
Filename string
Store configStore
}
// Write the current config out to a toml file.
func (c *config) Save() error {
b, err := toml.Marshal(c.Store)
if err != nil {
return err
}
return os.WriteFile(c.Filename, b, 0644)
}
// Load the current config from a toml file.
func (c *config) Load() error {
b, err := os.ReadFile(c.Filename)
if err != nil {
return err
}
return toml.Unmarshal(b, &c.Store)
}
func NewDatastore(filename string) (*config, error) {
c := &config{
Filename: filename,
}
if err := c.Load(); err != nil {
if os.IsNotExist(err) {
if err := c.Save(); err != nil {
return nil, err
}
}
}
// Set some defaults
if c.Store.DBFilename == "" {
c.Store.DBFilename = "cacheodon.sqlite3"
}
return c, nil
}