gonfigurator

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2019 License: MIT Imports: 5 Imported by: 1

README

Gonfigurator

This package is used to load YAML configuration files and parse them into structs.

Install

go

go get github.com/fresh8gaming/gonfigurator

gb

gb vendor fetch github.com/fresh8gaming/gonfigurator

Example

import (
	"github.com/fresh8gaming/gonfigurator"
)

type cloudConfig struct {
	ProjectID string       `json:"project_id"`
}

type loggingConfig struct {
	Level string `json:"level"`
}

type myserviceConfig struct {
	Cloud   cloudConfig   `json:"cloud"`
	Logging loggingConfig `json:"logging"`
}

var config myserviceConfig


func main() {
	err := gonfigurator.Parse("/etc/f8-uploader/uploader.yaml", &config)
	if err != nil {
		log.Fatal(err)
	}
	log.Println(config.Cloud.ProjectID)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Load added in v1.2.1

func Load() error

func Parse

func Parse(defaultPath string, v interface{}) error

Parse loads the YAML file at the given path and reads it into v

func ParseConfig added in v1.1.0

func ParseConfig(loader ConfigLoader, target interface{}) error

ParseConfig uses the specified loader to extract configuration into the target

func ParseCustomFlag added in v1.2.0

func ParseCustomFlag(defaultPath string, flagName string, v interface{})

ParseCustomFlag will schedule to load the YAML file from the default path or using the path specified using custom command line flag (flagName) when Load() is called

Types

type ConfigLoader added in v1.1.0

type ConfigLoader interface {
	Parse(path string, target interface{}) error
	ParseCustomFlag(defaultPath string, flagName string, v interface{})
	Path() string
	Load() error
}

ConfigLoader should load a file at the given path into the target interface

type ConfigLoaderMock added in v1.1.0

type ConfigLoaderMock struct {
	// ParseFunc mocks the Parse function.
	ParseFunc func(path string, target interface{}) error
	// PathFunc mocks the Path function.
	PathFunc func() string
}

ConfigLoaderMock is a mock implementation of ConfigLoader.

    func TestSomethingThatUsesConfigLoader(t *testing.T) {

        // make and configure a mocked ConfigLoader
        mockedConfigLoader := &ConfigLoaderMock{
            ParseFunc: func(path string, target interface{}) error {
	               panic("TODO: mock out the Parse function")
            },
            PathFunc: func() string {
	               panic("TODO: mock out the Path function")
            },
        }

        // TODO: use mockedConfigLoader in code that requires ConfigLoader

    }

func (*ConfigLoaderMock) Parse added in v1.1.0

func (mock *ConfigLoaderMock) Parse(path string, target interface{}) error

Parse calls ParseFunc.

func (*ConfigLoaderMock) Path added in v1.1.0

func (mock *ConfigLoaderMock) Path() string

Path calls PathFunc.

type YamlLoader added in v1.1.0

type YamlLoader struct {
	ConfigPath string
}

YamlLoader implements the loader interface to load YAML files

func (*YamlLoader) Parse added in v1.1.0

func (l *YamlLoader) Parse(path string, target interface{}) error

Parse loads the .yml file from Loader.ConfigPath if set, defaultPath as a fallback. Adheres to the LoaderBlueprint interface.

func (*YamlLoader) Path added in v1.1.0

func (l *YamlLoader) Path() string

Path returns the config path for the yaml file returns ConfigPath if specified on the struct, else attempts to parse from -c flag

Jump to

Keyboard shortcuts

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