configparser

package module
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Sep 29, 2023 License: MIT Imports: 11 Imported by: 3

README

Configuration Parser

A library which can be used to pull configuration values from either environment variables or command line flags.

You pass a struct to configparser, and it populates that struct with values pulled from environment variables and command line flags. You can also tell configparser how you want various fields in the struct to be parsed by using tags in the struct.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

func Parse(ptrtostruct interface{}) error

Parse will take in a pointer to a struct and set each field to an environment variable or a flag from the command line. The environment variable will take precedence over the command line flag.

Parse will invoke ParseWithDir with dir set to an empty string.

Example
type Config struct {
	Hostname string `env:"HOST" flag:"host" usage:"hostname of the server" mandatory:"true"`
	Port     int    `default:"8080"`
	Async    bool
}

c := Config{}

// Directory for config files are defined in the CONFIGDIR environment
// variable, the -configdir command line argument, or the /config
// directory, in that order.
err := ParseWithDir(&c, RetrieveConfigDirectory("CONFIGDIR", "configdir", "/config"))
if err != nil {
	log.Fatal(err)
}
fmt.Printf("Hostname: %v\n", c.Hostname)

// Needed because we are calling flag.Parse() each time we run a test.
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
Output:

func ParseWithDir added in v0.2.3

func ParseWithDir(ptrtostruct interface{}, dir string) error

ParseWithDir will take in a pointer to a struct and set each field to a value in the a file, environment variable, or a flag from the command line. The file will take precedence over the environment variable and the environment variable will take precedence over the command line flag.

If a field is of type bool, it will be set to true as long as the corresponding environment variable is set, irrespective of the environment variable's value.

Set the appropriate tag in each field to tell ParseWithDir how to handle the field. ParseWithDir accepts the following tags: env, flag, default, usage, mandatory.

The env tag specifies the environment variable name which corresponds to the field. If this is not specified, ParseWithDir uses the uppercase version of the field name.

The flag tag specifies the command line flag name which corresponds to the field. If this is not specified, ParseWithDir uses the lowercase version of the field name.

The default tag specifies a default value for the field. This value is used if the corresponding environment variable and command line flag do not exist.

The mandatory tag marks the field as mandatory. If the corresponding environment variable and command line flag do not exist, ParseWithDir will print an error message and the usage to stderr and return with an error. ParseWithDir will assume that the field is mandatory as long as the tag exists - it doesn't matter what value the tag is set to.

The usage tag specifies the usage text for the command line flag.

func RetrieveConfigDirectory added in v0.2.1

func RetrieveConfigDirectory(envKey, flagKey, defaultval string) string

Retrieves file config directory from an environment variable or command line flag. The environment variable takes precedence. This function is only used to retrieve the configuration directory name.

Types

This section is empty.

Jump to

Keyboard shortcuts

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