settings

package module
v0.0.0-...-9f33643 Latest Latest
Warning

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

Go to latest
Published: May 5, 2021 License: BSD-3-Clause Imports: 7 Imported by: 0

README

settings

A very simple Go yaml configuration file loader.

How to Use

Assuming you want to load database.yaml in myapp folder:

collection, err := settings.NewCollection("database", "myapp")
if err != nil {
    // do something    
}

model := &struct {
    Address string `yaml:"address"`
    Port    int    `yaml:"port"`
}{}

if err := collection.Load(model); err != nil {
    // use default or do something.
}

If you want a different extension:

collection, err := settings.NewCollection("database", "myapp")
if err != nil {
    // do something    
}
collection.Extension = "conf" // will look for database.conf instead.

model := &struct {
    Address string `yaml:"address"`
    Port    int    `yaml:"port"`
}{}

if err := collection.Load(model); err != nil {
    // use default or do something.
}

If you want to add additional search path:

collection, err := settings.NewCollection("database", "myapp")
if err != nil {
    // do something    
}
collection.SearchPaths = append(collection.SearchPaths, "/path/to/somewhere")

model := &struct {
    Address string `yaml:"address"`
    Port    int    `yaml:"port"`
}{}

if err := collection.Load(model); err != nil {
    // use default or do something.
}

default search paths are

  1. $HOME/.config
  2. /etc (Non Windows)
  3. /usr/local/etc (Non Windows)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCollectionNotFound = errors.New("collection not found")

ErrCollectionNotFound occurred when settings file cannot be found in search paths.

Functions

This section is empty.

Types

type Collection

type Collection struct {

	// Extension is settings file extension, default is 'yaml'.
	Extension string

	// Paths is list of directories to look for settings file.
	// Default values are as follows,
	// - $HOME/.config
	// - /etc				(Non Windows)
	// - /usr/local/etc		(Non Windows)
	SearchPaths []string
	// contains filtered or unexported fields
}

Collection is a group of settings which are saved in a single yaml file.

func NewCollection

func NewCollection(name, dirname string) (*Collection, error)

NewCollection create new collection given its name and directory name.

func (*Collection) Load

func (cl *Collection) Load(v interface{}) error

Load read settings file and unmarshal the content as given v. For example, if a collection created with directory name 'test' and named 'settings', with default extension, then this function will look for SEARCH_DIR/test/settings.yaml.

func (*Collection) Name

func (cl *Collection) Name() string

Name returns name of this collection.

Jump to

Keyboard shortcuts

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