From af2fa2adad464a27847e91b44e7cdc057fd623d2 Mon Sep 17 00:00:00 2001 From: Brandon Philips Date: Tue, 6 Aug 2013 18:35:49 -0700 Subject: [PATCH] bump(github.com/ccding/go-config-reader): fcf8cc3fb5100a2466c414cea91a4bb6344087fd --- .../ccding/go-config-reader/config/config.go | 57 +++++++++++++++++-- .../ccding/go-config-reader/example.conf | 3 + 2 files changed, 54 insertions(+), 6 deletions(-) diff --git a/third_party/github.com/ccding/go-config-reader/config/config.go b/third_party/github.com/ccding/go-config-reader/config/config.go index 50cf185be..75a48579e 100644 --- a/third_party/github.com/ccding/go-config-reader/config/config.go +++ b/third_party/github.com/ccding/go-config-reader/config/config.go @@ -18,10 +18,13 @@ package config import ( "bufio" + "errors" "os" "strings" ) +var commentPrefix = []string{"//", "#", ";"} + func Read(filename string) (map[string]string, error) { var res = map[string]string{} in, err := os.Open(filename) @@ -30,11 +33,19 @@ func Read(filename string) (map[string]string, error) { } scanner := bufio.NewScanner(in) line := "" + section := "" for scanner.Scan() { - if strings.HasPrefix(scanner.Text(), "//") { + if scanner.Text() == "" { continue } - if strings.HasPrefix(scanner.Text(), "#") { + if line == "" { + sec := checkSection(scanner.Text()) + if sec != "" { + section = sec + "." + continue + } + } + if checkComment(scanner.Text()) { continue } line += scanner.Text() @@ -42,13 +53,47 @@ func Read(filename string) (map[string]string, error) { line = line[:len(line)-1] continue } - sp := strings.SplitN(line, "=", 2) - if len(sp) != 2 { - continue + key, value, err := checkLine(line) + if err != nil { + return res, errors.New("WRONG: " + line) } - res[strings.TrimSpace(sp[0])] = strings.TrimSpace(sp[1]) + res[section+key] = value line = "" } in.Close() 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 +} diff --git a/third_party/github.com/ccding/go-config-reader/example.conf b/third_party/github.com/ccding/go-config-reader/example.conf index a5645cb95..706e7b1f1 100644 --- a/third_party/github.com/ccding/go-config-reader/example.conf +++ b/third_party/github.com/ccding/go-config-reader/example.conf @@ -5,3 +5,6 @@ cc = dd, 2 ejkl ijfadjfl # 12jfiahdoif dd = c \ oadi + + [test] + a = c c d