-
Notifications
You must be signed in to change notification settings - Fork 0
/
PropsReader.go
169 lines (147 loc) · 4.06 KB
/
PropsReader.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package propsReader
import (
"bytes"
"errors"
"github.com/guoapeng/props/utils"
"log"
"os"
"os/exec"
"os/user"
"regexp"
"runtime"
"strings"
)
type AppConfigProperties interface {
Get(key string) string
}
type appConfigProperties struct {
props map[string]string
}
func (appConf *appConfigProperties) Get(key string) string {
return appConf.props[key]
}
type AppConfigFactory struct {
propertyFile string
SystemDir string
HomeDir string
OsUtils utils.OsUtils
BufioUtils utils.BufioUtils
}
func Home() (string, error) {
user, err := user.Current()
if nil == err {
return user.HomeDir, nil
}
// cross platform compile support
if "windows" == runtime.GOOS {
return homeWindows()
}
// Unix-like system, so just assume Unix
return homeUnix()
}
func homeUnix() (string, error) {
// First prefer the HOME environmental variable
if home := os.Getenv("HOME"); home != "" {
return home, nil
}
// If that fails, try the shell
var stdout bytes.Buffer
cmd := exec.Command("sh", "-c", "eval echo ~$USER")
cmd.Stdout = &stdout
if err := cmd.Run(); err != nil {
return "", err
}
result := strings.TrimSpace(stdout.String())
if result == "" {
return "", errors.New("blank output when reading home directory")
}
return result, nil
}
func homeWindows() (string, error) {
drive := os.Getenv("HOMEDRIVE")
path := os.Getenv("HOMEPATH")
home := drive + path
if drive == "" || path == "" {
home = os.Getenv("USERPROFILE")
}
if home == "" {
return "", errors.New("HOMEDRIVE, HOMEPATH, and USERPROFILE are blank")
}
return home, nil
}
func NewFactory(appName string, propertyFile string) *AppConfigFactory {
if homedir, err := Home(); err == nil {
return &AppConfigFactory{OsUtils: utils.NewOsUtils(), BufioUtils: utils.NewBufioUtils(), SystemDir: "/etc/" + appName + "/", HomeDir: homedir + "/." + appName + "/", propertyFile: propertyFile}
} else {
log.Fatal("can't find home directory")
}
return nil
}
func (factory *AppConfigFactory) New() (AppConfigProperties, error) {
var appConfigFile string
appConfigFile = factory.propertyFile
if len(appConfigFile) == 0 {
log.Fatal("mandatory property file is not set")
}
systemProps, _ := factory.ReadPropertiesFile(factory.SystemDir + appConfigFile)
homeProps1, _ := factory.ReadPropertiesFile(factory.HomeDir + appConfigFile)
for k, v := range homeProps1 {
systemProps[k] = v
}
return &appConfigProperties{props: systemProps}, nil
}
func (factory *AppConfigFactory) ReadPropertiesFile(filename string) (map[string]string, error) {
config := map[string]string{}
if len(filename) == 0 {
return config, nil
}
if exists, err := factory.OsUtils.PathExists(filename); !exists || err != nil {
return config, err
}
file, err := factory.OsUtils.Open(filename)
if err != nil {
log.Fatal(err)
return nil, err
}
defer file.Close()
scanner := factory.BufioUtils.NewScanner(file)
for scanner.Scan() {
line := scanner.Text()
preProcessedLine := strings.TrimSpace(string(line))
if strings.HasPrefix(preProcessedLine, "source") {
position := strings.Index(preProcessedLine, "source")
if tempProps, err := factory.ReadPropertiesFile(strings.TrimSpace(preProcessedLine[position+6:])); err == nil {
for k, v := range tempProps {
config[k] = v
}
}
}
if strings.HasPrefix(preProcessedLine, "#") {
continue
}
if equal := strings.Index(preProcessedLine, "="); equal >= 0 {
if key := strings.TrimSpace(preProcessedLine[:equal]); len(key) > 0 {
value := ""
if len(preProcessedLine) > equal {
value = strings.TrimSpace(preProcessedLine[equal+1:])
value = factory.replaceEnvVariables(value)
}
config[key] = value
}
}
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
return nil, err
}
return config, nil
}
func (factory *AppConfigFactory) replaceEnvVariables(original string) string {
re := regexp.MustCompile("\\$\\{([A-Za-z0-9_]+)\\}")
newStr := original
matched := re.FindAllStringSubmatch(original, -1)
for _, match := range matched {
newStr = strings.ReplaceAll(newStr, match[0], factory.OsUtils.Getenv(match[1]))
}
return newStr
}