Logo

0x3d.Site

is designed for aggregating information.
Welcome
check repository here

Configure

GoDoc

Configure is a Go package that gives you easy configuration of your project through redundancy. It has an API inspired by negroni and the flag package.

What is it?

Configure aims to be the github.com/codegangsta/negroni of configuration. It is a Checker manager, in the same way negroni manages net/http middleware. A Checker is a way of retrieving configuration values from a source: these can be easily made by completing the Checker interface. The idea is that you as a developer provide Configure with a selection of Checkers, either built in or not and it will iterate over them attempting to find values defined by the developer. If a Checker is successful in its retrieval, then Configure will stop the iteration for that value. If it is not then Configure will attempt the next Checker in chronological order.

Getting Started

After you have installed Go (and have made sure to correctly setup your GOPATH) create a new .go file, maybe hello.go.

package main

import (
  "fmt"
  
  "github.com/paked/configure"
)

var (
  conf = configure.New()
  name = conf.String("name", "Harrison", "The name you want to greet")
)

func init() {
  conf.Use(configure.NewEnvironment())
  conf.Use(configure.NewFlag())
}

func main() {
  conf.Parse()
  fmt.Printf("Hello, %v", *name)
}

If you run this code with

go run hello.go

Hello, Harrison will be printed to your terminal, not that interesting right... read on!

Stage One : Declaration

var (
  conf = configure.New()
  name = conf.String("name", "Harrison", "The name you want to greet")
)

The declaration stage is important because it defines exactly what you CAN configure! First, conf is created which is your key to adding Checkers and retrieving variables. After that you begin properly declaring your variables, in this example only a string is declared but in practice you can use any number of Strings, Ints or Bools. The variables returned by these methods are pointers to their respective types.

Stage Two : Configuration

func init() {
  conf.Use(configure.NewEnvironment())
  conf.Use(configure.NewFlag())
}

The configuration stage is where you configure configure by adding Checkers to the stack. Checkers are objects which will attempt to retrieve your variables from their respective data sources. When a Checker fails the next one in the stack is called, the stack is in the same order that the Checker's were added in. You can configure configure anytime before you call the conf.Parse() function, but the init() function provides a reliable place to do so.

note: When using the Environment Checker, all keys will be translated to uppercase and have dashes replaced with underscores (ie. hello-world to HELLO_WORLD).

Stage Three : Usage

func main() {
  conf.Parse()
  fmt.Printf("Hello, %v", *name)
}

The final stage is where you can actually use the variables you have declared. After using conf.Parse() your variables should then be populated and accesible by dereferencing it (name).

Execution

If you were to run this code in its current state it would print Hello, Harrison because Harrison is the default value provided in the declaration stage. But if you provide --name=Johny when you execute the command it will print Hello, Johny. At this point configure is behaving like the default flag package through the Flag Checker. Now, run export NAME=Jarvis in your command line and execute the program again and ommit the entire --name=command line flag. You will see a Hello, Jarvis, as configure has fallen back upon the Environment Checker. Note that, if you provide both means of input the environment variable will be used, as it has higher priority as it was added before the Flag Checker in the configuration stage. This works with any number of Checkers from any source, as long as the fulfil the Checker interface.

Further Reading

More package documentation can be found on godoc.

A more complicated example can be found in the example folder, it uses all three variable types (Int, Bool and String) and all three of the default Checker's (JSON, Environment and Flag).

Contributing

If you notice something that you feel is broken or missing in configure feel free to open up an issue so that we can discuss it more. While small changes could be immediately put into a PR, I believe it saves everyones time to discuss major changes before implementing them. Contributions are welcome and appreciated.

Checkers

NameLocationInitialiserDescription
Environment[builtin] http://github.com/paked/configureNewEnvironment()Environment checks the os environment variables for values
JSON[builtin] http://github.com/paked/configureNewJSON(io.Reader)Retrieves values from an io.Reader containing JSON
Flag[builtin] http://github.com/paked/configureNewFlag()Retrieve flagged values from os.Args in a --x=y format
HCL[builtin] http://github.com/paked/configureNewHCL(io.Reader)Retrieve values from an io.Reader containing HCL

If you write your own Checker I would LOVE to see it, create a PR with a new entry in the table!

Note

As you may have noticed, I am not particularly great at English. If you notice a way to de-garble a few of my sentences be sure to let me know... Not only I, but future readers will be grateful too :)

