service_config

package
v0.0.0-...-3b3f045 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2015 License: Apache-2.0, Apache-2.0 Imports: 9 Imported by: 0

README

service-config

Build Status

Tiny Go library for flexible config file loading.

Why?

To allow a service to be configured in multiple different ways:

  • Enable usage by both humans and machines
  • Enable usage both locally and remote
  • Optionally satisfy 12 factor constraints

Features

Loading mechanisms (in order of precedence):

  • Config as flag json string: -config={ "key": "value" }
  • Config as flag path to json file: -configPath=/path/to/config.json
  • The -h flag will print the above flags, as well as any defaults specified using the AddDefaults method
  • Config as environment variable json string: CONFIG={ "key": "value" }
  • Config as environment variable path to json file: CONFIG_PATH=/path/to/config.json

Usage

See the example service for usage.

Dependencies

service-config depends on candiedyaml for json & yaml parsing and on mergo merging defaults.

$ go get -u github.com/cloudfoundry-incubator/candiedyaml
$ go get -u github.com/imdario/mergo

Limitations

A non-empty default value can not be overridden by an empty provided value.

To allow empty values in the config properties (e.g. -config={ "password": "" }), the default value for that property must also be empty.

For example, the following default config would allow a blank password to be provided:

serviceConfig.AddDefaults(Config{
   password: "",
})

While this example would not allow the provided password to be blank:

serviceConfig.AddDefaults(Config{
   password: "password",
})

License

Copyright 2015 Pivotal Software, Inc

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.

Documentation

Index

Constants

View Source
const (
	ConfigPathEnvVar = "CONFIG_PATH"
	ConfigEnvVar     = "CONFIG"
)

Variables

View Source
var NoConfigError = errors.New("No Config or Config Path Specified. Please supply one of the following: -config, -configPath, CONFIG, or CONFIG_PATH")

Functions

This section is empty.

Types

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

func NewReader

func NewReader(configBytes []byte) *Reader

func (Reader) Read

func (r Reader) Read(model interface{}) error

func (Reader) ReadWithDefaults

func (r Reader) ReadWithDefaults(model interface{}, defaults interface{}) error

type ServiceConfig

type ServiceConfig struct {
	// contains filtered or unexported fields
}

func New

func New() *ServiceConfig

func (*ServiceConfig) AddDefaults

func (c *ServiceConfig) AddDefaults(defaultModel interface{})

func (*ServiceConfig) AddFlags

func (c *ServiceConfig) AddFlags(flagSet *flag.FlagSet)

func (ServiceConfig) ConfigBytes

func (c ServiceConfig) ConfigBytes() ([]byte, error)

func (ServiceConfig) ConfigPath

func (c ServiceConfig) ConfigPath() string

func (ServiceConfig) PrintUsage

func (c ServiceConfig) PrintUsage()

func (ServiceConfig) Read

func (c ServiceConfig) Read(model interface{}) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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