cfg

package
v0.2.9 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2024 License: MIT Imports: 8 Imported by: 4

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

View Source
const DefaultDirectory = "~/.config/hamradio"

DefaultDirectory is the place where all hamradio tools store their configuration locally.

View Source
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

func Directory(path string) (string, error)

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

func Exists(path, filename string) bool

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

func LoadJSON(path, filename string, data interface{}) error

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

func PrepareDirectory(path string) (string, error)

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.

func SaveJSON

func SaveJSON(path, filename string, data interface{}) error

SaveJSON saves the given JSON data to the given file 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.

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.

type Key

type Key string

Key names

const (
	MyCall    Key = "my.call"
	MyLocator Key = "my.locator"
)

Some commonly used parameters.

Jump to

Keyboard shortcuts

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