cnfgfile

package module
v0.0.0-...-f880041 Latest Latest
Warning

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

Go to latest
Published: May 31, 2023 License: MIT Imports: 11 Imported by: 6

README

golift.io/cnfgfile

Go Report Card

This package simply allows you to load a configuration file from XML, YAML, TOML or JSON using one method call.

Documentation

Overview

Package cnfgfile provides a shorthand procedure to unmarshal any config file(s). You can put your configuration into any file format: XML, YAML, JSON, TOML. You can pass in more than one config file to unmarshal a hierarchy of configs. Works well with parent cnfg package. Call this package or cnfg in either order. The former overrides the latter.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrNoFile = fmt.Errorf("must provide at least 1 file to unmarshal")

Functions

func Unmarshal

func Unmarshal(config interface{}, configFile ...string) error

Unmarshal parses a configuration file (of any format) into a config struct. This is a shorthand method for calling Unmarshal against the json, xml, yaml or toml packages. If the file name contains an appropriate suffix it is unmarshaled with the corresponding package. If the suffix is missing, TOML is assumed. Works with multiple files, so you can have stacked configurations. Will detect (and decompress) a file that is gzip or bzip2 compressed.

Example

The cnfgfile.Unmarshal() procedure can be used in place of: xml.Unmarshal, json.Unmarshal, yaml.Unmarshal and toml.Unmarshal. This procedure also reads in the provided file, so you don't need to do any of the io work beforehand. Using this procedure in your app allows your consumers to a use a config file format of their choosing. Very cool stuff when you consider _that file_ could just be a config file for a larger project.

package main

import (
	"fmt"
	"os"

	"golift.io/cnfg"
	"golift.io/cnfgfile"
)

func main() {
	// Recommend adding tags for each type to your struct members. Provide full compatibility.
	type Config struct {
		Interval cnfg.Duration `json:"interval" xml:"interval" toml:"interval" yaml:"interval"`
		Location string        `json:"location" xml:"location" toml:"location" yaml:"location"`
		Provided bool          `json:"provided" xml:"provided" toml:"provided" yaml:"provided"`
	}

	// Create a test file with some test data to unmarshal.
	// YAML is just an example, you can use any supported format.
	path, err := os.CreateTemp("", "test_config_file.yaml")
	if err != nil {
		panic(err)
	}

	yaml := "---\ninterval: 5m\nlocation: Earth\nprovided: true\n"
	if _, err = fmt.Fprint(path, yaml); err != nil {
		panic(err)
	}

	path.Close()

	// Start with an empty config. Or set some defaults beforehand.
	config := &Config{}

	// Simply pass in your config file. If it contains ".yaml" it will be parsed as YAML.
	// Same for ".xml" and ".json". If the file has none of these extensions it is parsed
	// as TOML. Meaning if you name your config "config.conf" it needs ot be TOML formatted.
	err = cnfgfile.Unmarshal(config, path.Name())
	if err != nil {
		panic(err)
	}

	fmt.Printf("interval: %v, location: %v, provided: %v", config.Interval, config.Location, config.Provided)
}
Output:

interval: 5m, location: Earth, provided: true

Types

This section is empty.

Jump to

Keyboard shortcuts

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