• Stars
    star
    275
  • Rank 144,288 (Top 3 %)
  • Language
    Go
  • Created over 10 years ago
  • Updated over 3 years ago

Reviews

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

Repository Details

Persist flag values into an ini file

globalconf

Build Status

Effortlessly persist/retrieve flags of your Golang programs. If you need global configuration instead of requiring user always to set command line flags, you are looking at the right package. globalconf allows your users to not only provide flags, but config files and environment variables as well.

Usage

import "github.com/rakyll/globalconf"

Loading a config file

By default, globalconf provides you a config file under ~/.config/<yourappname>/config.ini.

globalconf.New("appname") // loads from ~/.config/<appname>/config.ini

If you don't prefer the default location you can load from a specified path as well.

globalconf.NewWithOptions(&globalconf.Options{
	Filename: "/path/to/config/file",
})

You may like to override configuration with env variables. See "Environment variables" header to see how to it works.

globalconf.NewWithOptions(&globalconf.Options{
	Filename:  "/path/to/config/file",
	EnvPrefix: "APPCONF_",
})

Parsing flag values

globalconf populates flags with data in the config file if they are not already set.

var (
	flagName    = flag.String("name", "", "Name of the person.")
	flagAddress = flag.String("addr", "", "Address of the person.")
)

Assume the configuration file to be loaded contains the following lines.

name = Burcu
addr = Brandschenkestrasse 110, 8002

And your program is being started, $ myapp -name=Jane

conf, err := globalconf.New("myapp")
conf.ParseAll()

*flagName is going to be equal to Jane, whereas *flagAddress is Brandschenkestrasse 110, 8002, what is provided in the configuration file.

Custom flag sets

Custom flagsets are supported, but required registration before parse is done. The default flagset flag.CommandLine is automatically registered.

globalconf.Register("termopts", termOptsFlagSet)
conf.ParseAll() // parses command line and all registered flag sets

Custom flagset values should be provided in their own segment. Getting back to the sample ini config file, termopts values will have their own segment.

name = Burcu
addr = Brandschenkestrasse 110, 8002

[termopts]
color = true
background = ff0000

Environment variables

If an EnvPrefix is provided, environment variables will take precedence over values in the configuration file. Set the EnvPrefix option when calling globalconf.NewWithOptions. An EnvPrefix will only be used if it is a non-empty string. Command line flags will override the environment variables.

opts := globalconf.Options{
	EnvPrefix: "APPCONF_",
	Filename:  "/path/to/config",
}
conf, err := globalconf.NewWithOptions(&opts)
conf.ParseAll()

With environment variables:

APPCONF_NAME = Burcu

and configuration:

name = Jane
addr = Brandschenkestrasse 110, 8002

name will be set to "burcu" and addr will be set to "Brandschenkestrasse 110, 8002".

Modifying stored flags

Modifications are persisted as long as you set a new flag and your GlobalConf object was configured with a filename.

f := &flag.Flag{Name: "name", Value: val}
conf.Set("", f) // if you are modifying a command line flag

f := &flag.Flag{Name: "color", Value: val}
conf.Set("termopts", color) // if you are modifying a custom flag set flag

Deleting stored flags

Like Set, Deletions are persisted as long as you delete a flag's value and your GlobalConf object was configured with a filename.

conf.Delete("", "name") // removes command line flag "name"s value from config
conf.Delete("termopts", "color") // removes "color"s value from the custom flag set

License

Copyright 2014 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Analytics

More Repositories

1

hey

HTTP load generator, ApacheBench (ab) replacement
Go
17,071
star
2

boom

HTTP(S) load generator, ApacheBench (ab) replacement, written in Go
Go
4,745
star
3

statik

Embed files into a Go executable
Go
3,649
star
4

go-hardware

A directory of hardware related libs, tools, and tutorials for Go
Go
1,329
star
5

gom

A visual interface to work with runtime profiling data for Go
Go
1,295
star
6

gotest

go test with colors
Go
1,242
star
7

coop

Cheat sheet for some of the common concurrent flows in Go
Go
1,214
star
8

autopprof

Pprof made easy at development time for Go
Go
738
star
9

drive

Pull or push Google Drive files
Go
465
star
10

openai-go

Go client libraries for OpenAI
Go
443
star
11

go-test-trace

Go test with tracing.
Go
381
star
12

govalidate

Validates your Go installation and dependencies.
Go
335
star
13

portmidi

Go bindings for libportmidi
Go
282
star
14

magicmime

Go bindings for libmagic to detect MIME types
Go
216
star
15

ticktock

A cron job scheduler for Go
Go
209
star
16

go2xcode

Go package to Xcode project generator
Go
177
star
17

fake-it-til-you-make-it

A program that demonstrates that GitHub contribution graph can be cheated
Go
147
star
18

golambda

AWS Lambda Go functions made easy...
Go
137
star
19

launchpad

Talk to your Novation Launchpad in Go
Go
130
star
20