designed and implemented by Harrison Shoebridge
Golang
Golang
Golang, or Go, is a statically typed programming language developed by Google. It’s known for its simplicity, performance, and support for concurrent programming. Go is widely used in cloud computing, server-side applications, and microservices.
GitHub - goyek/goyek: Task automation Go library
GitHub - goyek/goyek: Task automation Go library
gommon/color at master · labstack/gommon
gommon/color at master · labstack/gommon
GitHub - blevesearch/bleve: A modern text/numeric/geo-spatial/vector indexing library for go
GitHub - blevesearch/bleve: A modern text/numeric/geo-spatial/vector indexing library for go
GitHub - schollz/progressbar: A really basic thread-safe progress bar for Golang applications
GitHub - schollz/progressbar: A really basic thread-safe progress bar for Golang applications
GitHub - free/concurrent-writer: Highly concurrent drop-in replacement for bufio.Writer
GitHub - free/concurrent-writer: Highly concurrent drop-in replacement for bufio.Writer
GitHub - mattn/go-colorable
GitHub - mattn/go-colorable
GitHub - StudioSol/set: A simple Set data structure implementation in Go (Golang) using LinkedHashMap.
GitHub - StudioSol/set: A simple Set data structure implementation in Go (Golang) using LinkedHashMap.
GitHub - essentialkaos/branca: Authenticated encrypted API tokens (IETF XChaCha20-Poly1305 AEAD) for Golang
GitHub - essentialkaos/branca: Authenticated encrypted API tokens (IETF XChaCha20-Poly1305 AEAD) for Golang
Cloud Native Computing Foundation
Cloud Native Computing Foundation
GitHub - sakirsensoy/genv: Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.
GitHub - sakirsensoy/genv: Genv is a library for Go (golang) that makes it easy to read and use environment variables in your projects. It also allows environment variables to be loaded from the .env file.
GitHub - heetch/confita: Load configuration in cascade from multiple backends into a struct
GitHub - heetch/confita: Load configuration in cascade from multiple backends into a struct
GitHub - brianvoe/sjwt: Simple JWT Golang
GitHub - brianvoe/sjwt: Simple JWT Golang
GitHub - subpop/go-ini: Go package that encodes and decodes INI-files
GitHub - subpop/go-ini: Go package that encodes and decodes INI-files
GitHub - DylanMeeus/GoAudio: Go tools for audio processing & creation 🎶
GitHub - DylanMeeus/GoAudio: Go tools for audio processing & creation 🎶
GitHub - daviddengcn/go-colortext: Change the color of console text.
GitHub - daviddengcn/go-colortext: Change the color of console text.
GitHub - andOTP/andOTP: [Unmaintained] Open source two-factor authentication for Android
GitHub - andOTP/andOTP: [Unmaintained] Open source two-factor authentication for Android
GitHub - alfiankan/crab-config-files-templating: Dynamic configuration file templating tool for kubernetes manifest or general configuration files
GitHub - alfiankan/crab-config-files-templating: Dynamic configuration file templating tool for kubernetes manifest or general configuration files
GitHub - viney-shih/go-cache: A flexible multi-layer Go caching library to deal with in-memory and shared cache by adopting Cache-Aside pattern.
GitHub - viney-shih/go-cache: A flexible multi-layer Go caching library to deal with in-memory and shared cache by adopting Cache-Aside pattern.
GitHub - TreyBastian/colourize: An ANSI colour terminal package for Go
GitHub - TreyBastian/colourize: An ANSI colour terminal package for Go
GitHub - Evertras/bubble-table: A customizable, interactive table component for the Bubble Tea framework
GitHub - Evertras/bubble-table: A customizable, interactive table component for the Bubble Tea framework
GitHub - deatil/go-array: A Go package that read or set data from map, slice or json
GitHub - deatil/go-array: A Go package that read or set data from map, slice or json
GitHub - cometbft/cometbft: CometBFT (fork of Tendermint Core): A distributed, Byzantine fault-tolerant, deterministic state machine replication engine
GitHub - cometbft/cometbft: CometBFT (fork of Tendermint Core): A distributed, Byzantine fault-tolerant, deterministic state machine replication engine
GitHub - icza/session: Go session management for web servers (including support for Google App Engine - GAE).
GitHub - icza/session: Go session management for web servers (including support for Google App Engine - GAE).
GitHub - cristalhq/jwt: Safe, simple and fast JSON Web Tokens for Go
GitHub - cristalhq/jwt: Safe, simple and fast JSON Web Tokens for Go
GitHub - lrita/cmap: a thread-safe concurrent map for go
GitHub - lrita/cmap: a thread-safe concurrent map for go
GitHub - wzshiming/ctc: Console Text Colors - The non-invasive cross-platform terminal color library does not need to modify the Print method
GitHub - wzshiming/ctc: Console Text Colors - The non-invasive cross-platform terminal color library does not need to modify the Print method
GitHub - goradd/maps: map library using Go generics that offers a standard interface, go routine synchronization, and sorting
GitHub - goradd/maps: map library using Go generics that offers a standard interface, go routine synchronization, and sorting
GitHub - alfiankan/teleterm: Telegram Bot Exec Terminal Command
GitHub - alfiankan/teleterm: Telegram Bot Exec Terminal Command
GitHub - JeremyLoy/config: 12 factor configuration as a typesafe struct in as little as two function calls
GitHub - JeremyLoy/config: 12 factor configuration as a typesafe struct in as little as two function calls
GitHub - goraz/onion: Layer based configuration for golang
GitHub - goraz/onion: Layer based configuration for golang
Golang
More on Golang

Programming Tips & Tricks

Code smarter, not harder—insider tips and tricks for developers.

Error Solutions

Turn frustration into progress—fix errors faster than ever.

Shortcuts

The art of speed—shortcuts to supercharge your workflow.
  1. Collections 😎
  2. Frequently Asked Question's 🤯

Tools

available to use.

Made with ❤️

to provide resources in various ares.