ff

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

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

Go to latest
Published: Sep 28, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

README

ff go.dev reference Latest Release Build Status

ff stands for flags-first, and provides an opinionated way to populate a flag.FlagSet with configuration data from the environment. By default, it parses only from the command line, but you can enable parsing from environment variables (lower priority) and/or a configuration file (lowest priority).

Building a commandline application in the style of kubectl or docker? Consider package ffcli, a natural companion to, and extension of, package ff.

Usage

Define a flag.FlagSet in your func main.

import (
	"flag"
	"os"
	"time"

	"github.com/go-pa/ff"
)

func main() {
	fs := flag.NewFlagSet("my-program", flag.ExitOnError)
	var (
		listenAddr = fs.String("listen-addr", "localhost:8080", "listen address")
		refresh    = fs.Duration("refresh", 15*time.Second, "refresh interval")
		debug      = fs.Bool("debug", false, "log debug information")
		_          = fs.String("config", "", "config file (optional)")
	)

Then, call ff.Parse instead of fs.Parse. Options are available to control parse behavior.

	ff.Parse(fs, os.Args[1:],
		ff.WithEnvVarPrefix("MY_PROGRAM"),
		ff.WithConfigFileFlag("config"),
		ff.WithConfigFileParser(ff.PlainParser),
	)

This example will parse flags from the commandline args, just like regular package flag, with the highest priority. (The flag's default value will be used only if the flag remains unset after parsing all provided sources of configuration.)

Additionally, the example will look in the environment for variables with a MY_PROGRAM prefix. Flag names are capitalized, and separator characters are converted to underscores. In this case, for example, MY_PROGRAM_LISTEN_ADDR would match to listen-addr.

Finally, if a -config file is specified, the example will try to parse it using the PlainParser, which expects files in this format.

listen-addr localhost:8080
refresh 30s
debug true

You could also use the JSONParser, which expects a JSON object.

{
	"listen-addr": "localhost:8080",
	"refresh": "30s",
	"debug": true
}

Or, you could write your own config file parser.

// ConfigFileParser interprets the config file represented by the reader
// and calls the set function for each parsed flag pair.
type ConfigFileParser func(r io.Reader, set func(name, value string) error) error

Flags and env vars

One common use case is to allow configuration from both flags and env vars.

package main

import (
	"flag"
	"fmt"
	"os"

	"github.com/go-pa/ff"
)

func main() {
	fs := flag.NewFlagSet("myservice", flag.ExitOnError)
	var (
		port  = fs.Int("port", 8080, "listen port for server (also via PORT)")
		debug = fs.Bool("debug", false, "log debug information (also via DEBUG)")
	)
	ff.Parse(fs, os.Args[1:], ff.WithEnvVarNoPrefix())

	fmt.Printf("port %d, debug %v\n", *port, *debug)
}
$ env PORT=9090 myservice
port 9090, debug false
$ env PORT=9090 DEBUG=1 myservice -port=1234
port 1234, debug true

Documentation

Overview

Package ff is a flags-first helper package for configuring programs.

Runtime configuration must always be specified as commandline flags, so that the configuration surface area of a program is self-describing. Package ff provides an easy way to populate those flags from environment variables and config files.

See the README at https://github.com/go-pa/ff for more information.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func JSONParser

func JSONParser(r io.Reader, set func(name, value string) error) error

JSONParser is a parser for config files in JSON format. Input should be an object. The object's keys are treated as flag names, and the object's values as flag values. If the value is an array, the flag will be set multiple times.

func Parse

func Parse(fs *flag.FlagSet, args []string, options ...Option) error

Parse the flags in the flag set from the provided (presumably commandline) args. Additional options may be provided to parse from a config file and/or environment variables in that priority order.

func PlainParser

func PlainParser(r io.Reader, set func(name, value string) error) error

PlainParser is a parser for config files in an extremely simple format. Each line is tokenized as a single key/value pair. The first whitespace-delimited token in the line is interpreted as the flag name, and all remaining tokens are interpreted as the value. Any leading hyphens on the flag name are ignored.

Types

type ConfigFileParser

type ConfigFileParser func(r io.Reader, set func(name, value string) error) error

ConfigFileParser interprets the config file represented by the reader and calls the set function for each parsed flag pair.

type Context

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

Context contains private fields used during parsing.

type JSONParseError

type JSONParseError struct {
	Inner error
}

JSONParseError wraps all errors originating from the JSONParser.

func (JSONParseError) Error

func (e JSONParseError) Error() string

Error implenents the error interface.

func (JSONParseError) Unwrap

func (e JSONParseError) Unwrap() error

Unwrap implements the errors.Wrapper interface, allowing errors.Is and errors.As to work with JSONParseErrors.

type Option

type Option func(*Context)

Option controls some aspect of Parse behavior.

func WithAllowMissingConfigFile

func WithAllowMissingConfigFile(allow bool) Option

WithAllowMissingConfigFile tells Parse to permit the case where a config file is specified but doesn't exist. By default, missing config files result in an error.

func WithConfigFile

func WithConfigFile(filename string) Option

WithConfigFile tells Parse to read the provided filename as a config file. Requires WithConfigFileParser, and overrides WithConfigFileFlag. Because config files should generally be user-specifiable, this option should be rarely used. Prefer WithConfigFileFlag.

func WithConfigFileFlag

func WithConfigFileFlag(flagname string) Option

WithConfigFileFlag tells Parse to treat the flag with the given name as a config file. Requires WithConfigFileParser, and is overridden by WithConfigFile.

To specify a default config file, provide it as the default value of the corresponding flag -- and consider also using the WithAllowMissingConfigFile option.

func WithConfigFileParser

func WithConfigFileParser(p ConfigFileParser) Option

WithConfigFileParser tells Parse how to interpret the config file provided via WithConfigFile or WithConfigFileFlag.

func WithConfigFileVia

func WithConfigFileVia(filename *string) Option

WithConfigFileVia tells Parse to read the provided filename as a config file. Requires WithConfigFileParser, and overrides WithConfigFileFlag. This is useful for sharing a single root level flag for config files among multiple ffcli subcommands.

func WithEnvVarNoPrefix

func WithEnvVarNoPrefix() Option

WithEnvVarNoPrefix tells Parse to try to set flags from environment variables without any specific prefix. Flag names are matched to environment variables by capitalizing the flag name, and replacing separator characters like periods or hyphens with underscores. By default, flags are not set from environment variables at all.

func WithEnvVarPrefix

func WithEnvVarPrefix(prefix string) Option

WithEnvVarPrefix tells Parse to try to set flags from environment variables with the given prefix. Flag names are matched to environment variables with the given prefix, followed by an underscore, followed by the capitalized flag names, with separator characters like periods or hyphens replaced with underscores. By default, flags are not set from environment variables at all.

func WithEnvVarSplit

func WithEnvVarSplit(delimiter string) Option

WithEnvVarSplit tells Parse to split environment variables on the given delimiter, and to make a call to Set on the corresponding flag with each split token.

func WithIgnoreUndefined

func WithIgnoreUndefined(ignore bool) Option

WithIgnoreUndefined tells Parse to ignore undefined flags that it encounters in config files. By default, if Parse encounters an undefined flag in a config file, it will return an error. Note that this setting does not apply to undefined flags passed as arguments.

type StringConversionError

type StringConversionError struct {
	Value interface{}
}

StringConversionError is returned when a value in a config file can't be converted to a string, to be provided to a flag.

func (StringConversionError) Error

func (e StringConversionError) Error() string

Error implements the error interface.

Directories

Path Synopsis
Package ffcli is for building declarative commandline applications.
Package ffcli is for building declarative commandline applications.
Package fftest provides unit test helpers.
Package fftest provides unit test helpers.
Package fftoml provides a TOML config file paser.
Package fftoml provides a TOML config file paser.
Package ffyaml provides a YAML config file parser.
Package ffyaml provides a YAML config file parser.

Jump to

Keyboard shortcuts

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