mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
bump(github.com/ccding/go-config-reader): fcf8cc3fb5100a2466c414cea91a4bb6344087fd
This commit is contained in:
parent
f746b1e4f1
commit
af2fa2adad
@ -18,10 +18,13 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var commentPrefix = []string{"//", "#", ";"}
|
||||||
|
|
||||||
func Read(filename string) (map[string]string, error) {
|
func Read(filename string) (map[string]string, error) {
|
||||||
var res = map[string]string{}
|
var res = map[string]string{}
|
||||||
in, err := os.Open(filename)
|
in, err := os.Open(filename)
|
||||||
@ -30,11 +33,19 @@ func Read(filename string) (map[string]string, error) {
|
|||||||
}
|
}
|
||||||
scanner := bufio.NewScanner(in)
|
scanner := bufio.NewScanner(in)
|
||||||
line := ""
|
line := ""
|
||||||
|
section := ""
|
||||||
for scanner.Scan() {
|
for scanner.Scan() {
|
||||||
if strings.HasPrefix(scanner.Text(), "//") {
|
if scanner.Text() == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if strings.HasPrefix(scanner.Text(), "#") {
|
if line == "" {
|
||||||
|
sec := checkSection(scanner.Text())
|
||||||
|
if sec != "" {
|
||||||
|
section = sec + "."
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if checkComment(scanner.Text()) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
line += scanner.Text()
|
line += scanner.Text()
|
||||||
@ -42,13 +53,47 @@ func Read(filename string) (map[string]string, error) {
|
|||||||
line = line[:len(line)-1]
|
line = line[:len(line)-1]
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
sp := strings.SplitN(line, "=", 2)
|
key, value, err := checkLine(line)
|
||||||
if len(sp) != 2 {
|
if err != nil {
|
||||||
continue
|
return res, errors.New("WRONG: " + line)
|
||||||
}
|
}
|
||||||
res[strings.TrimSpace(sp[0])] = strings.TrimSpace(sp[1])
|
res[section+key] = value
|
||||||
line = ""
|
line = ""
|
||||||
}
|
}
|
||||||
in.Close()
|
in.Close()
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func checkSection(line string) string {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
lineLen := len(line)
|
||||||
|
if lineLen < 2 {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if line[0] == '[' && line[lineLen-1] == ']' {
|
||||||
|
return line[1 : lineLen-1]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkLine(line string) (string, string, error) {
|
||||||
|
key := ""
|
||||||
|
value := ""
|
||||||
|
sp := strings.SplitN(line, "=", 2)
|
||||||
|
if len(sp) != 2 {
|
||||||
|
return key, value, errors.New("WRONG: " + line)
|
||||||
|
}
|
||||||
|
key = strings.TrimSpace(sp[0])
|
||||||
|
value = strings.TrimSpace(sp[1])
|
||||||
|
return key, value, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkComment(line string) bool {
|
||||||
|
line = strings.TrimSpace(line)
|
||||||
|
for p := range commentPrefix {
|
||||||
|
if strings.HasPrefix(line, commentPrefix[p]) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
@ -5,3 +5,6 @@ cc = dd, 2 ejkl ijfadjfl
|
|||||||
# 12jfiahdoif
|
# 12jfiahdoif
|
||||||
dd = c \
|
dd = c \
|
||||||
oadi
|
oadi
|
||||||
|
|
||||||
|
[test]
|
||||||
|
a = c c d
|
||||||
|
Loading…
x
Reference in New Issue
Block a user