config

package module
v0.1.14 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2020 License: MIT Imports: 4 Imported by: 0

README

Config

release github

pipeline status release status

A Go package to deal with configuration.

Usage

Importing
import "github.com/usvc/config"
Defining configuration

Following example can also be found at ./cmd/config/config.go

var conf = config.Map{
	// with env : BOOL=true
	// with flag: --bool | -b
	"bool": &config.Bool{
		Default:   false,
		Shorthand: "b",
		Usage:     "specifies a boolean value",
	},
	// with env : FLOAT=-123
	// with flag: --float -123 | -f -123
	"float": &config.Float{
		Default:   1.6180339887498948482045868343,
		Shorthand: "f",
		Usage:     "specifies a floating point value",
	},
	// with env : INT=-123
	// with flag: --int -123 | -i -123
	"int": &config.Int{
		Default:   -1,
		Shorthand: "i",
		Usage:     "specifies a signed integer value",
	},
	// with env : INTS="-123 -456"
	// with flag: --int-slice -123,-456 | -I -123,-456
	"int slice": &config.IntSlice{
		Default:   []int{-2, -3},
		Shorthand: "I",
		Usage:     "specifies a slice of signed integers value",
	},
	// with env : STRING=value
	// with flag: --string value | -s value
	"string": &config.String{
		Default:   "default",
		Shorthand: "s",
		Usage:     "specifies a string value",
	},
	// with env : STRING_SLICE="value1 value2"
	// with flag: --string-slice value1,value2 | -S value1,value2
	"string slice": &config.StringSlice{
		Default:   []string{"hello", "world"},
		Shorthand: "S",
		Usage:     "specifies a slice of strings value",
	},
	// with env : UINT=123
	// with flag: --uint 123 | -u 123
	"uint": &config.Uint{
		Default:   1,
		Shorthand: "u",
		Usage:     "specifies an unsigned integer value",
	},
	// with env : UINT_SLICE="123 456"
	// with flag: --uint-slice 123,456 | -U 123,456
	"uint slice": &config.UintSlice{
		Default:   []uint{2, 3},
		Shorthand: "U",
		Usage:     "specifies a slice of unsigned integers value",
	},
}
Consuming from environment

Following example assumes the above conf variable was defined.

conf.LoadFromEnvironment()
Applying to Cobra (github.com/spf13/cobra package)

Following example assumes the above conf variable was defined.

cmd := cobra.Command { /* ... config ... */ }
conf.ApplyToCobra(cmd)
Deciding environment/flag precedence

Following example assumes the above conf variable was defined.

To give priority to environment variables first, call LoadFromEnvironment() outside of the cobra.Command's runtime functions (eg. PreRun/Run)

func main() {
  cmd := cobra.Command{ /* ... config ... */ }
  conf.ApplyToCobra(cmd)
  conf.LoadFromEnvironment()
  cmd.Execute()
}

To give priority to values from flags first, call LoadFromEnvironment() inside one of cobra.Command's runtime functions:

func main() {
  cmd := cobra.Command{
    // ... other config ...
    PreRun: func(c *cobra.Command, args []string) {
      conf.LoadFromEnvironment()
    },
    // ... other config ...
  }
  conf.ApplyToCobra(cmd)
  cmd.Execute()
}
Retrieving values frrom the configuration

Following example assumes the above conf variable was defined.

fmt.Println("bool     : %v", conf.GetValue())
fmt.Println("float    : %v", conf.GetValue())
fmt.Println("int      : %v", conf.GetValue())
fmt.Println("[]int    : %v", conf.GetValue())
fmt.Println("string   : %s", conf.GetValue())
fmt.Println("[]string : %v", conf.GetValue())
fmt.Println("uint     : %v", conf.GetValue())
fmt.Println("[]uint   : %v", conf.GetValue())
Note on *Slice types

For the slice types (IntSlice, StringSlice, UintSlice), when running in flag mode, the delimiter is a comma (,), but in environment variable mode, the delimiter is a space ( ). This is because of how the underlying package (github.com/spf13/viper) does the string splitting.

Note on configuration names

The config.Map is used to define a dictionary of your configuration values using a map[string]config.Config data structure. The string becomes the name of the configuration and this string is manipulated before being passed to the downstream library.

For environment variables, all non-alphanumeric characters will be converted to an underscore (_), and all alphabetic characters will be converted to UPPERCASE_IN_SNAKE_CASE.

For flags, all non-alphanumeric characters will be converted to a hypen (-), and all alphabetic characters will be converted to lowercase-in-kebab-case.

Example CLI Application

The example CLI application can be found in the ./cmd/config directory and includes the configuration as found above. To test it out you can run the following:

# bool
go run ./cmd/config --bool
BOOL=1 go run ./cmd/config

# float
go run ./cmd/config --float 3.142
FLOAT=3.142 go run ./cmd/config

# int
go run ./cmd/config --int -12345
INT=-12345 go run ./cmd/config

# []int
go run ./cmd/config --int-slice -12345 --int-slice -67890
INT_SLICE="-12345 -67890" go run ./cmd/config

