-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
78 lines (66 loc) · 1.66 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"bufio"
"fmt"
"os"
"regexp"
"strings"
)
type Options struct {
confFile string
}
type Config struct {
server string
address string
port string
directory string
size uint32
logfile string
}
func (c Config) String() string {
var s string
s += "Server name: " + c.server + "\n"
s += "Listening on: " + c.address + ":" + c.port + "\n"
s += "Paste directory: " + c.directory + "\n"
s += "Max size: " + string(c.size) + "\n"
s += "Log file: " + c.logfile + "\n"
return s
}
func parseConfig(fName string, c *Config) error {
f, err := os.Open(fName)
if err != nil {
return err
}
r := bufio.NewScanner(f)
line := 0
for r.Scan() {
s := r.Text()
line += 1
if matched, _ := regexp.MatchString("^([ \t]*)$", s); matched != true {
if matched, _ := regexp.MatchString("^#", s); matched != true {
if matched, _ := regexp.MatchString("^([a-zA-Z]+)=.*", s); matched == true {
fields := strings.Split(s, "=")
switch strings.Trim(fields[0], " \t\"") {
case "server":
c.server = strings.Trim(fields[1], " \t\"")
case "address":
c.address = strings.Trim(fields[1], " \t\"")
case "port":
c.port = strings.Trim(fields[1], " \t\"")
case "directory":
c.directory = strings.Trim(fields[1], " \t\"")
case "logfile":
c.logfile = strings.Trim(fields[1], " \t\"")
default:
fmt.Fprintf(os.Stderr, "Error reading config file %s at line %d: unknown variable '%s'\n",
fName, line, fields[0])
}
} else {
fmt.Fprintf(os.Stderr, "Error reading config file %s at line %d: unknown statement '%s'\n",
fName, line, s)
}
}
}
}
return nil
}