• Stars
    star
    67
  • Rank 462,595 (Top 10 %)
  • Language
    Go
  • License
    MIT License
  • Created almost 10 years ago
  • Updated about 5 years ago

Reviews

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

Repository Details

Go configuration made easy!

gofigure GoDoc Build Status Coverage Status

Go configuration made easy!

  • Just define a struct and call Gofigure
  • Supports strings, ints/uints/floats, slices and nested structs
  • Supports environment variables and command line flags

Requires Go 1.2+ because of differences in Go's flag package.

Example

go get github.com/ian-kent/gofigure

package main

import "github.com/ian-kent/gofigure"

type config struct {
  gofigure interface{} `envPrefix:"BAR" order:"flag,env"`
  RemoteAddr string `env:"REMOTE_ADDR" flag:"remote-addr" flagDesc:"Remote address"`
  LocalAddr  string `env:"LOCAL_ADDR" flag:"local-addr" flagDesc:"Local address"`
  NumCPU int `env:"NUM_CPU" flag:"num-cpu" flagDesc:"Number of CPUs"`
  Sources []string `env:"SOURCES" flag:"source" flagDesc:"Source URL (can be provided multiple times)"`
  Numbers []int `env:"NUMBERS" flag:"number" flagDesc:"Number (can be provided multiple times)"`
  Advanced struct{
      MaxBytes int64 `env:"MAX_BYTES" flag:"max-bytes" flagDesc:"Max bytes"`
      MaxErrors int64  `env:"MAX_ERRORS" flag:"max-errors" flagDesc:"Max errors"`
  }
}

func main() {
  var cfg config
  err := gofigure.Gofigure(&cfg)
  if err != nil {
    log.Fatal(err)
  }
  // use cfg
}

gofigure field

The gofigure field is used to configure Gofigure.

The order tag is used to set configuration source order, e.g. environment variables first then command line options second.

Any field matching camelCase format will be parsed into camel and case, and passed to the source matching camel.

For example, the envPrefix field is split into env and prefix, and the tag value is passed to the environment variable source as the prefix parameter.

Arrays and environment variables

Array support for environment variables is currently experimental.

To enable it, set GOFIGURE_ENV_ARRAY=1.

When enabled, the environment variable is split on commas, e.g.

struct {
    EnvArray []string `env:"MY_ENV_VAR"`
}

MY_ENV_VAR=a,b,c

EnvArray = []string{"a", "b", "c"}

Licence

Copyright Β©β€Ž 2014, Ian Kent (http://www.iankent.eu).

Released under MIT license, see LICENSE for details.

More Repositories

1

gptchat

A GPT-4 client which gives your favourite AI a memory and tools for self-improvement
Go
337
star
2

linkio

Simulate network link speed
Go
53
star
3

go-log

A logger, for Go
Go
42
star
4

purl

Perl, but fluffy like a cat!
Go
40
star
5

goose

Server-Sent Events in Go
Go
34
star
6

ikettle2

Hacks for the Smarter iKettle 2.0
Java
18
star
7

MailHog

Inspired by MailCatcher, based on M3MTA
Perl
14
star
8

vagrant-scala-play-mongodb

Vagrant build for Ubuntu with Scala, Play Framework and MongoDB
13
star
9

gopan

Perl dependency management toolchain
Go
12
star
10

envconf

Configure Go applications from the environment
Go
11
star
11

production-ready-go

Code samples from Production Ready Go talk at Golang UK 2017
Go
10
star
12

M3MTA

Experimental Mojolicious IOLoop mail transfer agent with MongoDB backend
Perl
7
star
13

atom-gradient-syntax-theme

Animated gradient syntax highlighting
CSS
5
star
14

cachebot

A Slack bot for CloudFlare
Go
4
star
15

cardiff-go-production-ready-go

Go
3
star
16

ssbs

Super Simple Build Server
Go
3
star
17

alexa

Go library for creating Amazon Alexa skills with Apex
Go
3
star
18

go-angularjs-jquery-bootstrap-boilerplate

Go + AngularJS + jQuery + Bootstrap boilerplate web application
Go
3
star
19

gotcha

A cloud-friendly web application framework in Go
Go
2
star
20

service.go

A microservice framework for Go
Go
2
star
21

RhUnit

A qUnit compatible javascript unit testing framework for Rhino
JavaScript
2
star
22

go-webapp

Demonstration Go web application
Go
1
star
23

ktee

Tee to a Kafka topic (see https://github.com/ian-kent/ktail)
Go
1
star
24

sfs

Static File Server
Go
1
star
25

bash-env

Bash environment configuration
Shell
1
star
26

dp-florence-api

POC for replacement Florence API
Go
1
star
27

automated-docs

JavaScript
1
star
28

htmlform

HTML form handling for Go
Go
1
star
29

imagestore

S3 backed imagestore for Marathon
Go
1
star
30

packer-ansible-centos-minimal

CentOS 6.5 minimal packer build with ansible install
Shell
1
star
31

civil-service-competency-framework

The (UK) Civil Service Competency Framework - in JSON and TOML
Go
1
star
32

ian-kent.github.io

CSS
1
star
33

learning-elm

Me learning Elm
Elm
1
star
34

vagrant-perl-mojolicious-mongodb

Vagrant build for Ubuntu with Perl, Mojolicious and MongoDB
Shell
1
star