# string
go run ./cmd/config --string "hello world"
STRING="hello world" go run ./cmd/config

# []string
go run ./cmd/config --string-slice "hello" --string-slice "world"
STRING_SLICE="hello world" go run ./cmd/config

# uint
go run ./cmd/config --uint 12345
UINT=12345 go run ./cmd/config

# []uint
go run ./cmd/config --uint-slice 12345 --uint-slice 67890
UINT_SLICE="12345 67890" go run ./cmd/config

Development Runbook

Getting Started
  1. Clone this repository
  2. Run make deps to pull in external dependencies
  3. Write some awesome stuff
  4. Run make test to ensure unit tests are passing
  5. Push
Continuous Integration (CI) Pipeline
On Github

Github is used to deploy binaries/libraries because of it's ease of access by other developers.

Releasing

Releasing of the binaries can be done via Travis CI.

  1. On Github, navigate to the tokens settings page (by clicking on your profile picture, selecting Settings, selecting Developer settings on the left navigation menu, then Personal Access Tokens again on the left navigation menu)
  2. Click on Generate new token, give the token an appropriate name and check the checkbox on public_repo within the repo header
  3. Copy the generated token
  4. Navigate to travis-ci.org and access the cooresponding repository there. Click on the More options button on the top right of the repository page and select Settings
  5. Scroll down to the section on Environment Variables and enter in a new NAME with RELEASE_TOKEN and the VALUE field cooresponding to the generated personal access token, and hit Add
On Gitlab

Gitlab is used to run tests and ensure that builds run correctly.

Version Bumping
  1. Run make .ssh
  2. Copy the contents of the file generated at ./.ssh/id_rsa.base64 into an environment variable named DEPLOY_KEY in Settings > CI/CD > Variables
  3. Navigate to the Deploy Keys section of the Settings > Repository > Deploy Keys and paste in the contents of the file generated at ./.ssh/id_rsa.pub with the Write access allowed checkbox enabled
  • DEPLOY_KEY: generate this by running make .ssh and copying the contents of the file generated at ./.ssh/id_rsa.base64
DockerHub Publishing
  1. Login to https://hub.docker.com, or if you're using your own private one, log into yours
  2. Navigate to your security settings at the /settings/security endpoint
  3. Click on Create Access Token, type in a name for the new token, and click on Create
  4. Copy the generated token that will be displayed on the screen
  5. Enter the following varialbes into the CI/CD Variables page at Settings > CI/CD > Variables in your Gitlab repository:
  • DOCKER_REGISTRY_URL: The hostname of the Docker registry (defaults to docker.io if not specified)
  • DOCKER_REGISTRY_USERNAME: The username you used to login to the Docker registry
  • DOCKER_REGISTRY_PASSWORD: The generated access token

Licensing

Code in this package is licensed under the MIT license (click to see full text))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool struct {
	Shorthand string
	Usage     string
	Default   bool
	Value     bool
}

func (*Bool) ApplyToFlagSet

func (s *Bool) ApplyToFlagSet(name string, flags *pflag.FlagSet)

func (*Bool) GetDefault

func (s *Bool) GetDefault() interface{}

func (*Bool) GetShorthand

func (s *Bool) GetShorthand() string

func (*Bool) GetUsage

func (s *Bool) GetUsage() string

func (*Bool) GetValue

func (s *Bool) GetValue() interface{}

func (*Bool) GetValuePointer

func (s *Bool) GetValuePointer() interface{}

func (*Bool) SetValue

func (s *Bool) SetValue(value interface{})

type Config

type Config interface {
	ApplyToFlagSet(name string, flagset *pflag.FlagSet)
	GetDefault() interface{}
	GetShorthand() string
	GetUsage() string
	GetValue() interface{}
	GetValuePointer() interface{}
	SetValue(value interface{})
}

type Float

type Float struct {
	Shorthand string
	Usage     string
	Default   float64
	Value     float64
}

func (*Float) ApplyToFlagSet

func (s *Float) ApplyToFlagSet(name string, flags *pflag.FlagSet)

func (*Float) GetDefault

func (s *Float) GetDefault() interface{}

func (*Float) GetShorthand

func (s *Float) GetShorthand() string

func (*Float) GetUsage

func (s *Float) GetUsage() string

func (*Float) GetValue

func (s *Float) GetValue() interface{}

func (*Float) GetValuePointer

func (s *Float) GetValuePointer() interface{}

func (*Float) SetValue

func (s *Float) SetValue(value interface{})

type Int

type Int struct {
	Shorthand string
	Usage     string
	Default   int
	Value     int
}

func (*Int) ApplyToFlagSet

func (s *Int) ApplyToFlagSet(name string, flags *pflag.FlagSet)

func (*Int) GetDefault

func (s *Int) GetDefault() interface{}

func (*Int) GetShorthand

func (s *Int) GetShorthand() string

func (*Int) GetUsage

func (s *Int) GetUsage() string

func (*Int) GetValue

func (s *Int) GetValue() interface{}

func (*Int) GetValuePointer

