• Stars
    star
    175
  • Rank 218,059 (Top 5 %)
  • Language
    Go
  • License
    MIT License
  • Created about 7 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Go package for program configuration

Build Status Coverage Status Go Report Card GoDoc

Description

gonfig is a configuration library designed using the following principles:

  1. The configuration variables are fully specified and loaded into a struct variable.
  2. You only need one statement to load the configuration fully.
  3. Configuration variables can be retrieved from various sources, in this order of increasing priority:
    • default values from the struct definition
    • the value already in the object when passed into Load()
    • config file in either YAML, TOML, JSON or a custom decoder
    • environment variables
    • command line flags

Furthermore, it has the following features:

  • supported types for interpreting:

    • native Go types: all int, uint, string, bool
    • types that implement TextUnmarshaler from the "encoding" package
    • byte slices ([]byte) are interpreted as base64
    • slices of the above mentioned types
    • map[string]interface{}
  • the location of the config file can be passed through command line flags or environment variables

  • printing help message (and hiding individual flags)

Documentation

Documentation can be found on godoc.org: https://godoc.org/github.com/stevenroose/gonfig

// Load loads the configuration of your program in the struct at c.
// Use conf to specify how gonfig should look for configuration variables.
// This method can panic if there was a problem in the configuration struct that
// is used (which should not happen at runtime), but will always try to produce
// an error instead if the user provided incorrect values.
//
// The recognised tags on the exported struct variables are:
//  - id: the keyword identifier (defaults to lowercase of variable name)
//  - default: the default value of the variable
//  - short: the shorthand used for command line flags (like -h)
//  - desc: the description of the config var, used in --help
//  - opts: comma-separated flags.  Supported flags are:
//     - hidden: Hides the option from help outputs.
func Load(c interface{}, conf Conf) error

// Conf is used to specify the intended behavior of gonfig.
type Conf struct {
	// ConfigFileVariable is the config variable that will be read before looking
	// for a config file.  If no value is specified in the environment variables
	// of the command line flags, the default config file will be read.
	// This flag should be defined in the config file struct and referred to here
	// by its ID.  The default value for this variable is obviously ignored.
	ConfigFileVariable string

	// FileDisable disabled reading config variables from the config file.
	FileDisable bool
	// FileDefaultFilename is the default filename to look for for the config
	// file.  If this is empty and no filename is explicitly provided, parsing
	// a config file is skipped.
	FileDefaultFilename string
	// FileDecoder specifies the decoder function to be used for decoding the
	// config file.  The following decoders are provided, but the user can also
	// specify a custom decoder function:
	//  - DecoderYAML
	//  - DecoderTOML
	//  - DecoderJSON
	// If no decoder function is provided, gonfig tries to guess the function
	// based on the file extension and otherwise tries them all in the above
	// mentioned order.
	FileDecoder FileDecoderFn

	// FlagDisable disabled reading config variables from the command line flags.
	FlagDisable bool
	// FlagIgnoreUnknown ignores unknown command line flags instead of stopping
	// with an error message.
	FlagIgnoreUnknown bool

	// EnvDisables disables reading config variables from the environment
	// variables.
	EnvDisable bool
	// EnvPrefix is the prefix to use for the the environment variables.
	// gonfig does not add an underscore after the prefix.
	EnvPrefix string

	// HelpDisable disables printing the help message when the --help or -h flag
	// is provided.  If this is false, an explicit --help flag will be added.
	HelpDisable bool
	// HelpMessage is the message printed before the list of the flags when the
	// user sets the --help flag.
	// The default is "Usage of [executable name]:".
	HelpMessage string
	// HelpDescription is the description to print for the help flag.
	// By default, this is "show this help menu".
	HelpDescription string
}

Usage

type Config struct {
	Color       string `short:"c" default:"red" desc:"color of the thing"`
	Number      int    `short:"n" desc:"number of things"`

    // Use 'id' to change the name of a flag.
	ConfigFile  string `id:"config" short:"C"`
}
var config = Config {
	// alternative way to set default values; they overwrite the ones in the struct
	Number: 42, 
}

// You can also create the config variable inline.
var config = struct {
	Color  string `short:"c" default:"red" desc:"color of the thing"`
}{}

func main() {
	err := gonfig.Load(&config, gonfig.Conf{
		ConfigFileVariable: "config", // enables passing --configfile myfile.conf

		FileDefaultFilename: "myapp.conf",
        // The default decoder will try TOML, YAML and JSON.
		FileDecoder: gonfig.DecoderTOML,

		EnvPrefix: "MYAPP_",
	})
}

License

gonfig is licensed by an MIT license as can be found in the LICENSE file.

More Repositories

1

hal

the Bitcoin companion
Rust
155
star
2

dart-eventsource

A client and server implementation of Server-Side Events.
Dart
56
star
3

dart-reddit

A Reddit library for Dart. Not well-maintained, check out draw as well: https://pub.dev/packages/draw
Dart
29
star
4

rust-bitcoin-p2p

Rust implementation of Bitcoin p2p protocol.
Rust
26
star
5

dart-hex

Easy hexadecimal convertion using dart:convert API.
Dart
19
star
6

go-bitcoin-core-rpc

An RPC client for the Bitcoin Core JSON-RPC API in Go (Golang).
Go
19
star
7

covenants.info

HTML
15
star
8

dart-sync

A library for managing asynchronous processes inspired by the Go sync package.
Dart
12
star
9

dart-websockets

A generic WebSocket package for Dart
Dart
11
star
10

doubletake

Create Bitcoin double-spend discouraging bonds on Liquid.
Rust
9
star
11

watch-with-me

Dart
9
star
12

hal-elements

an Elements extension for hal
Rust
8
star
13

dart-cryptoutils

A crypto utility library for Dart.
Dart
8
star
14

rust-trezor-api

Rust client library for interfacing with Trezor hardware wallet devices
Rust
7
star
15

dart-events

Event handling library for Dart.
Dart
7
star
16

rust-liquid-rpc

Rust client library for the Liquid JSON-RPC API.
Rust
6
star
17

ripple-lib-dart

Dart port of the ripple-lib Ripple library.
Dart
5
star
18

dart-shelf_eventsource

A shelf handler for Server-Side Events (SSE).
Dart
4
star
19

rust-bitcoin-wallet

Simple PoC serde-serializable Bitcoin wallet.
Rust
4
star
20

dart-btcwallet-rpc

Dart
3
star
21

cryptonotes

Personal reference of some papers on cryptocurrency-related topics
Shell
3
star
22

rust-slip21

SLIP-0021 implementation in Rust
Rust
3
star
23

ABC-MDGP

For a school assignment, we implemented an Artificial Bee Colony algorithms for solving the Maximally Diverse Grouping Problem in Java.
Java
3
star
24

lightninggraph

HTML
2
star
25

dart-webfinger

A library that implements the WebFinger protocol defined by RFC 7033.
Dart
2
star
26

rust-daemon-runner

Rust wrapper for running daemon-like programs
Rust
2
star
27

dart-snippets

A collection of useful Dart code snippets.
Dart
2
star
28

rust-bitcoin-scriptexec

Rust
2
star
29

dart-skiplist

A skiplist implementation in Dart.
Dart
1
star
30

template

A simple command line tool for filling file templates based on Go's text/template
Go
1
star
31

dart-enums

A library to create Enums in Dart.
Dart
1
star
32

dart-xrd

Extensible Resource Descriptors in Dart
Dart
1
star
33

rust-base64-compat

base64 crate for Rust that supports rustc v0.19.0 and newer
Rust
1
star