config

package
v0.0.0-...-2523971 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2019 License: MIT Imports: 7 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispenser

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

Dispenser operates on a stream of tokens and provides some handy methods for structure based decisions and token loading

func NewDispenser

func NewDispenser(name string, input io.Reader) Dispenser

NewDispenser returns a new dispenser for the input reader. It panics if an error is encountered

func NewDispenserFromInput

func NewDispenserFromInput(input Input) Dispenser

NewDispenserFromInput creates a new token dispenser from input. It panics if an error is encountered

func NewDispenserFromTokens

func NewDispenserFromTokens(name string, tokens []Token) Dispenser

NewDispenserFromTokens returns a new dispenser with the given tokens

func (*Dispenser) ArgErr

func (d *Dispenser) ArgErr() error

ArgErr returns a syntax error that an argument has been expected

func (*Dispenser) File

func (d *Dispenser) File() string

File returns the file of the current token

func (*Dispenser) Line

func (d *Dispenser) Line() int

Line returns the line of the current token

func (*Dispenser) Next

func (d *Dispenser) Next() bool

Next loads the next token in the stream. It returns true as long as a next token is available

func (*Dispenser) NextArg

func (d *Dispenser) NextArg() bool

NextArg loads the next token as long as it's on the same line as the current token. It handles newlines in the current token correctly

func (*Dispenser) NextBlock

func (d *Dispenser) NextBlock() bool

NextBlock returns true as long as the next token either opens a new block or is already in a block. It does not support nested blocks

func (*Dispenser) NextLine

func (d *Dispenser) NextLine() bool

NextLine loads the next token as long as it's on the next line or in a different file. Newlines in the current token are handled correctly

func (*Dispenser) RemainingArgs

func (d *Dispenser) RemainingArgs() []string

RemainingArgs returns all remaining arguments form the current line

func (*Dispenser) SetTokenSubstitutionFunc

func (d *Dispenser) SetTokenSubstitutionFunc(fn TokenSubstitutionFunc)

SetTokenSubstitutionFunc sets a function to be called before returning the value of a token. It can be used to apply environment variable substitution or similar

func (*Dispenser) SyntaxErr

func (d *Dispenser) SyntaxErr(f string, args ...interface{}) error

SyntaxErr returns a new syntax error

func (*Dispenser) Unread

func (d *Dispenser) Unread() bool

Unread moves back to the previous token and returns true. If there's not previous token false is returned

func (*Dispenser) Val

func (d *Dispenser) Val() string

Val return the value of the current token

type Input

type Input interface {
	// Name returns the name of the input.
	// It's mostly used for error reporing
	Name() string

	// Content returns the complete content of the interface
	Content() (string, error)
}

Input represents somekind of configuration input source

func FileInput

func FileInput(path string) Input

FileInput returns a configuration Input that is backed by a local file

type LoadFunc

type LoadFunc func() Input

LoadFunc loads the input and returns it. If the loader cannot provide a configuration input it should return nil

func ChainLoader

func ChainLoader(chain ...Loader) LoadFunc

ChainLoader chains configuration loaders and returns the input from the first loader to find one

func FileLoader

func FileLoader(f string) LoadFunc

FileLoader is a config.Loader that returns a file based config input

func (LoadFunc) Load

func (l LoadFunc) Load() Input

Load implements the Loader interface

type Loader

type Loader interface {
	// Load loads the input and returns it. If the loader cannot
	// provide a configuration input it should return nil
	Load() Input
}

Loader is capabole of opeing a configuration input

type Parser

type Parser struct {
	Dispenser
	// contains filtered or unexported fields
}

Parser parses data from a dispenser

func NewParser

func NewParser(file string, r io.Reader) *Parser

NewParser creates a new parser for the given reader. It panics if something goes wrong

func NewParserFromDispenser

func NewParserFromDispenser(d Dispenser) *Parser

NewParserFromDispenser returns a new parser using d

func NewParserFromInput

func NewParserFromInput(input Input) *Parser

NewParserFromInput returns a new parser from the given input. It panics if something goes wrong

func (*Parser) ForEachInOrder

func (p *Parser) ForEachInOrder(directiveOrder []string, fn func(n string, d Dispenser) error) ([]string, error)

ForEachInOrder calls fn for each directive in the order they are listed in directiveOrder. fn is called with a new dispenser that has the whole set of tokens (prefixed and separated with the directive name itself). If fn returns an error the whole iteration is stopped and that error is returned from ForEachInOrder. The returned string slice includes all directive names that have not been handled because they were missing in directiveOrder. Use that slice to detect unknown directives in the config file

func (*Parser) Parse

func (p *Parser) Parse() (map[string][][]Token, error)

Parse parses the complete input file and returns the tokens grouped by directive usage

func (*Parser) UnknownDirectives

func (p *Parser) UnknownDirectives(knowDirectives []string) []string

UnknownDirectives returns a list of directive names that did not appear in knownDirectives. It is mainly meant for error reporting

type SyntaxErr

type SyntaxErr struct {
	// File is the name of the file or input
	File string

	// Line is the line the error occured
	Line int

	// Text is the current token text
	Text string

	// Message is the error message
	Message string
}

SyntaxErr is a syntax error found in a config input

func (*SyntaxErr) Error

func (s *SyntaxErr) Error() string

Error returns a formatted error message for SyntaxErr and implements the error interface

type Token

type Token struct {
	Text string
	Line int
	File string
}

Token is the smallest parsable unit in the configuration file

type TokenSubstitutionFunc

type TokenSubstitutionFunc func(s string) string

TokenSubstitutionFunc is called for each token value and can substitude or change the value of the token

Jump to

Keyboard shortcuts

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