diff options
author | Runxi Yu <me@runxiyu.org> | 2024-12-29 18:40:22 +0000 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2024-12-29 18:40:22 +0000 |
commit | b6c8304364d1457fa7454da257f7ad3d71131b91 (patch) | |
tree | de85e19971aa6ac127abf35b80421ff8f6907727 /scfg/scfg.go | |
parent | Initial commit (diff) | |
download | go-lindenii-common-b6c8304364d1457fa7454da257f7ad3d71131b91.tar.gz go-lindenii-common-b6c8304364d1457fa7454da257f7ad3d71131b91.tar.zst go-lindenii-common-b6c8304364d1457fa7454da257f7ad3d71131b91.zip |
Add ~emersion/go-scfg as a subdirectory
Diffstat (limited to '')
-rw-r--r-- | scfg/scfg.go | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/scfg/scfg.go b/scfg/scfg.go new file mode 100644 index 0000000..6f20b49 --- /dev/null +++ b/scfg/scfg.go @@ -0,0 +1,55 @@ +// Package scfg parses and formats configuration files. +package scfg + +import ( + "fmt" +) + +// Block is a list of directives. +type Block []*Directive + +// GetAll returns a list of directives with the provided name. +func (blk Block) GetAll(name string) []*Directive { + l := make([]*Directive, 0, len(blk)) + for _, child := range blk { + if child.Name == name { + l = append(l, child) + } + } + return l +} + +// Get returns the first directive with the provided name. +func (blk Block) Get(name string) *Directive { + for _, child := range blk { + if child.Name == name { + return child + } + } + return nil +} + +// Directive is a configuration directive. +type Directive struct { + Name string + Params []string + + Children Block + + lineno int +} + +// ParseParams extracts parameters from the directive. It errors out if the +// user hasn't provided enough parameters. +func (d *Directive) ParseParams(params ...*string) error { + if len(d.Params) < len(params) { + return fmt.Errorf("directive %q: want %v params, got %v", d.Name, len(params), len(d.Params)) + } + for i, ptr := range params { + if ptr == nil { + continue + } + *ptr = d.Params[i] + } + return nil +} |