Skip to content

Commit

Permalink
support comment in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsonz committed Jul 7, 2017
1 parent f057e74 commit a4a7446
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion sni.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"bufio"
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
Expand Down Expand Up @@ -237,7 +238,29 @@ Next:
func parseConfig() {
conf, err := ioutil.ReadFile(configFileName)
checkErr("read config file error: ", err, Error)
err = json.Unmarshal(conf, &config)

var lines []string
for _, line := range strings.Split(strings.Replace(string(conf), "\r\n", "\n", -1), "\n") {
line = strings.TrimSpace(line)
if !strings.HasPrefix(line, "//") && line != "" {
lines = append(lines, line)
}
}

var b bytes.Buffer
for i, line := range lines {
if len(lines)-1 > i {
nextLine := lines[i+1]
if nextLine == "]" || nextLine == "]," || nextLine == "}" || nextLine == "}," {
if strings.HasSuffix(line, ",") {
line = strings.TrimSuffix(line, ",")
}
}
}
b.WriteString(line)
}

err = json.Unmarshal(b.Bytes(), &config)
checkErr("parse config file error: ", err, Error)
}

Expand Down

0 comments on commit a4a7446

Please sign in to comment.