goflenfig

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Oct 12, 2021 License: MIT Imports: 10 Imported by: 1

README

goflenfig : A super simple flag and config package

builds.sr.ht status PkgGoDev Coverage

Overview

goflenfig is a semi-drop-in replacement (only StringVar, IntVar and BoolVar are currently implemented) for the standard flag package. It is extremely simple to use and allows flags to be specified either as arguments on the command-line, environment variables or configuration in a JSON file.

It is not intended as a replacement for "commander"-style libraries or as a fully-fledged configuration file library.

It is intended to make a 12-factor style application a little easier to build and test. I use this quite a lot for microservices deployed in Kubernetes.

Install

go get git.sr.ht/~yoink00/goflenfig

Usage

The order of precedence is:

  1. Command-line argument (preferred)
  2. Environment variable
  3. Configuration file (least preferred)

The configuration file is searched (for *NIX platforms) according to the XDG standard in these directories:

  1. $XDG_CONFIG_HOME/<sub-directory> or $HOME/.config/<sub-directory>
  2. Iterating through the list of directories in $XDG_CONFIG_DIRS looking for a sub-directory called <sub-directory> stopping at the first one found.
  3. /etc/<sub-directory>

The name of <sub-directory> is either the name of the binary (path.Base(os.Args[0])) but this can be overriden using ConfigDirectory().

The name of the config file defaults to config but this can be overriden with ConfigFilename().

The package performs some case-conversion:

  • JSON map names use lowerCamelCase
  • Environment variable names use SCREAMING_SNAKE_CASE
  • Command-line flags are not converted but kebab-case is commonly used

Example

package main

import (
	"git.sr.ht/~yoink00/goflenfig"
)

var testFlag     string
var testIntFlag  int
var testBoolFlag bool

func init() {
	goflenfig.Prefix("TEST_")
	goflenfig.StringVar(&testFlag, "test", "default", "Specify a test flag")
	goflenfig.IntVar(&testIntFlag, "test_int", 100, "Specify a test int flag")
	goflenfig.BoolVar(&testBoolFlag, "test_bool", false, "Specify a test bool flag")
}

func main() {
	goflenfig.Parse()

	fmt.Println(testFlag)
	fmt.Println(testIntFlag)
	fmt.Println(testBoolFlag)
}

Contributing

Code contributions

The goflenfig project uses the git send-email for collaborating. The tools are usually baked into Git and are used by projects like the Linux kernel, PostgreSQL and Git. This link will guide you through setting it up if you need to.

Please submit any patches in a plaintext email to ~yoink00/goflenfig@lists.sr.ht or if for some reason that doesn't work to u.yoink00.goflenfig@lists.sr.ht.

Anything else

Feel free to send any comments, issues or questions in a plaintext email to ~yoink00/goflenfig@lists.sr.ht or if for some reason that doesn't work to u.yoink00.goflenfig@lists.sr.ht.

Author

Stuart Wallace

License

MIT

Documentation

Overview

Package goflenfig is a simple flag and config package that is a semi-drop-in replacement (only `StringVars` is currently implemented) for the standard `flag` package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolVar added in v0.5.0

func BoolVar(p *bool, name string, value bool, usage string)

BoolVar defines an boolean flag with specified name, default value, and usage string. The argument p points to an bool variable in which to store the value of the flag.

func ConfigDirectory added in v0.2.0

func ConfigDirectory(d string)

ConfigDirectory allows users of the package to specify a name for the directory in which config should be located. The default is the binary name as discovered by `path.Base(os.Args[0])`. The config directory will located in a platform specific location (see README.md for more details).

func ConfigFilename added in v0.2.0

func ConfigFilename(f string)

ConfigFilename allows users of the package to specify the name of the file that contains the configuration. The default is `config`.

func EnvPrefix added in v0.2.1

func EnvPrefix(p string)

EnvPrefix allows users of the package to specify a prefix for environment variables. Environment variables are uppercase versions of the name specified in the `StringVar` function with `EnvPrefix` prepended. For example a flag of `fileName` and a prefix of `TEST_` will result in an environment variable of `TEST_FILENAME`.

func IntVar added in v0.3.0

func IntVar(p *int, name string, value int, usage string)

IntVar defines an integer flag with specified name, default value, and usage string. The argument p points to an int variable in which to store the value of the flag.

func Parse

func Parse()

Parse parses the command-line flags from os.Args[1:]. Must be called after all flags are defined and before flags are accessed by the program. This will call `flag.Parse` as well a process environment variables and the configuration file. Panics maybe thrown, if: a config file exists but it cannot be opened or it cannot be parsed.

func StringVar

func StringVar(p *string, name string, value string, usage string)

StringVar defines a string flag with specified name, default value, and usage string. The argument p points to a string variable in which to store the value of the flag.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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