~pkal/dirconf

ref: 5067334a87eb9bb5c80398557f0c9d7c2b1f8636 dirconf/README.md -rw-r--r-- 2.2 KiB
5067334a — Philip K fixed compilation issues 2 years ago

#dirconf

Dirconf implements Go library for a simple configuration format, that instead of storing everything in a file, uses the file system as a hierarchical key-value storage.

#Example

import dc "git.sr.ht/~zge/dirconf"

func main() {
	var name string
	var age int

	conf := dc.Conf{
		&dc.String{Ptr: &name, Must: true, Key: "name"},
		&dc.Int{Ptr: &age, Key: "person/age"},
		&dc.Bool{OnSet: someFunc, Key: "test/value"},
	}

	err := conf.Parse()
	if err != nil {
		// handle error
	}
}

#Usage

The main two steps are:

  1. Listing all "Options" in a dirconf.Conf variable, which in turn is just a type-aliased slice of dirconf.Option objects. Such objects are dirconf.String, dirconf.Int, dirconf.Func, ...

  2. With all options listed, the Parse method (or ParseIn, if you want to manually specify the directory that should be used) will try to apply the file structure to the listed options.

    Parse will by default use ~/.config/[name of current binary] as the directory.

Most Options have a few properties in common, although the type system doesn't recognise this:

  • Ptr: A pointer to a variable that should be set to the parsed value. If nil (by default), it's ignored
  • Must: A Boolean value indicating that this value must value must have a corresponding file in the directory. If not set, an error will be returned from Parse (dirconf.ErrNoValueFound).
  • OnSet: Like Ptr, but will instead call a function with the value that has been found. Will be ignored if nil (by default).
  • Default: Value to use if nothing is found. Doesn't override Must. Not too useful for Ptr, more so for OnSet.
  • Key: A slash (/) delimited string that describes the local path the file for the value should be found in, within the configuration directory.

The auxiliary method WriteSchema, which is called by Parse and ParseIn by default, will write a file in the configuration directory named .schema, that specifies all options. This can be parsed by configuration editors, to give an overview of all possible options.

Dirconf is distributed under CC0. See LICENSE for more details