-
Notifications
You must be signed in to change notification settings - Fork 6
/
config.go
69 lines (56 loc) · 1.68 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
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"syscall"
"github.com/BurntSushi/toml"
)
type (
config struct {
StatusPath string `toml:"status"`
ListenPath string `toml:"listen"`
URL string `toml:"url"`
}
)
var (
configs map[string]config
)
const (
// configFilename is the name of the configuration file loaded from the
// user's home directory.
configFilename = ".phpfpmtop.conf"
// defaultConfig will be written to configFilename if none is found.
defaultConfig = `# Default section will be used if there's no arguments given to phpfpmtop.
[default]
listen = "/var/run/php5-fpm.sock" # The value of the "listen" option in the PHP-FPM-pool config.
status = "/status" # The value of the "status" option.
# If you define an URL, phpfpmtop will try to retrieve the status page via
# HTTP. FPM options will be ignored.
#url = "https://www.example.com/phpfpm_status"
`
)
func init() {
home := os.Getenv("HOME")
if runtime.GOOS == "windows" {
home = os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
if home == "" {
home = os.Getenv("USERPROFILE")
}
}
p := path.Join(home, configFilename)
_, err := toml.DecodeFile(p, &configs)
if e, ok := err.(*os.PathError); ok && e.Err == syscall.ENOENT {
// The configuration file was not found. Let's provide a sane
// default for php5-fpm.
configs = make(map[string]config)
ioutil.WriteFile(p, []byte(defaultConfig), 0640)
fmt.Printf("New configuration file written to \033[33m%s\033[0m. Please verify, and restart phpfpmtop.\n\n\033[33m%s\033[0m\n", p, defaultConfig)
os.Exit(1)
} else if err != nil {
fmt.Printf("Error when opening %s: %s", p, err.Error())
os.Exit(1)
}
}