-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
55 lines (44 loc) · 949 Bytes
/
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
package main
import (
"fmt"
"os"
"gopkg.in/gcfg.v1"
)
const BASE string = "fireman"
const CONFIGNAME string = BASE + ".conf"
type Config struct {
Main struct {
Server string
Key string
}
}
var cfg Config
var cfgfile *string
func (conf *Config) Save() error {
f, err := os.Create(*cfgfile)
if err != nil {
return err
}
f.WriteString("[main]\n")
fmt.Fprintf(f, "server = %s\n", cfg.Main.Server)
fmt.Fprintf(f, "key = %s\n", cfg.Main.Key)
f.Close()
os.Chmod(*cfgfile, 0700)
return nil
}
func loadConfig(name *string) {
cfg.Main.Server = "http://localhost:9090/plugins/userService/"
cfg.Main.Key = "<key>"
reloadConfig(name)
}
func reloadConfig(name *string) {
if !exists(*name) {
p("Configuration file %s does not exist. Generating a default file.", *name)
p("Creating default %s", *name)
cfg.Save()
}
err := gcfg.ReadFileInto(&cfg, *name)
if err != nil {
f("Configuration error: %s", err.Error())
}
}