configuration

package
v0.38.0 Latest Latest
Warning

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

Go to latest
Published: May 6, 2020 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrDecodeFailed is given when the config cannot be decoded.
	ErrDecodeFailed = errConfig.Code("decode_fail").Error("failed to decode config")
	// ErrEncodeFailed is given when the config cannot be encoded.
	ErrEncodeFailed = errConfig.Code("encode_fail").Error("failed to encode config")
	// ErrFileNotFound is given when the config file cannot be found.
	ErrFileNotFound = errConfig.Code("not_found").Error("config file not found")

	// ErrTypeNotSet is given when a config has no type specified
	ErrTypeNotSet = errConfig.Code("type_not_set").Error("field `type` of config is not set")
)
View Source
var (
	// ErrVersionNotReachable is given when the migrater cannot reach the desired version
	ErrVersionNotReachable = errConfig.Code("not_reachable").Error("desired config version not reachable")
)

Functions

func ParseMap

func ParseMap(src *ConfigMap, dst interface{}) error

ParseMap uses mapstructure to convert a ConfigMap into a struct

For example, the following JSON:

{
    "ssh_key": "/home/joris/.ssh/secrethub"
}

Can be loaded in a struct of type:

    type UserConfig struct {
	    SSHKeyPath string `json:"ssh_key,omitempty"`
    }

decodeHook is used to convert non-standard types into the correct format

func Read

func Read(data []byte, destination interface{}) error

Read attempts to read a config in a destination interface.

func ReadFromFile

func ReadFromFile(path string, destination interface{}) error

ReadFromFile attempts to read a config as a struct from a file. It will unmarshal yaml and json into the destination.

func WriteToFile

func WriteToFile(src interface{}, path string, fileMode os.FileMode) error

WriteToFile attempts to marshal the src arg and write to a file at the given path. If the file is of an unsupported extension we cannot determine what type of encoding to use, so it defaults to writing encoded json to that file. JSON is written in indented 'pretty' format to allow for easy user editing.

Types

type ConfigMap

type ConfigMap map[string]interface{}

ConfigMap is the type used for configurations that are still in a map format Map format is to make changes in structure with migrations possible

func MigrateConfigTo

func MigrateConfigTo(io ui.IO, config ConfigMap, versionFrom int, versionTo int, migrations []Migration, checkOnly bool) (ConfigMap, error)

MigrateConfigTo attempts to convert a config in ConfigMap format from versionFrom to versionTo A list of Migrations has to be passed that form at least a path from versionFrom to versionTo Function may fail if the migration path contains dead ends that do not lead to versionTo Setting checkOnly to true will not perform the actual migration and can be used to check whether there is a valid migration path from one version to another

func ReadConfigurationDataFromFile

func ReadConfigurationDataFromFile(path string) (ConfigMap, []byte, error)

ReadConfigurationDataFromFile retrieves the data and attempts to read the config as a ConfigMap from a file. This simplifies the process of determining to parse the config as a ConfigMap or to directly unmarshal into a struct.

func ReadMap

func ReadMap(data []byte) (ConfigMap, error)

ReadMap attempts to unmarshal a []byte it into the dest map. Both json and yaml are supported

func (ConfigMap) GetType

func (c ConfigMap) GetType() (string, error)

GetType returns the type of the configuration file If it is not set, it is assumed to have no type.

func (ConfigMap) GetVersion

func (c ConfigMap) GetVersion() (int, error)

GetVersion returns the version of the configuration file. If it is not set, it is assumed that is configuration version 1.

type Migration

type Migration struct {
	VersionFrom int
	VersionTo   int
	UpdateFunc  func(ui.IO, ConfigMap) (ConfigMap, error)
}

Migration defines a function that is used to convert a configuration in ConfigMap format from VersionFrom to VersionTo

Jump to

Keyboard shortcuts

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