initial
This commit is contained in:
72
parser.go
Normal file
72
parser.go
Normal file
@@ -0,0 +1,72 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
configKeyValueRe = regexp.MustCompile(`(\w+) *= *(.+)\b[\t| |\n]*`)
|
||||
configOptionRe = regexp.MustCompile(`(\w+)`)
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Values map[string]string
|
||||
}
|
||||
|
||||
func (c *Config) Get(key string) (s string) {
|
||||
if val, ok := c.Values[key]; ok {
|
||||
s = val
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Config) GetBool(key string) (b bool) {
|
||||
_, b = c.Values[key]
|
||||
return
|
||||
}
|
||||
|
||||
func (c *Config) GetInt(key string) (n int) {
|
||||
if val, ok := c.Values[key]; ok {
|
||||
num, err := strconv.Atoi(val)
|
||||
if err == nil {
|
||||
n = num
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func Parse(filename string) (*Config, error) {
|
||||
file, err := os.Open(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer file.Close()
|
||||
c := parseReader(file)
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func parseReader(r io.Reader) *Config {
|
||||
var c Config
|
||||
c.Values = make(map[string]string)
|
||||
scanner := bufio.NewScanner(r)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if strings.HasPrefix(line, "#") {
|
||||
continue
|
||||
}
|
||||
switch {
|
||||
case configKeyValueRe.MatchString(line):
|
||||
kvpair := configKeyValueRe.FindStringSubmatch(line)
|
||||
c.Values[kvpair[1]] = kvpair[2]
|
||||
case configOptionRe.MatchString(line):
|
||||
opt := configOptionRe.FindString(line)
|
||||
c.Values[opt] = ""
|
||||
}
|
||||
}
|
||||
return &c
|
||||
}
|
31
parser_test.go
Normal file
31
parser_test.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package conf
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var testConf = []byte(`network
|
||||
server=10.0.0.10
|
||||
port=10000
|
||||
token=test
|
||||
editor=vim
|
||||
color`)
|
||||
|
||||
func TestParser(t *testing.T) {
|
||||
r := bytes.NewReader(testConf)
|
||||
c := parseReader(r)
|
||||
if c.Get("token") != "test" {
|
||||
fmt.Println("failed finding key value")
|
||||
t.Fail()
|
||||
}
|
||||
if c.GetInt("port") != 10000 {
|
||||
fmt.Println("failed finding int")
|
||||
t.Fail()
|
||||
}
|
||||
if c.GetBool("color") != true {
|
||||
fmt.Println("failed finding bool")
|
||||
t.Fail()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user