-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
63 lines (55 loc) · 1.2 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
package puppetquery
import (
"log"
"os"
"path/filepath"
"github.com/nightlyone/ini"
)
// Locations of site/global config and per user config.
var (
GlobalConfig = "/etc/puppetquery/config.ini"
UserConfig = ".config/puppetquery/config.ini"
)
// Endpoint, if we didn't configure any
var DefaultEndpoint = "http://localhost:8080"
/// This part does the puppet endpoint autoconfiguration
var config *ini.File
var endpoint = DefaultEndpoint
func init() {
config = loadConfig()
if puppetdb, ok := config.Global["url"]; ok {
endpoint = puppetdb
}
}
func loadConfig() *ini.File {
if home := os.Getenv("HOME"); home != "" {
c, err := tryLoadConfig(filepath.Join(home, UserConfig))
if err != nil {
log.Println("ERROR: reading per user config: ", err)
return new(ini.File)
}
if c != nil {
return c
}
}
c, err := tryLoadConfig(GlobalConfig)
if err != nil {
log.Println("ERROR: reading global config: ", err)
return new(ini.File)
}
if c != nil {
return c
}
return new(ini.File)
}
func tryLoadConfig(filename string) (*ini.File, error) {
c, err := ini.ReadFile(filename)
switch {
case err == nil:
return c, nil
case os.IsNotExist(err):
return nil, nil
default:
return nil, err
}
}