forked from minotar/imgd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configuration.go
70 lines (60 loc) · 1.34 KB
/
configuration.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
package main
import (
"code.google.com/p/gcfg"
"io"
"os"
)
const (
// The file we read from
CONFIG_FILE = "config.gcfg"
// The example file kept in version control. We'll copy and load from this
// by default.
CONFIG_EXAMPLE = "config.example.gcfg"
)
type Configuration struct {
Server struct {
Address string
Cache string
StatisticsEnabled bool
}
Redis struct {
Address string
Ttl string
Auth string
Prefix string
PoolSize int
}
}
// Reads the configuration from the config file, copying a config into
// place from the example if one does not yet exist.
func (c *Configuration) load() error {
err := c.ensureConfigExists()
if err != nil {
return err
}
return gcfg.ReadFileInto(c, CONFIG_FILE)
}
// Creates the config.json if it does not exist.
func (c *Configuration) ensureConfigExists() error {
if _, err := os.Stat(CONFIG_FILE); os.IsNotExist(err) {
return copyFile(CONFIG_EXAMPLE, CONFIG_FILE)
} else {
return nil
}
}
// Copies *only the contents* of one file to a new path.
func copyFile(src string, dest string) error {
original, err := os.Open(src)
if err != nil {
return err
}
defer original.Close()
destination, err := os.Create(dest)
if err != nil {
return err
}
defer destination.Close()
// do the actual work
_, err = io.Copy(destination, original)
return err
}