forked from dcposch/scramble
/
config.go
70 lines (57 loc) · 1.29 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
package main
import (
"encoding/json"
"io/ioutil"
"log"
"os"
"fmt"
)
type Config struct {
DbServer string
DbUser string
DbPassword string
DbCatalog string
SmtpMxHost string
SmtpPort int // internal, nginx handles TLS and forwards
HttpPort int // internal, nginx handles SSL and forwards
}
func GetConfig() *Config {
return &config
}
var defaultConfig = Config{
"127.0.0.1",
"scramble",
"scramble",
"scramble",
"dev.scramble.io",
8825,
8888,
}
var config Config
func init() {
configFile := os.Getenv("HOME") + "/.scramble/config.json"
// try to read configuration. if missing, write default
configBytes, err := ioutil.ReadFile(configFile)
if err != nil {
writeDefaultConfig(configFile)
fmt.Println("Config file written to ~/.scramble/config.json. Please edit & run again")
os.Exit(1)
return
}
// try to parse configuration. on error, die
err = json.Unmarshal(configBytes, &config)
if err != nil {
log.Panicf("Invalid configuration file %s: %v", configFile, err)
}
}
func writeDefaultConfig(configFile string) {
log.Printf("Creating default configration file %s", configFile)
configBytes, err := json.MarshalIndent(defaultConfig, "", " ")
if err != nil {
panic(err)
}
err = ioutil.WriteFile(configFile, configBytes, 0600)
if err != nil {
panic(err)
}
}