cfg

package
v0.0.0-...-fb9b9fb Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2017 License: MIT Imports: 6 Imported by: 0

README

cfg

A package to parse configuration files

Why

Yeah yeah, there's YAML and JSON. They're great if you want to read straight into a struct. But when you want optional parameters, Go code starts to look clunky. You need to rely on []Interface{}([]byte) a little too much. This package allows you to query for parameters and uses a format similar to the one found in Java's Properties.

Usage

A cfg object is built from a file:

import (
  "github.com/tokyo/src/cfg"
)

cfg := cfg.New("file.txt")

Config files contain key-value pairs, each on a single line. Please refer to the following config contents when reading the examples below:

LogFile log/prod.log
LogLevel Most

connection.dev.user techops
connection.dev.host 10.1.1.1
connection.dev.port 2345

connection.prod.user techops
connection.prod.host 10.1.1.1
connection.prod.port 2345

# Comments start with a hash!
email [email protected]
email [email protected]
email [email protected]

Keys

Keys are contiguous strings that can be subdivided into segments using dot (".") as a delimiter. A key may NOT contain whitespace characters. If a key contains dots then cfg can interpret it as a concatenation of sub-keys.

Accessing complete keys:

cfg.Has("LogFile")             // True
cfg.Has("LogFileX")            // False
cfg.Has("connection.dev.user") // True
cfg.Has("connection.dev")      // False - Need complete key

cfg.Get("LogFile") // "log/prod.log"
cfg.Get("Junk")    // Dies with Exit(1)
cfg.Get("email")   // Dies with Exit(1) - duplicate keys, use GetN() - see below

Notice that the key email occurs multiple times. In this case calling Get("email") will error out. You must use the method GetN() to access duplicate keys (see section Duplicate Keys below).

Sub-Keys

You can segment keys out into sub-keys by using the dot character (".") as a delimiter. The Has and Get methods can take multiple strings as arguments. They will join the supplied strings with dots and treat the result as a complete key:

cfg.Has("connection", "dev", "user") // True - Equivalent to cfg.Has("connection.dev.user")
cfg.Has("connection", "dev")         // False - Need complete key

cfg.Has("connection", "dev", "user") // "techops" - Equivalent to cfg.Get("connection.dev.user")

The cfg package allows you to discover sub-keys for a given prefix:

cfg.SubKeys("connection")        // ["dev", "user"]          - All sub-keys for the prefix "connection"
cfg.SubKeys("connection", "dev") // ["user", "host", "port"] - All sub-keys for the prefix "connection.dev"
cfg.SubKeys("connection.dev")    // ["user", "host", "port"] - Same

To check if a particular sub-key exists for a given prefix, use HasSubKey. The method HasSubKey needs at least two string arguments. The final argument is the sub-key sought. The preceeding arguments comprise the prefix:

cfg.HasSubKey("connection", "dev")         // True  - The prefix "connection" has a sub-key named "dev"
cfg.HasSubKey("connection", "stg")         // False - The prefix "connection" has no sub-key named "stg"
cfg.HasSubKey("connection", "dev", "user") // True  - The prefix "connection.dev" has a sub-key named "user"

If you want to check if a particular prefix exists, use HasPrefix():

cfg.HasPrefix("connection")                // True  - The prefix "connection" exists
cfg.HasPrefix("connection", "dev")         // True  - The prefix "connection.dev" exists
cfg.HasPrefix("connection", "stg")         // False - The prefix "connection.stg" does not exist
cfg.HasPrefix("connection", "dev", "user") // False - The prefix "connection.dev.user" is NOT a prefix, it is a complete key

Duplicate Keys

The cfg package supports duplicate keys. The methods Size and GetN can be used to iterate over values.

cfg.Size("email")    // 3 - The key "email" occurs three times

cfg.GetN(0, "email") // "[email protected]"
cfg.GetN(1, "email") // "[email protected]"
cfg.GetN(2, "email") // "[email protected]"

Sub-Configs

You can extract configurations grouped under a common prefix by using the Descend method:

cfg := cfg.New("file.txt")
dev := cfg.Descend("connection", "dev")

Then the contents of dev is:

user techops
host 10.1.1.1
port 2345

The Descend method returns a newly created Config instance. Its keys are all the keys of the original configuration that matched the supplied prefix. However, the prefix is stripped in the new configuration. In the example above, the prefix "connection.dev" was matched by three entries. Upon removing the prefix from those entries, the resulting keys are user, host and port. If an unrecognized prefix is passed to the Descend method, it will return a newly created Config instance with no entries.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

Config holds an ordered list of entries. Config also stores a "stem" to help make error messages more readable when dealing with descended configs.

func New

func New(filename string) *Config

New returns a new Config object constructed using the supplied filename

func (*Config) Descend

func (c *Config) Descend(stems ...string) *Config

Descend returns a newly created Config containing all key-value pairs of the original Config whose keys match the supplied prefix. The keys of the new Config are the original keys with the prefix removed. If there are no keys that match the supplied prefix, the returned Config will have no entries (but will still be a valid Config object).

Config ------ server.user Bruce server.host 10.1.1.1 server.port 1234 menu lunch persons 12

Descend("server") ----------------- user Bruce host 10.1.1.1 port 1234

func (*Config) Get

func (c *Config) Get(key ...string) string

Get returns the value for the given key. If the key does not exist it exits(1). If there are multiple occurrences of the key it exits(1)

func (*Config) GetN

func (c *Config) GetN(i int, key ...string) string

GetN returns the Nth value for the given key.

func (*Config) Has

func (c *Config) Has(key ...string) bool

Has returns true if the key occurs.

func (*Config) HasPrefix

func (c *Config) HasPrefix(stems ...string) bool

HasPrefix ...

func (*Config) HasSubKey

func (c *Config) HasSubKey(stems ...string) bool

HasSubKey ...

func (*Config) Is

func (c *Config) Is(key, val string) bool

Is returns true if the value for the given key matches the supplied value. If the key passed does not exist it exits(1). If there are multiple occurrences of the key it exits(1)

func (*Config) Size

func (c *Config) Size(key ...string) int

Size returns the number of occurrences of the supplied key.

func (*Config) SubKeys

func (c *Config) SubKeys(stems ...string) []string

SubKeys returns the sub-keys for the given prefix.

Given: ------ db.us.host www.host.com db.us.port 1234 db.gb.host www.host.com db.gb.port 1234

Then: ----- SubKeys("db") => ["us", "gb"] SubKeys("db", "us") => ["host", "port"] SubKeys("db", "us", "port") => []

Jump to

Keyboard shortcuts

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