func (s *Int) GetValuePointer() interface{}

func (*Int) SetValue

func (s *Int) SetValue(value interface{})

type IntSlice

type IntSlice struct {
	Shorthand string
	Usage     string
	Default   []int
	Value     []int
}

func (*IntSlice) ApplyToFlagSet

func (s *IntSlice) ApplyToFlagSet(name string, flags *pflag.FlagSet)

func (*IntSlice) GetDefault

func (s *IntSlice) GetDefault() interface{}

func (*IntSlice) GetShorthand

func (s *IntSlice) GetShorthand() string

func (*IntSlice) GetUsage

func (s *IntSlice) GetUsage() string

func (*IntSlice) GetValue

func (s *IntSlice) GetValue() interface{}

func (*IntSlice) GetValuePointer

func (s *IntSlice) GetValuePointer() interface{}

func (*IntSlice) SetValue

func (s *IntSlice) SetValue(value interface{})

type Map

type Map map[string]Config

func (*Map) ApplyToCobra

func (m *Map) ApplyToCobra(command *cobra.Command)

func (Map) Get added in v0.1.8

func (m Map) Get(id string) interface{}

func (Map) GetBool added in v0.1.4

func (m Map) GetBool(id string) bool

func (Map) GetFloat added in v0.1.4

func (m Map) GetFloat(id string) float64

func (Map) GetInt added in v0.1.4

func (m Map) GetInt(id string) int

func (Map) GetIntSlice added in v0.1.4

func (m Map) GetIntSlice(id string) []int

func (Map) GetString added in v0.1.4

func (m Map) GetString(id string) string

func (Map) GetStringSlice added in v0.1.4

func (m Map) GetStringSlice(id string) []string

func (Map) GetUint added in v0.1.4

func (m Map) GetUint(id string) uint

func (Map) GetUintSlice added in v0.1.4

func (m Map) GetUintSlice(id string) []uint

func (*Map) LoadFromEnvironment added in v0.1.8

func (m *Map) LoadFromEnvironment()

type String

type String struct {
	Shorthand string
	Usage     string
	Default   string
	Value     string
}

func (*String) ApplyToFlagSet

func (s *String) ApplyToFlagSet(name string, flags *pflag.FlagSet)

func (*String) GetDefault

func (s *String) GetDefault() interface{}

func (*String) GetShorthand

func (s *String) GetShorthand() string

func (*String) GetUsage

func (s *String) GetUsage() string

func (*String) GetValue

func (s *String) GetValue() interface{}

func (*String) GetValuePointer

func (s *String) GetValuePointer() interface{}

func (*String) SetValue

func (s *String) SetValue(value interface{})

type StringSlice

type StringSlice struct {
	Shorthand string
	Usage     string
	Default   []string
	Value     []string
}

func (*StringSlice) ApplyToFlagSet

func (s *StringSlice) ApplyToFlagSet(name string, flags *pflag.FlagSet)

func (*StringSlice) GetDefault

func (s *StringSlice) GetDefault() interface{}

func (*StringSlice) GetShorthand

func (s *StringSlice) GetShorthand() string

func (*StringSlice) GetUsage

func (s *StringSlice) GetUsage() string

func (*StringSlice) GetValue

func (s *StringSlice) GetValue() interface{}

func (*StringSlice) GetValuePointer

func (s *StringSlice) GetValuePointer() interface{}

func (*StringSlice) SetValue

func (s *StringSlice) SetValue(value interface{})

type Uint

type Uint struct {
	Shorthand string
	Usage     string
	Default   uint
	Value     uint
}

func (*Uint) ApplyToFlagSet

func (s *Uint) ApplyToFlagSet(name string, flags *pflag.FlagSet)

func (*Uint) GetDefault

func (s *Uint) GetDefault() interface{}

func (*Uint) GetShorthand

func (s *Uint) GetShorthand() string

func (*Uint) GetUsage

func (s *Uint) GetUsage() string

func (*Uint) GetValue

func (s *Uint) GetValue() interface{}

func (*Uint) GetValuePointer

func (s *Uint) GetValuePointer() interface{}

func (*Uint) SetValue

func (s *Uint) SetValue(value interface{})

type UintSlice

type UintSlice struct {
	Shorthand string
	Usage     string
	Default   []uint
	Value     []uint
}

func (*UintSlice) ApplyToFlagSet

func (s *UintSlice) ApplyToFlagSet(name string, flags *pflag.FlagSet)

func (*UintSlice) GetDefault

func (s *UintSlice) GetDefault() interface{}

func (*UintSlice) GetShorthand

func (s *UintSlice) GetShorthand() string

func (*UintSlice) GetUsage

func (s *UintSlice) GetUsage() string

func (*UintSlice) GetValue

func (s *UintSlice) GetValue() interface{}

func (*UintSlice) GetValuePointer

func (s *UintSlice) GetValuePointer() interface{}

func (*UintSlice) SetValue

func (s *UintSlice) SetValue(value interface{})

Directories

Path Synopsis
cmd
config command

Jump to

Keyboard shortcuts

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