config

package
v0.0.0-...-fb7d9fd Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2019 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Code is taken from github.com/caarlos0/env and modified to handle JSON values for examples, please visit the github repo

Package config provides Settings interface for accessing Predix environment variables. The values can be boolean, number, string, or JSON object.

An example shell script for defining and setting a json as value is given here. you can read the JSON or any part of it by providing the environment name as key and the list of object names and array indexes as the path to the value.

e.g. for MY_ENV defined below, Settings.GetEnv("MY_ENV", "array", "1") will return "b"

#!/usr/bin/env bash
#
function define(){
  IFS='\n' read -r -d '' ${1} || true;
}
#
export value="this is a value"
#
define MY_ENV << JSON
{
"home": "$HOME",
"array": ["a", "b", "c"],
"pwd": "`pwd`"
}
JSON
#
export MY_ENV

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotAStructPtr is returned if you pass something that is not a pointer to a
	// Struct to Parse
	ErrNotAStructPtr = errors.New("Expected a pointer to a Struct")
	// ErrUnsupportedType if the struct field type is not supported by env
	ErrUnsupportedType = errors.New("Type is not supported")
	// ErrUnsupportedSliceType if the slice element type is not supported by env
	ErrUnsupportedSliceType = errors.New("Unsupported slice type")
)

Functions

func Parse

func Parse(v interface{}) error
Example
type config struct {
	Home         string `env:"HOME"`
	Port         int    `env:"PORT" envDefault:"3000"`
	IsProduction bool   `env:"PRODUCTION"`
}
os.Setenv("HOME", "/tmp/fakehome")
cfg := config{}
parse(&cfg)
fmt.Println(cfg)
Output:

{/tmp/fakehome 3000 false}

Types

type Settings

type Settings struct {
	Env *cfenv.App

	sync.Mutex
	// contains filtered or unexported fields
}

func AppSettings

func AppSettings() *Settings

func NewSettings

func NewSettings() *Settings

func (Settings) GetBoolEnv

func (r Settings) GetBoolEnv(name string, path ...string) bool

GetEnv returns env boolean value for the given name. If the value is JSON and path is provided, return the part specified.

func (Settings) GetEnv

func (r Settings) GetEnv(name string, path ...string) interface{}

GetEnv returns env value for the given name. If the value is JSON and path is provided, return the part specified.

func (Settings) GetIntEnv

func (r Settings) GetIntEnv(name string, path ...string) int

GetEnv returns env int value for the given name. If the value is JSON and path is provided, return the part specified.

func (Settings) GetService

func (r Settings) GetService(names ...string) interface{}

GetService looks up by name and then by label and returns the service from VCAP_SERVICES environment variable

func (Settings) GetStringEnv

func (r Settings) GetStringEnv(name string, path ...string) string

GetEnv returns env string value for the given name. If the value is JSON and path is provided, return the part specified.

func (Settings) Parse

func (r Settings) Parse(v interface{}) error

func (Settings) PostgresUri

func (r Settings) PostgresUri(a ...string) string

func (Settings) RabbitmqUri

func (r Settings) RabbitmqUri(a ...string) string

func (*Settings) RedisService

func (r *Settings) RedisService(a ...string) interface{}

func (Settings) ServiceUri

func (r Settings) ServiceUri(names ...string) string

ServiceUri looks up by name and then by label and returns service uri from the VCAP_SERVICES environment variable

func (Settings) String

func (r Settings) String() string

Jump to

Keyboard shortcuts

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