pprof-merge

Merge multiple pprof profile files into a single file
Go
118
star
21

events2prom

A metric collection daemon allows you to configure aggregations in runtime
Go
107
star
22

goproxy-s3

Go proxy that serves from S3
Go
104
star
23

go-sql-driver-spanner

Google Cloud Spanner driver for Go
Go
89
star
24

awesome-spanner

A curated list of awesome Google Cloud Spanner references, tools, libraries and more.
82
star
25

coinsensus

Decentralized distributed systems consensus
62
star
26

opencensus-grpc-demo

Export metrics and traces from gRPC servers and clients
Java
59
star
27

spannerz

Google Cloud Spanner Query Planner Visualizer
Go
59
star
28

aws-containers

My personal mind map of container platforms and tools on AWS
53
star
29

gowiki

Personal fork of Go wiki
47
star
30

command

CLI subcommands for Go
Go
44
star
31

trace

Drafts of a Dapper-style tracing client for Go
Go
44
star
32

obs-luts

LUT files to use with OBS
43
star
33

littlebits

littleBits Go module -- an io.Reader and io.Writer to read from and write to a circuit
Go
40
star
34

dfanout

A simple HTTP/2 fanout server
Go
38
star
35

goutil

Various Go utility packages.
Go
37
star
36

gce-metadata

GCE metadata command line tool
Go
32
star
37

myko

A simple attribution engine.
Go
28
star
38

log-to-xray

Write log entries, get X-Ray traces.
Go
27
star
39

pprof-upload

Upload pprof output to continuous profiler
Go
26
star
40

keys

Let your golang programs store and access passwords in a secure way
Go
26
star
41

go-numa

NUMA bindings for Go, requires libnuma.
Go
24
star
42

httpaa

Where http.HaandleFunc and alike lives.
Go
23
star
43

drummachine

Akai MPD18 replica written in Go, runs on mobile devices
Go
21
star
44

go-xcode

Reference Xcode project to build iOS apps with Go
Makefile
18
star
45

firmata

Firmata client for Go
Arduino
18
star
46

k8s-helloworld-leaderelection

Go
16
star
47

audio

Go
15
star
48

bubblr

Android client for App Engine Channels API
Java
15
star
49

rrqueue

Simple priority queues and round robin consumer for golang
Go
15
star
50

cosmicbackgroundmusic

Go
13
star
51

mysql-args-anonymizer

Go
13
star
52

sensors

Experiments with Android NDK sensors and Go
Go
13
star
53

redfail

Simple command to colorize the stderr of a target program
Go
12
star
54

opentelemetry-metric-go

Experiments, no real code.
Go
12
star
55

node-plussignin

Google+ Sign In middleware for Connect.
JavaScript
12
star
56

gcp-connectivitytests

Google Cloud Connectivity Tests
Go
11
star
57

perf2cloudprofiler

Upload perf output to Google Cloud profiler from anywhere
Go
10
star
58

drivefuse

Google Drive for Linux, BSD and MacOSX
C
10
star
59

grpcutil

Go
9
star
60

spanner-dotfiles

A set of personal aliases to use with Google Cloud Spanner.
7
star
61

talks

7
star
62

go-brillo

Go development notes for the Brillo targets. (External contributors who are willing to contribute are welcomed to comment.)
7
star
63

go-benchmarks

6
star
64

al

OpenAL bindings for Go (work-in-progress)
6
star
65

janis

The missing components of Android SDK
Java
6
star
66

pizza

rakyll.pizza
Go
6
star
67

rakyll.github.io

HTML
5
star
68

blinker

A tiny Raspberry Pi program controlled by a Go mobile app
Go
5
star
69

pubsub

Go
5
star
70

rakyll

Public profile...
5
star
71

refutil

Personal collection of reflection utils for Go
Go
4
star
72

benchmarks

Interesting Go benchmarks to watch
Go
4
star
73

experimental

Experimental Go packages
Go
3
star
74

node-ini

Simple ini reader for Node.js
JavaScript
3
star
75

snippetr

Extract code snippets from source code.
JavaScript
3
star
76

go0

$ docker run -i -t rakyll/go0
Dockerfile
2
star
77

ecs-metadata-proxy

Proxy to debug metadata server in ECS tasks
Go
2
star
78

aestaticdeploy

Takes a static directory and serves it on Google App Engine
Go
2
star
79

zigot

En extensible protocol for cloud storage, against of proprietary APIs.
2
star
80

appdatapreferences-localstorage

Syncs your local storage to Google Drive's appdata folder.
JavaScript
2
star
81

skeleton

skeletons for go projects
Go
1
star
82

travis-build

Go
1
star
83

trace2

Go
1
star
84

allgo

All Go is a repo to be used to test godoc command and godoc.org
Go
1
star
85

dockerfiles

All images are ARMv7
1
star
86

reviews

Catch all repo for reviewing things.
1
star