config

package module
v0.0.0-...-cf9b1b2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 30, 2019 License: ISC Imports: 2 Imported by: 0

README

Config

Config is a small, low-level package for reading configuration. The syntax if very simple and flexible, it resembles the syntax of httpd's configuration file syntax.

The package does no IO and does not load the configuration into a map or struct. Instead a callback must be provided, which is called for every entry in the given configuration.

See example_test.go for a complete example.

NOTICE: Config is useful, but still in development. There might be bug here and there. :)

Installation

go get -u 61924.nl/g/config

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(input string, callback CommandFn) error

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

type CommandFn

type CommandFn func([]string, []string)

CommandFn is called whenever the parser finished reading a command.

It's arguments are the current namespace and parameters (excluding blocks) as a string slice.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL