globalconf

package
v0.9.36 Latest Latest
Warning

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

Go to latest
Published: Jul 7, 2015 License: GPL-3.0, LGPL-2.1-or-later Imports: 7 Imported by: 0

README

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: "MYAPP_",
	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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(flagSetName string, set *flag.FlagSet)

Registers a flag set to be parsed. Register all flag sets before calling this function. flag.CommandLine is automatically registered.

Types

type GlobalConf

type GlobalConf struct {
	Filename  string
	EnvPrefix string
	// contains filtered or unexported fields
}

Represents a GlobalConf context.

func New

func New(appName string) (g *GlobalConf, err error)

Opens/creates a config file for the specified appName. The path to config file is ~/.config/appName/config.ini.

func NewWithOptions

func NewWithOptions(opts *Options) (g *GlobalConf, err error)

NewWithOptions creates a GlobalConf from the provided Options. The caller is responsible for creating any referenced config files.

func (*GlobalConf) Delete

func (g *GlobalConf) Delete(flagSetName, flagName string) error

Deletes a flag from config file and persists the changes to the disk.

func (*GlobalConf) Parse

func (g *GlobalConf) Parse()

Parses all the registered flag sets, including the command line set and sets values from the config file if they are not already set.

func (*GlobalConf) ParseAll

func (g *GlobalConf) ParseAll()

Parses command line flags and then, all of the registered flag sets with the values provided in the config file.

func (*GlobalConf) ParseSet

func (g *GlobalConf) ParseSet(flagSetName string, set *flag.FlagSet)

Parses the config file for the provided flag set. If the flags are already set, values are overwritten by the values in the config file. Defaults are not set if the flag is not in the file.

func (*GlobalConf) Set

func (g *GlobalConf) Set(flagSetName string, f *flag.Flag) error

Sets a flag's value and persists the changes to the disk.

type Options

type Options struct {
	Filename  string
	EnvPrefix string
}

Jump to

Keyboard shortcuts

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