Documentation ¶
Overview ¶
Package cfg implements a library to access configuration data in a JSON file.
All hamradio tools share the same file in the same directory for configuration data: ~/.config/hamradio/conf.json
Index ¶
Constants ¶
const DefaultDirectory = "~/.config/hamradio"
DefaultDirectory is the place where all hamradio tools store their configuration locally.
const DefaultFilename = "conf.json"
DefaultFilename is the default name of the configuration file that is used by all hamradio tools, relative to the configuration directory.
Variables ¶
This section is empty.
Functions ¶
func Directory ¶
Directory returns the path of the configuration directory as absolute path. If the given path is the empty string, the default directory is returned.
func Exists ¶
Exists returns true if the given file exists in the given path. If the path is the empty string, the default configuration directory is used. If the given filename is the empty string, the default filename is used.
func LoadJSON ¶
LoadJSON loads JSON data from the given file in the given path and unmarshals it into the given data object. If the path is the empty string, the default configuration directory is used. If the given filename is the empty string, the default filename is used.
func PrepareDirectory ¶
PrepareDirectory ensures that the given directory exists. The given path is resolved and any missing parent directories are created if necessary. The function returns the absolute path of the directory. If the given path is the empty string, the default configuration directory is used.
Types ¶
type Configuration ¶
type Configuration map[string]interface{}
Configuration contains configuration data in a generic key value structure.
func Load ¶
func Load(path, filename string) (Configuration, error)
Load loads the configuration from the given file in the given directory. If the path is the empty string, the default configuration directory is used. If the given filename is the empty string, the default filename is used.
func LoadDefault ¶
func LoadDefault() (Configuration, error)
LoadDefault loads JSON configuration data from the default file in the default configuration directory.
func Read ¶
func Read(in io.Reader) (Configuration, error)
Read reads JSON configuration data from the given reader.
func (Configuration) Get ¶
func (config Configuration) Get(key Key, defaultValue interface{}) interface{}
Get retrieves the value at the given path in the configuration data. If the key path cannot be found, the given default value is returned.
func (Configuration) GetSlice ¶
func (config Configuration) GetSlice(key Key, readElement func(int, map[string]interface{}))
GetSlice retrieves the value at the given path as array. It iterates over its elements and calls the given callback for each element.
func (Configuration) GetStrings ¶
func (config Configuration) GetStrings(key Key, defaultValue []string) []string
GetStrings retrieves the value at the given path as string slice. If the key path cannot be found, the given default value is returned.