knf

package
v12.20.2+incompatible Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2021 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package knf provides methods for working with configs in KNF format

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetB

func GetB(name string, defvals ...bool) bool

GetB returns global config value as boolean

func GetF

func GetF(name string, defvals ...float64) float64

GetF returns global config value as floating number

func GetI

func GetI(name string, defvals ...int) int

GetI returns global config value as int

func GetI64

func GetI64(name string, defvals ...int64) int64

GetI64 returns global config value as int64

func GetM

func GetM(name string, defvals ...os.FileMode) os.FileMode

GetM returns global config value as file mode

func GetS

func GetS(name string, defvals ...string) string

GetS returns global config value as string

func GetU

func GetU(name string, defvals ...uint) uint

GetU returns global config value as uint

func GetU64

func GetU64(name string, defvals ...uint64) uint64

GetU64 returns global config value as uint64

func Global

func Global(file string) error

Global reads and parses config file Global config will be accessible globally from any part of the code

Example
// Load global config
err := Global("/path/to/your/config.knf")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

// Read string value
GetS("section:string")

// Read integer value
GetI("section:int")

// Read float value
GetF("section:float")

// Read boolean value
GetB("section:boolean")

// Read file mode value
GetM("section:file-mode")

// Check section
if HasSection("section") {
	// Section exist
}

// Check property
if HasProp("section:string") {
	// Property exist
}

// Slice of all sections
Sections()

// Slice of all properties in section
Props("section")
Output:

func HasProp

func HasProp(name string) bool

HasProp check if property exist

func HasSection

func HasSection(section string) bool

HasSection check if section exist

func Props

func Props(section string) []string

Props returns slice with properties names in some section

func Reload

func Reload() (map[string]bool, error)

Reload reads and parse global config file

Example
err := Global("/path/to/your/config.knf")

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

changes, err := Reload()

if err != nil {
	fmt.Printf("Error: %v\n", err)
	return
}

// Print info about changed values
for prop, changed := range changes {
	fmt.Printf("Property %s changed → %t\n", prop, changed)
}
Output:

func Sections

func Sections() []string

Sections returns slice with section names

func Validate

func Validate(validators []*Validator) []error

Validate require slice with pointers to validators and returns slice with validation errors

Types

type Config

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

Config is basic config struct

func Read

func Read(file string) (*Config, error)

Read reads and parse config file

func (*Config) GetB

func (c *Config) GetB(name string, defvals ...bool) bool

GetB returns config value as boolean

func (*Config) GetF

func (c *Config) GetF(name string, defvals ...float64) float64

GetF returns config value as floating number

func (*Config) GetI

func (c *Config) GetI(name string, defvals ...int) int

GetI returns config value as int

func (*Config) GetI64

func (c *Config) GetI64(name string, defvals ...int64) int64

GetI64 returns config value as int64

func (*Config) GetM

func (c *Config) GetM(name string, defvals ...os.FileMode) os.FileMode

GetM returns config value as file mode

func (*Config) GetS

func (c *Config) GetS(name string, defvals ...string) string

GetS returns config value as string

func (*Config) GetU

func (c *Config) GetU(name string, defvals ...uint) uint

GetU returns config value as uint

func (*Config) GetU64

func (c *Config) GetU64(name string, defvals ...uint64) uint64

GetU64 returns config value as uint64

func (*Config) HasProp

func (c *Config) HasProp(name string) bool

HasProp check if property exist

func (*Config) HasSection

func (c *Config) HasSection(section string) bool

HasSection check if section exist

func (*Config) Props

func (c *Config) Props(section string) []string

Props returns slice with properties names in some section

func (*Config) Reload

func (c *Config) Reload() (map[string]bool, error)

Reload read and parse config file

func (*Config) Sections

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

Sections returns slice with section names

func (*Config) Validate

func (c *Config) Validate(validators []*Validator) []error

Validate executes all given validators and returns slice with validation errors

type PropertyValidator

type PropertyValidator func(config *Config, prop string, value interface{}) error

PropertyValidator is default type of property validation function

type Validator

type Validator struct {
	Property string            // Property name
	Func     PropertyValidator // Validation function
	Value    interface{}       // Expected value
}

Validator is config property validator struct

Directories

Path Synopsis
Package validators provides basic KNF validators
Package validators provides basic KNF validators
fs
Package fs provides KNF validators for checking file-system items
Package fs provides KNF validators for checking file-system items
network
Package network provides KNF validators for checking items related to network
Package network provides KNF validators for checking items related to network
regexp
Package regexp provides KNF validators with regular expressions
Package regexp provides KNF validators with regular expressions
system
Package system provides KNF validators for checking system items (user, groups, network interfaces) Package system provides KNF validators for checking system items (user, groups, network interfaces)
Package system provides KNF validators for checking system items (user, groups, network interfaces) Package system provides KNF validators for checking system items (user, groups, network interfaces)

Jump to

Keyboard shortcuts

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