aboutsummaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
Diffstat (limited to 'config.go')
-rw-r--r--config.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..65e15ba
--- /dev/null
+++ b/config.go
@@ -0,0 +1,63 @@
1package main
2
3import (
4 "fmt"
5 "log"
6 "os"
7
8 "github.com/BurntSushi/toml"
9)
10
11type Config struct {
12 Database struct {
13 Hostname string `toml:"hostname"`
14 //Port int `toml:"port"`
15 Database string `toml:"database"`
16 User string `toml:"user"`
17 Password string `toml:"password"`
18 SSLEnabled bool `toml:ssl_enabled"`
19 } `toml:"database"`
20}
21
22func (c *Config) DBConnectionString() string {
23 var sslmode string
24 if c.Database.SSLEnabled {
25 sslmode = "enabled"
26 } else {
27 sslmode = "disabled"
28 }
29 return fmt.Sprintf("host=%s user=%s dbname=%s password=%s sslmode=%s",
30 c.Database.Hostname,
31 c.Database.User,
32 c.Database.Database,
33 c.Database.Password,
34 sslmode)
35}
36
37func LoadConfig() Config {
38 var config Config
39 var path string
40 log.Println("Loading config...")
41 for _, v := range []string{
42 "grilist.conf",
43 "~/grilist.conf",
44 "/etc/grilist.conf",
45 } {
46 log.Printf("Try \"%s\"...", v)
47 if _, err := os.Stat(v); err != nil {
48 log.Printf("Loading config at \"%s\" failed!", v)
49 } else {
50 log.Printf("Config file found!")
51 path = v
52 break
53 }
54 }
55 if path == "" {
56 log.Fatal("No config file found at the possible locations!")
57 }
58 if _, err := toml.DecodeFile(path, &config); err != nil {
59 log.Printf("%v", err)
60 log.Fatal("Fatal error while reading the config!")
61 }
62 return config
63}