Documentation
¶
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Parse ¶
Parse reads the input and calls callback whenever it finishes reading a command.
Example ¶
package main
import (
"fmt"
"log"
"strings"
"61924.nl/g/config"
)
type configEntry struct {
Key string
Values []string
}
func main() {
input := `
prefork 2
server "default" {
listen on * port 80
}
types {
text/css css
text/html html htm
text/plain txt
image/gif gif
image/jpeg jpeg jpg
image/png png
application/javascript js
application/xml xml
}
server "www.example.com" {
alias "example.com"
listen on * port 80
listen on * tls port 443
root "/htdocs/www.example.com"
}
server "www.a.example.com" {
listen on 203.0.113.1 port 80
root "/htdocs/www.a.example.com"
}
`
entries := []configEntry{}
err := config.Parse(input, func(namespace, params []string) {
key := strings.Join(namespace, ".")
val := make([]string, len(params))
copy(val, params)
entries = append(entries, configEntry{
Key: key,
Values: val,
})
})
if err != nil {
log.Fatal(err)
}
padding := maxKeyLen(entries) + 4
for _, e := range entries {
padding := strings.Repeat(" ", padding-len(e.Key))
fmt.Printf("%s%s%q\n", e.Key, padding, e.Values)
}
}
func maxKeyLen(entries []configEntry) int {
max := 0
for _, e := range entries {
keyLen := len(e.Key)
if keyLen > max {
max = keyLen
}
}
return max
}
Output: prefork ["2"] server ["\"default\""] server.listen ["on" "*" "port" "80"] types [] types.text/css ["css"] types.text/html ["html" "htm"] types.text/plain ["txt"] types.image/gif ["gif"] types.image/jpeg ["jpeg" "jpg"] types.image/png ["png"] types.application/javascript ["js"] types.application/xml ["xml"] server ["\"www.example.com\""] server.alias ["\"example.com\""] server.listen ["on" "*" "port" "80"] server.listen ["on" "*" "tls" "port" "443"] server.root ["\"/htdocs/www.example.com\""] server ["\"www.a.example.com\""] server.listen ["on" "203.0.113.1" "port" "80"] server.root ["\"/htdocs/www.a.example.com\""]
Types ¶
Click to show internal directories.
Click to hide internal directories.