covargo

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2018 License: MIT Imports: 6 Imported by: 0

README

covargo

Config Variables helper library in Go.

Go (golang) library for loading configuration variables from cli flags, env vars or files

Installation

Same as most go libraries:

go get -u https://github.com/amattn/covargo

or if you are into vgo, just add the import statement and do a vgo build

Usage

covargo makes it easy to load configuration variables in a variety of ways.

in order of highest priority first:

  1. Command line flag
  2. Environment variable
  3. cli flag or defalut filepath that points to single file
  4. cli flag or defalut filepath that points to json file // TODO

Basic usage:

  • Create a collection or use the default collection.
  • Add congig Items, each identified by a string key.
  • For each item, define how it can be loaded.
  • Load the items.
  • Each item can then be queried for its value.

Here's an example of using the default collection:

const (
	DROPBOOKSOFT_SECRET_APP_TOKEN = "DROPBOOKSOFT_SECRET_APP_TOKEN"
)

item := covargo.Add(DROPBOOKSOFT_SECRET_APP_TOKEN)
item.SetEnvVar(DROPBOOKSOFT_SECRET_APP_TOKEN)
item.SetCliValueFlags("d", "dbs_token", "secret token used to access dbs API")

// here we are using the same string for the item key as the env var.
item.SetEnvVar(DROPBOOKSOFT_SECRET_APP_TOKEN)

covargo.Load(DROPBOOKSOFT_SECRET_APP_TOKEN)

token := covargo.StringValue(DROPBOOKSOFT_SECRET_APP_TOKEN)

log.Println("token:", token)

Here's an example of using your own collection:

const (
	DROPBOOKSOFT_SECRET_APP_TOKEN = "DROPBOOKSOFT_SECRET_APP_TOKEN"
)

col := NewCollection()

item := col.Add(DROPBOOKSOFT_SECRET_APP_TOKEN)

item.SetEnvVar(DROPBOOKSOFT_SECRET_APP_TOKEN)
item.SetCliValueFlags("d", "dbs_token", "secret token used to access dbs API")

col.Load(DROPBOOKSOFT_SECRET_APP_TOKEN)

token := col.StringValue(DROPBOOKSOFT_SECRET_APP_TOKEN)

// use token to connect to API, or whatever
log.Println("token:", token)

Current Limitations

  • Only supporting string values at the moment
  • Flags used the built-in flagset, flag.CommandLine.

License

MIT. Please see the LICENSE file for the usual boilerplate.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func About added in v0.1.2

func About()

func BuildDate

func BuildDate() time.Time

func BuildNumber

func BuildNumber() int64

func Contains

func Contains(key string) bool

func Load

func Load(key string) error

func LoadAll

func LoadAll() error

func Remove

func Remove(key string)

func StringValue

func StringValue(key string) string

func Version

func Version() string

Types

type Collection

type Collection map[string]*Item
var DefaultCollection Collection

func NewCollection

func NewCollection() Collection

func (Collection) Add

func (col Collection) Add(key string) *Item

add a config item to our "library" of items will panic if ENV_VAR_NAME is empty CliFlag and JSONKey can be empty, and will default to ENV_VAR_NAME typically, CliFlag is shorter and easy to type and JSONKey is empty or a lowercase, snake case variant of ENV_VAR_NAME

func (Collection) Contains

func (col Collection) Contains(key string) bool

func (Collection) Get

func (col Collection) Get(key string) *Item

func (Collection) Load

func (col Collection) Load(key string) error

func (Collection) LoadAll

func (col Collection) LoadAll() error

func (Collection) Remove

func (col Collection) Remove(key string)

func (Collection) StringValue

func (col Collection) StringValue(key string) string

convenience

type Item

type Item struct {
	Key string // required

	// method cli flag
	Longflag  string // long version of cli flag, such as -version. if "", we don't set a long flag
	Shortflag string // short version of cli flag, such as -v. if "", we don't set a short flag
	FlagUsage string // help message for cli flag(s)

	// method: environment variable value
	ENV_VAR_NAME string // environment variable name. if "" this variable cannot be loaded from environment variable

	// method: read a file, use contents of file as value
	FileContentsPathLongFlag  string // a cli flag that tells where to load a file and use the entire contents of the file as the config variable.
	FileContentsPathShortFlag string // a cli flag that tells where to load a file and use the entire contents of the file as the config variable.
	FileContentsDefaultPath   string // default value to look for file to read and use entire contents as value of config var
	FileContentsUsage         string

	// method Json file
	JSONFilePathLongFlag  string
	JSONFilePathShortFlag string
	JSONFileDefaultPath   string
	Json_key              string // lookup key for pulling value out of a json map.  if "", this variable cannot be loaded from a json file

	DefaultValue string // none of the methods work? use this default value

	RawValue string // value we load from ENV, cli flag, file, etc.

	LastLoad   time.Time  // when did we last attempt to load?
	LoadMethod LoadMethod // how was the value loaded?
	RawSet     time.Time  // when was Raw Value successfully set?
	// contains filtered or unexported fields
}

we use wonky casing to hint what the standard usage is.

func Add

func Add(key string) *Item

add a config item to our "library" of items will panic if ENV_VAR_NAME is empty CliFlag and JSONKey can be empty, and will default to ENV_VAR_NAME typically, CliFlag is shorter and easy to type and JSONKey is empty or a lowercase, snake case variant of ENV_VAR_NAME

func Get

func Get(key string) *Item

func NewItem added in v0.1.1

func NewItem(key string) *Item

add a config item to our "library" of items will panic if `key` is empty if ENV_VAR_NAME is "", then LoadValue() won't check environment variables. if Shortflag and/or longflag is "", LoadValue() won't check cli flags. if JSONKey is "", LoadValue() won't check json file. typically, environment variables are all caps and words are separated by underscores (_) typically, CliFlag is shorter and easy to type and JSONKey is empty or a lowercase, snake case variant of ENV_VAR_NAME

func (*Item) LoadValue

func (ci *Item) LoadValue() error

load the raw value from cli flag, env var, file, etc. returns true if

func (*Item) SetCliValueFlags

func (ci *Item) SetCliValueFlags(shortflag, longflag, usage string)

for now, we only support strings...

func (*Item) SetDefaultValue

func (ci *Item) SetDefaultValue(default_value string)

func (*Item) SetEnvVar

func (ci *Item) SetEnvVar(name string)

func (*Item) SetFileContentsFlags

func (ci *Item) SetFileContentsFlags(shortflag, longflag, default_path, usage string)

func (Item) StringValue added in v0.1.1

func (ci Item) StringValue() string

type LoadMethod

type LoadMethod int
const (
	LongFlag LoadMethod = iota
	ShortFlag
	EnvVar
	FileContentsLongFlag
	FileContentsShortFlag
	FileContentsDefaultLocation

	JsonFileLocationLongFlag
	JsonFileLocationShortFlag
	JsonFileDefaultLocation

	Error
	LoadMethodCount
)

Jump to

Keyboard shortcuts

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