cfg

package module
v0.0.0-...-15789f7 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2024 License: BSD-3-Clause Imports: 12 Imported by: 2

Documentation

Overview

cfg is a library to read config files. Structure is similar but not identical to ini files.

Features: comments, sections, multiline values, validation, reload, shell-out execution

This is not a database replacement. The whole cfg file is processed once on Load()/LoadExec() and returns a Config type.

Rules:

  • A key/value database separated by colons (:). Processed from the beginning to the end.
  • Multiline values need an indent of 2 spaces from the second line on until the last for that value.
  • Comments need a # at the start of the line and is only valid for a key/value pair not as a comment for [main] or a custom section name.
  • Sections need to start with [ and explicitly end with ] in a single line.
  • The default section is called 'main'.
  • Keys can be used in different sections distinctively. If the same key is used in the same section, then the keys last occurence value is set.
  • Keys and Sections should not be longer than 32 chars. You can adjust this through MaxNameSize.
  • Values of keys can not be longer than MaxValueSize (1024) chars. You can adjust this through MaxValueSize.
  • If the data you load returns an empty config, you will receive an error. You can adjust this through EmptyConfig.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	MaxValueSize       int = 1024
	MaxCommentLineSize int = 256
	MaxNameSize        int = 32
	EmptyConfig        bool
)
View Source
var (
	// Amount of seconds to run a shell until canceled on LoadExec() / Reload() calls. / 1 second = 1e+9
	ShellExecTimeout  time.Duration     = 10 * time.Second
	ShellEnvVariables map[string]string = make(map[string]string)
)
View Source
var EnableValidationOnLoad bool

EnableValidationOnLoad forces validation if true and if DefaultValidation is set.

Functions

func Debug

func Debug() bool

Debug toggles (enables/disables) debug output to stdout and returns either true if enabled or respectively false if disabled.

Types

type Comments

type Comments []string

type Config

type Config struct {

	// If DefaultValidation is set, you can explicitily exclude a key from validation. map[section]key
	ValidationExclude map[Name][]Name
	// contains filtered or unexported fields
}

Config holds all loaded data.

func Load

func Load(r io.Reader) (*Config, error)

Load takes data from a Reader and returns a pointer to Config and nil if successful. This function does not execute anything, it just reads.

Example
package main

import (
	"log"
	"os"

	"catinello.eu/cfg"
)

func main() {
	f, err := os.Open("test/test.cfg")
	if err != nil {
		log.Fatal(err)
	}

	_, err = cfg.Load(f)
	if err != nil {
		log.Fatal(err)
	}
}
Output:

func LoadExec

func LoadExec(r io.Reader, restrict string) (*Config, error)

Load takes data from a Reader just like Load() and adds limitation through the restrict value.

Restrict is a list of explicit command line application names (no absolute paths) that will be allowed to execute. Implicit usage is not blocked. If the list is empty, all available commands are potentially allowed!

$SHELL must be bash, zsh, ksh. Since a restricted shell is called. Used commands need to be looked up in $PATH.

func (*Config) Delete

func (c *Config) Delete(section, key Name) error

Delete removes the given key in that section. If the key is empty, the whole section gets wiped.

func (*Config) DisableExec

func (c *Config) DisableExec()

DisableExec() disables the shell out execution on $(cmd) usage if config was created with LoadExec(). Convenience function for the special case that you need to load a config with execution for example just once and do not need it on Reload() calls.

func (*Config) Keys

func (c *Config) Keys(section Name) ([]Name, error)

Keys returns a list of all Keys of section in c, if section exists.

func (*Config) Reload

func (c *Config) Reload(r io.Reader) error

Reload takes data from a Reader and replaces *Config content with the new data.

func (*Config) Sections

func (c *Config) Sections() []Name

Sections returns a list of all sections in c.

func (*Config) Set

func (c *Config) Set(section, key Name, value string) error

Set makes it possible to set a new value to an existing key or a new key in a also potentially new section.

func (*Config) Store

func (c *Config) Store(w io.Writer) error

Store can write the config back to a Writer.

func (*Config) Validator

func (c *Config) Validator(section, key Name, f Validation) error

Validator sets a specific Validation function for the given key in given section of config c. This validation is checked on Value() calls of that given key in section of the used config.

func (*Config) Value

func (c *Config) Value(section, key Name) (string, error)

Value returns the value of key in section of c, if section and key exist.

Example
package main

import (
	"fmt"
	"log"
	"os"

	"catinello.eu/cfg"
)

func main() {
	f, err := os.Open("test/test.cfg")
	if err != nil {
		log.Fatal(err)
	}

	c, err := cfg.Load(f)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(c.Value("main", "foo"))
}
Output:

bar <nil>

type Data

type Data struct {
	Key       Name
	Value     string
	Comment   Comments
	Validator Validation
}

Data consists of a Key/Value combination and additional Comments.

type Name

type Name string

type Sections

type Sections struct {
	Section  Name
	Contents []Data
	Comment  Comments
}

Section contains the name of the section and connected data with possible comments.

type Validation

type Validation func(section, key, value string) bool

Add your validation function to the appropiate objects to validate on Value calls.

var DefaultValidation Validation

DefaultValidation is set to all loaded Data objects and can be overwritten by Validator per Data on Value calls.

Directories

Path Synopsis
cmd
kv

Jump to

Keyboard shortcuts

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