envini

package module
v0.7.1 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2021 License: MIT Imports: 5 Imported by: 0

README

envini

Enironment variable-aware INI reading.

Example

	ini, err := envini.Load("debug.ini")
	if err != nil {
		return nil, err
	}
	ini.ForceUpper()
	ini.GetEnvString("", "hostname")

This loads the file debug.ini and sets parsing to forcing uppercase envvars. If the variable HOSTNAME exists in the environment, it's returned, otherwise the file contents are returned. If all else fails, the variable type's zero value is returned ("" for strings, 0 for integers, 0.0 for floats and false for booleans).

If ForceUpper() wasn't used, it would simply look for hostnameinstead.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolValue

func BoolValue(s string) bool

BoolValue of supplied string.

Types

type INI

type INI struct {
	// Sections hold properties.
	Sections   map[string]*Section
	Properties map[string]*Property
	// contains filtered or unexported fields
}

INI main structure.

func Load

func Load(fn string) (*INI, error)

Load an INI file.

func New

func New() *INI

New INI convenience function.

func Unmarshal

func Unmarshal(s string) *INI

Unmarshal a string with the contents of an INI file.

func (*INI) ForceUpper

func (ini *INI) ForceUpper()

ForceUpper checks the environment for upper-case versions of the supplied INI variables.

func (*INI) GetBool

func (ini *INI) GetBool(s, k string) bool

GetBool returns the boolean value for a variable in the INI file. Non-existense results in false as the return value.

func (*INI) GetEnvBool

func (ini *INI) GetEnvBool(s, k string) bool

GetEnvBool returns GetBool(), overriding with an environment variable if found.

func (*INI) GetEnvFloat

func (ini *INI) GetEnvFloat(s, k string) float64

GetEnvFloat returns GetFloat(), overriding with an environment variable if found.

func (*INI) GetEnvInt

func (ini *INI) GetEnvInt(s, k string) int64

GetEnvInt returns GetInt(), overriding with an environment variable if found.

func (*INI) GetEnvString

func (ini *INI) GetEnvString(s, k string) string

GetEnvString returns a variable from the specified section, or overridden by an environment variable.

func (*INI) GetFloat

func (ini *INI) GetFloat(s, k string) float64

GetFloat returns the float value for a variable in the INI file. Non-existense results in 0 as the return value.

func (*INI) GetInt

func (ini *INI) GetInt(s, k string) int64

GetInt returns the integer value for a variable in the INI file. Non-existense results in 0 as the return value.

func (*INI) GetString

func (ini *INI) GetString(s, k string) string

GetString returns a variable from specified section.

func (*INI) MakeSection

func (ini *INI) MakeSection(name string) *Section

MakeSection will create a new section, or return an existing one with the same name.

func (*INI) Marshal

func (ini *INI) Marshal() string

Marshal INI-structure into a string.

func (*INI) Set

func (ini *INI) Set(s, k, v string)

Set is a convenience method to drill down into the correct section to set a property. The section will be created if missing.

type Property

type Property struct {

	// Key is the name of the property/variable.
	Key string
	// Value is a string. Use Get*() to convert.
	Value string
	// Env variable name.
	Env string
	// contains filtered or unexported fields
}

Property holds a key string and value of any type.

func NewProp

func NewProp(sec *Section, k, v string) *Property

NewProp creates a new property. Keys are set to lowercase.

func (*Property) GetBool

func (p *Property) GetBool() bool

GetBool counts anything but "true", "on" or "enabled" as false.

func (*Property) GetFloat

func (p *Property) GetFloat() float64

GetFloat returns a float64 conversion of the value.

func (*Property) GetInt

func (p *Property) GetInt() int64

GetInt returns an int64 conversion of the value.

func (*Property) Set

func (p *Property) Set(v string)

Set a string.

func (*Property) String

func (p *Property) String() string

String output of the property, with newline.

type Section

type Section struct {
	// Name (title) of the section.
	Name string
	// Properties in this section.
	Properties map[string]*Property
	// contains filtered or unexported fields
}

Section with properties.

func (*Section) GetBool

func (sec *Section) GetBool(k string) bool

GetBool from property in this section, or false if it doesn't exist.

func (*Section) GetFloat

func (sec *Section) GetFloat(k string) float64

GetFloat from property in this section, or 0.0 if it doesn't exist.

func (*Section) GetInt

func (sec *Section) GetInt(k string) int64

GetInt from property in this section, or 0 if it doesn't exist.

func (*Section) GetString

func (sec *Section) GetString(k string) string

GetString from property in this section, or empty if it doesn't exist.

func (*Section) Set

func (sec *Section) Set(k, v string)

Set or replace a property in the section.

func (*Section) SetDirty

func (sec *Section) SetDirty()

SetDirty marks string output for recalculation.

func (*Section) String

func (sec *Section) String() string

String output of the section, with a trailing newline.

Jump to

Keyboard shortcuts

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