go-scfg – git.sr.ht/~emersion/go-scfg Index | Examples | Files

package scfg

import "git.sr.ht/~emersion/go-scfg"

Package scfg parses and formats configuration files.




func Write

func Write(w io.Writer, blk Block) error

Write writes a parsed configuration to the provided io.Writer.


type Block

type Block []*Directive

Block is a list of directives.

func Load

func Load(path string) (Block, error)

Load loads a configuration file.

func Read

func Read(r io.Reader) (Block, error)

Read parses a configuration file from an io.Reader.

func (Block) Get

func (blk Block) Get(name string) *Directive

Get returns the first directive with the provided name.

func (Block) GetAll

func (blk Block) GetAll(name string) []*Directive

GetAll returns a list of directives with the provided name.

type Decoder

type Decoder struct {
	// contains filtered or unexported fields

Decoder reads and decodes an scfg document from an input stream.



	var data struct {
		Foo int `scfg:"foo"`
		Bar struct {
			Param string `scfg:",param"`
			Baz   string `scfg:"baz"`
		} `scfg:"bar"`

	raw := `foo 42
bar asdf {
	baz hello

	r := strings.NewReader(raw)
	if err := scfg.NewDecoder(r).Decode(&data); err != nil {

	fmt.Printf("Foo = %v\n", data.Foo)
	fmt.Printf("Bar.Param = %v\n", data.Bar.Param)
	fmt.Printf("Bar.Baz = %v\n", data.Bar.Baz)

	// Output:
	// Foo = 42
	// Bar.Param = asdf
	// Bar.Baz = hello


Foo = 42
Bar.Param = asdf
Bar.Baz = hello

func NewDecoder

func NewDecoder(r io.Reader) *Decoder

NewDecoder returns a new decoder which reads from r.

func (*Decoder) Decode

func (dec *Decoder) Decode(v interface{}) error

Decode reads scfg document from the input and stores it in the value pointed to by v.

If v is nil or not a pointer, Decode returns an error.

Blocks can be unmarshaled to:

Duplicate directives are not allowed, unless the struct field or map value is a slice of values representing a directive: structs or maps.

Directives can be unmarshaled to:

The decoding of each struct field can be customized by the format string stored under the "scfg" key in the struct field's tag. The tag contains the name of the field possibly followed by a comma-separated list of options. The name may be empty in order to specify options without overriding the default field name. As a special case, if the field name is "-", the field is ignored. The "param" option specifies that directive parameters are stored in this field (the name must be empty).

type Directive

type Directive struct {
	Name   string
	Params []string

	Children Block
	// contains filtered or unexported fields

Directive is a configuration directive.

func (*Directive) ParseParams

func (d *Directive) ParseParams(params ...*string) error

ParseParams extracts parameters from the directive. It errors out if the user hasn't provided enough parameters.

Source Files

reader.go scfg.go struct.go unmarshal.go writer.go

v0.0.0-20240128091534-2ae16e782082 (latest)
Jan 28, 2024
10 packages
Last checked
4 months ago

Tools for package owners.