2018-06-21 23:21:10 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func configFileLoad(filepath string) error {
|
|
|
|
file, err := os.Open(filepath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer file.Close()
|
|
|
|
|
2018-06-21 23:33:32 +08:00
|
|
|
num := 0
|
2018-06-21 23:21:10 +08:00
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
2018-06-21 23:33:32 +08:00
|
|
|
num += 1
|
|
|
|
|
2018-06-21 23:28:26 +08:00
|
|
|
line := strings.TrimSpace(scanner.Text())
|
2018-06-21 23:29:29 +08:00
|
|
|
if line == "" {
|
|
|
|
continue
|
|
|
|
}
|
2018-06-21 23:21:10 +08:00
|
|
|
|
2018-06-21 23:29:42 +08:00
|
|
|
if strings.HasPrefix(line, "#") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-06-21 23:21:10 +08:00
|
|
|
i := strings.Index(line, "=")
|
|
|
|
if i == -1 {
|
2018-06-21 23:33:32 +08:00
|
|
|
logger.Errorf("%s: line %d: neither a comment nor a valid setting", filepath, num)
|
|
|
|
return errorInvalidConfigFile
|
2018-06-21 23:21:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
key := line[:i]
|
|
|
|
value := line[i+1:]
|
|
|
|
|
|
|
|
if !strings.HasPrefix(key, "COMMENTO_") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-06-21 23:38:28 +08:00
|
|
|
if os.Getenv(key[9:]) != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2018-06-21 23:21:10 +08:00
|
|
|
os.Setenv(key[9:], value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|