gofigure

package module
v0.0.0-...-51777c6 Latest Latest
Warning

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

Go to latest
Published: May 14, 2015 License: MIT Imports: 8 Imported by: 0

README

gofigure

Build Status GoDoc

A configuration utility for Go inspired by The Twelve-Factor App methodology that allows configuration options to be set via command line flags and environment variables.

Usage

import "github.com/cpliakas/gofigure"
config := gofigure.New()
config.EnvPrefix = "MYAPP_"

config.Add("listen").
	EnvVar("LISTEN").
	Default(":3000").
	Description("The address to listen on.")

config.Parse()

fmt.Println(*config.Get("listen"))

The following commands set the value of the listen flag to :3001:

./myapp --listen=:3001
MYAPP_LISTEN=:3001 ./myapp
MYAPP_LISTEN=:3002 ./myapp --listen=:3001

Disclaimer

I am new to Go, the best way to learn a language is to create a project and expose it to the world for feedback. For full-featured configuration utilities written by a more competent Gophers, check out the globalconf and Viper projects.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GooptFigureBool

func GooptFigureBool(names []string, help string) *bool

func GooptFigureString

func GooptFigureString(names []string, def string, help string) *string

This function is a convenience function. It displays the default values for us but handles them in the way we prefer it.

Types

type Category

type Category struct {
	Name       string
	Parent     *Category
	Categories CategoryMap
	Values     ValueMap
}

func NewCategory

func NewCategory(name string, parent *Category) *Category

func (Category) Find

func (c Category) Find(spec string) (string, bool)

func (Category) FindOption

func (c Category) FindOption(o *Option) (string, bool)

func (Category) Print

func (c Category) Print(indent string)

type CategoryMap

type CategoryMap map[string]*Category

func (CategoryMap) Delete

func (c CategoryMap) Delete(key string)

func (CategoryMap) Exists

func (c CategoryMap) Exists(key string) bool

func (CategoryMap) Get

func (c CategoryMap) Get(key string) *Category

func (CategoryMap) MustGet

func (c CategoryMap) MustGet(key string) *Category

func (CategoryMap) Print

func (c CategoryMap) Print(indent string)

func (CategoryMap) Set

func (c CategoryMap) Set(key string, value *Category)

type Config

type Config struct {
	Description         string
	DescribeEnvironment bool // if true, the environment variable is automatically added to the flag description
	DisableCommandLine  bool
	EnvPrefix           string
	EnvOverridesFile    bool
	FileParser          File
	RequireFile         bool // application will panic if RequireFile == true, FileParser != nil and file doesn't exist
	Version             string
	// contains filtered or unexported fields
}

Config contains the configuration options that may be set by command line flags and environment variables.

func New

func New() *Config

Returns a new Config instance.

func (*Config) Add

func (c *Config) Add(name string) *Option

Adds a configuration option, returns an Option instance for easily setting the corresponding environment variable, default value, and description.

func (*Config) AddBoolean

func (c *Config) AddBoolean(name string) *Option

func (Config) Get

func (c Config) Get(name string) *string

Returns a configuration option by flag name.

func (*Config) GetOptions

func (c *Config) GetOptions() map[string]*Option

Returns all options.

func (*Config) Parse

func (c *Config) Parse()

Parses the configuration options into defined flags, sets the value accordingly. Options are read first from command line flags, then from environment variables, and falls back to the default value if neither are set.

See https://github.com/rakyll/globalconf/blob/master/globalconf.go

func (*Config) ParseEnv

func (c *Config) ParseEnv(passed map[string]bool)

func (*Config) ParseFile

func (c *Config) ParseFile(passed map[string]bool) error

type File

type File interface {
	Parse() (*Category, error)
}

type JsonFile

type JsonFile struct {
	Filename string
}

func NewJsonFile

func NewJsonFile(name string) *JsonFile

func (JsonFile) Parse

func (j JsonFile) Parse() (*Category, error)

func (JsonFile) ProcessLayer

func (j JsonFile) ProcessLayer(layer map[string]interface{}, name string, parent *Category) (*Category, error)

type Option

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

Option contains the details of a configuration options, e.g. corresponding environment variable, default value, description.

func (*Option) Default

func (o *Option) Default(def string) *Option

Sets the configuration option's default value.

func (*Option) Description

func (o *Option) Description(desc string) *Option

Sets the configuration options long description.

func (*Option) EnvVar

func (o *Option) EnvVar(envVar string) *Option

Sets the configuration option's corresponding environment variable.

func (*Option) FileSpec

func (o *Option) FileSpec(spec string) *Option

func (Option) GetDefault

func (o Option) GetDefault() string

func (Option) GetDescription

func (o Option) GetDescription() string

func (Option) GetEnvVar

func (o Option) GetEnvVar() string

func (Option) GetFileSpec

func (o Option) GetFileSpec() string

func (Option) GetLongOpt

func (o Option) GetLongOpt() string

func (Option) GetName

func (o Option) GetName() string

func (Option) GetShortOpt

func (o Option) GetShortOpt() string

func (*Option) LongOpt

func (o *Option) LongOpt(opt string) *Option

func (*Option) ShortOpt

func (o *Option) ShortOpt(opt string) *Option

type ValueMap

type ValueMap map[string]string

func (ValueMap) Delete

func (c ValueMap) Delete(key string)

func (ValueMap) Exists

func (c ValueMap) Exists(key string) bool

func (ValueMap) Get

func (c ValueMap) Get(key string) string

func (ValueMap) MustGet

func (c ValueMap) MustGet(key string) string

func (ValueMap) Print

func (c ValueMap) Print(indent string)

func (ValueMap) Set

func (c ValueMap) Set(key, value string)

Jump to

Keyboard shortcuts

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