config

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2022 License: MIT Imports: 8 Imported by: 1

README

Config

Wrapper around Viper

Example Usage

type configData struct {
  Value string `mapstructure:"value"`
}

yamlReader := strings.NewReader(`
data:
  value: Hello, World!
`)

readerOption := config.Reader(yamlReader)
// yaml is the default value
configExtensionOption := config.Extension("yaml")

options := []config.Option{readerOption, configExtensionOption}

// Could also use the following
// configFileOption := config.Name("myConfigFileName")
// configPathsOption := config.Paths("/my/absolute/path/to/config", "./my/relative/path/to/config")
// options := []config.Option{configFileOption, configPathsOption, configExtensionOption}

conf, err := config.Init(options...)
if err != nil {
  panic(err)
}

var confData configData
if err := conf.Get(`data`, &confData); err != nil {
  panic(err)
}

fmt.Println(confData.Value)

Documentation

Overview

Package config provides means to access configuration data at runtime in a type-safe manner

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interface

type Interface interface {
	// Get fills the structPtr with configuration values found at key
	Get(key string, structPtr interface{}) error
	// Keys returns the top level keys of the configuration data
	Keys() []string
}

Interface is an abstraction around the underlying configuration

func Init

func Init(options ...Option) (Interface, error)

Init initializes a config.Interface using the provided config.Option's An error is returned when there is an issue reading in the configuration

Example
package main

import (
	"fmt"
	"github.com/wgentry22/config"
	"strings"
)

func main() {
	type configData struct {
		Value string `mapstructure:"value"`
	}

	yamlReader := strings.NewReader(`
data:
  value: Hello, World!
`)

	readerOption := config.Reader(yamlReader)
	// yaml is the default value
	configExtensionOption := config.Extension("yaml")

	options := []config.Option{readerOption, configExtensionOption}

	// Could also use the following
	// configFileOption := config.Name("myConfigFileName")
	// configPathsOption := config.Paths("/my/absolute/path/to/config", "./my/relative/path/to/config")
	// options := []config.Option{configFileOption, configPathsOption, configExtensionOption}

	conf, err := config.Init(options...)
	if err != nil {
		panic(err)
	}

	var confData configData
	if err := conf.Get(`data`, &confData); err != nil {
		panic(err)
	}

	fmt.Println(confData.Value)
}
Output:

func MustInit

func MustInit(options ...Option) Interface

MustInit initializes a config.Interface using the provided config.Option's MustInit panics when there is an issue reading in the configuration

type Option

type Option func(*Options)

Option functions allow for customization of the associated Options

func Extension

func Extension(configExtension string) Option

Extension is an Option that sets the file extension of the configuration file to search for

func Hooks

func Hooks(hooks ...mapstructure.DecodeHookFunc) Option

Hooks is an Option which allows additional customization of how to unmarshal the underlying configuration into a struct

func Name

func Name(configName string) Option

Name is an Option that sets the name of the configuration file to search for

func Paths

func Paths(configPaths ...string) Option

Paths is an Option that sets where on the filesystem Interface should look for your configuration file

func Reader

func Reader(reader io.Reader) Option

Reader is an Option that populates the Interface from an io.Reader Helpful in testing.

type Options

type Options struct {
	// contains filtered or unexported fields
}

Options represents the information required to create an Interface instance

func (*Options) Error

func (o *Options) Error() string

Error returns a string representation of the error state during Init and MustInit

Jump to

Keyboard shortcuts

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