-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
127 lines (114 loc) · 2.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package config
import (
"fmt"
"io/ioutil"
"os"
"strings"
yaml "gopkg.in/yaml.v2"
)
type Fuse struct {
GitRepoDir string `yaml:"repo_dir"`
Debug bool
}
type Sshd struct {
ListenHost string `yaml:"host"`
ListenPort int32 `yaml:"port"`
PrivateKey string `yaml:"private_key"`
MaxClient int32 `yaml:"max_client"`
ShellPath string `yaml:"shell"`
}
type Syslog struct {
Protocol string
Host string
Level string
Tag string
}
type Log struct {
Local string
Level string
Syslog Syslog
}
type Environmental struct {
Sshd Sshd
Fuse Fuse
Log Log
}
type Config struct {
Development Environmental
Production Environmental
Test Environmental
}
var Current *Environmental
var Candidates = []string{
os.Getenv("PAGES_CONFIG"),
"/etc/pages.yml",
"/etc/pages.yaml",
"etc/pages.yml",
"etc/pages.yaml",
}
func Load() error {
config := Config{}
env := getEnvironment()
content, err := loadConfigFile()
if err != nil {
return err
}
err = yaml.Unmarshal(content, &config)
if err != nil {
return err
}
switch env {
case "development":
Current = &config.Development
case "test":
Current = &config.Test
case "production":
Current = &config.Production
default:
return fmt.Errorf("Invalid environment `%v`", env)
}
if Current.Sshd.ListenPort == 0 {
Current.Sshd.ListenPort = 22
}
if Current.Sshd.PrivateKey == "" {
return fmt.Errorf("Config Error: private key must be set")
}
if Current.Sshd.MaxClient == 0 {
Current.Sshd.MaxClient = 256
}
if Current.Sshd.ShellPath == "" {
Current.Sshd.ShellPath = "/bin/bash"
}
if Current.Log.Local == "" {
Current.Log.Local = "stderr"
}
if Current.Log.Level == "" {
Current.Log.Level = "DEBUG"
}
Current.Log.Level = strings.ToUpper(Current.Log.Level)
if Current.Log.Syslog.Level == "" {
Current.Log.Syslog.Level = "DEBUG"
}
Current.Log.Syslog.Level = strings.ToUpper(Current.Log.Syslog.Level)
return nil
}
func loadConfigFile() ([]byte, error) {
for _, candidate := range Candidates {
if len(candidate) == 0 {
continue
}
content, err := ioutil.ReadFile(candidate)
if err != nil {
continue
}
return content, nil
}
return nil, fmt.Errorf("Failed to load config file")
}
func getEnvironment() (env string) {
env = os.Getenv("PAGES_ENV")
if env == "" {
env = "development"
}
return
}