flagcfg

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2023 License: Apache-2.0 Imports: 11 Imported by: 6

README

flagcfg

Go Reference

The flagcfg package populates flags from a TOML config file. Each flag is assumed to have an optional top-level value in the config file, having the same name. However, if a flag contains a dash or a period, those are converted to underscores.

Flags that have aready been assigned are not overwritten.

This package can be used together with github.com/facebookgo/flagenv to load flags from a config file, environment variable, or command-line.

Example:

// Parse flags from command-line
flag.Parse()

// Parser flags from config
flagcfg.AddDefaults()
// or use flagcfg.AddDefaultFiles("MYAPP_CONFIG", "myapp.config")
flagcfg.Parse()

// Parse flags from environment (using github.com/facebookgo/flagenv)
flagenv.Prefix = "MYPREFIX_"
flagenv.Parse()

Documentation

Overview

Package flagcfg populates flags from a TOML config file. Each flag is assumed to have an optional top-level value in the config file, having the same name. However, if a flag contains a dash or a period, those are converted to underscores.

Flags that have already been assigned are not overwritten.

This package can be used together with github.com/facebookgo/flagenv to load flags from a config file, environment variable, or command-line.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddDefaultFiles

func AddDefaultFiles(envName, cfgName string)

AddDefaultFiles adds default config file locations to search, using the given environment variable name and name of the config file to look for (excluding path).

func AddDefaults

func AddDefaults()

AddDefaults adds default config file locations, using the binary name as a base. It will look for the file specificed by {NAME}_CONFIG, as well as "{NAME}.config" in various locations.

func AddFile

func AddFile(fileName string)

AddFile adds a location to search for a config file.

func Filename

func Filename() string

Filename returns the name of the config file that was parsed by Parse()

func FindConfig

func FindConfig() string

FindConfig returns the first config file that exists in the list.

func Parse

func Parse()

Parse will set each defined flag from the first configuration file found in the list of those added.

func ParseSet

func ParseSet(tomlData []byte, set *flag.FlagSet) error

ParseSet parses the configuration data in TOML format, placing found values into the flag set.

Types

type Locator

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

Locator can be used to find a given configuration file from a list you set up.

func (*Locator) AddDefaultFiles

func (c *Locator) AddDefaultFiles(envName, cfgName string)

AddDefaultFiles adds default config file locations to search, using the given environment variable name and name of the config file to look for (excluding path).

func (*Locator) AddDefaults

func (c *Locator) AddDefaults()

AddDefaults adds default config file locations, using the binary name as a base. It will look for the file specificed by {NAME}_CONFIG, as well as "{NAME}.config" in various locations.

func (*Locator) AddFile

func (c *Locator) AddFile(fileName string)

AddFile adds a location to search for a config file.

func (*Locator) FindConfig

func (c *Locator) FindConfig() string

FindConfig returns the first config file that exists in the list.

func (*Locator) Len

func (c *Locator) Len() int

Len returns the number of config files locations that have been added.

func (*Locator) Reset

func (c *Locator) Reset()

Reset resets the locator to be used again.

Jump to

Keyboard shortcuts

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