Documentation ¶
Overview ¶
Package knf provides methods for working with configuration files in KNF format
Index ¶
- Variables
- func GetB(name string, defvals ...bool) bool
- func GetD(name string, mod DurationMod, defvals ...time.Duration) time.Duration
- func GetF(name string, defvals ...float64) float64
- func GetI(name string, defvals ...int) int
- func GetI64(name string, defvals ...int64) int64
- func GetM(name string, defvals ...os.FileMode) os.FileMode
- func GetS(name string, defvals ...string) string
- func GetU(name string, defvals ...uint) uint
- func GetU64(name string, defvals ...uint64) uint64
- func Global(file string) error
- func HasProp(name string) bool
- func HasSection(section string) bool
- func Is(name string, value any) bool
- func Props(section string) []string
- func Reload() (map[string]bool, error)
- func Sections() []string
- func Validate(validators []*Validator) []error
- type Config
- func (c *Config) File() string
- func (c *Config) GetB(name string, defvals ...bool) bool
- func (c *Config) GetD(name string, mod DurationMod, defvals ...time.Duration) time.Duration
- func (c *Config) GetF(name string, defvals ...float64) float64
- func (c *Config) GetI(name string, defvals ...int) int
- func (c *Config) GetI64(name string, defvals ...int64) int64
- func (c *Config) GetM(name string, defvals ...os.FileMode) os.FileMode
- func (c *Config) GetS(name string, defvals ...string) string
- func (c *Config) GetU(name string, defvals ...uint) uint
- func (c *Config) GetU64(name string, defvals ...uint64) uint64
- func (c *Config) HasProp(name string) bool
- func (c *Config) HasSection(section string) bool
- func (c *Config) Is(name string, value any) bool
- func (c *Config) Props(section string) []string
- func (c *Config) Reload() (map[string]bool, error)
- func (c *Config) Sections() []string
- func (c *Config) Validate(validators []*Validator) []error
- type DurationMod
- type PropertyValidator
- type Validator
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrNilConfig = errors.New("Config is nil") ErrFileNotSet = errors.New("Path to config file is empty (non initialized struct?)") )
var ( Millisecond = DurationMod(time.Millisecond) Second = 1000 * Millisecond Minute = 60 * Second Hour = 60 * Minute Day = 24 * Hour )
Functions ¶
func GetD ¶
GetD returns configuration values as duration
Example ¶
err := Global("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Duration value from config (as seconds): %v\n", GetD("section:duration", Second)) fmt.Printf("Duration value from config (as minutes): %v\n", GetD("section:duration", Minute))
Output:
func GetF ¶
GetF returns configuration value as floating number
Example ¶
err := Global("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Floating number value from config: %g\n", GetF("section:float"))
Output:
func GetI ¶
GetI returns configuration value as int
Example ¶
err := Global("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Integer (int) value from config: %d\n", GetI("section:int"))
Output:
func GetI64 ¶
GetI64 returns configuration value as int64
Example ¶
err := Global("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Integer (int64) value from config: %d\n", GetI64("section:int64"))
Output:
func GetM ¶
GetM returns configuration value as file mode
Example ¶
err := Global("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("File mode value from config: %v\n", GetM("section:file-mode"))
Output:
func GetS ¶
GetS returns configuration value as string
Example ¶
err := Global("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("String value from config: %s\n", GetS("section:string"))
Output:
func GetU ¶
GetU returns configuration value as uint
Example ¶
err := Global("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Integer (uint) value from config: %d\n", GetU("section:uint"))
Output:
func GetU64 ¶
GetU64 returns configuration value as uint64
Example ¶
err := Global("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Integer (uint64) value from config: %d\n", GetU64("section:uint64"))
Output:
func Global ¶
Global reads and parses configuration file Global instance is accessible 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") // Read duration as seconds GetD("section:duration", Second) // Read duration as minutes GetD("section:duration", Minute) // 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 ¶
HasProp checks if the property is defined and set
Example ¶
err := Global("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Is property \"section:string\" exist: %t\n", HasProp("section:string"))
Output:
func HasSection ¶
HasSection checks if the section exists
Example ¶
err := Global("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Is section \"main\" exist: %t\n", HasSection("main"))
Output:
func Is ¶ added in v12.47.0
Is checks if given property contains given value
Example ¶
err := Global("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("String value from config equals \"test\": %t\n", Is("section:string", "test")) fmt.Printf("Integer value from config equals \"123\": %t\n", Is("section:int", 123)) fmt.Printf("Floating number value from config equals \"12.34\": %t\n", Is("section:float", 12.34))
Output:
func Props ¶
Props returns slice with properties names in some section
Example ¶
err := Global("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } for index, prop := range Props("section") { fmt.Printf("%d: %s\n", index, prop) }
Output:
func Reload ¶
Reload reloads global configuration 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:
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config is basic config struct
func Read ¶
Read reads and parses configuration file
Example ¶
cfg, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Value from config: %s\n", cfg.GetS("section:string"))
Output:
func (*Config) GetD ¶
GetD returns configuration value as duration
Example ¶
cfg, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Duration value from config (as seconds): %v\n", cfg.GetD("section:duration", Second)) fmt.Printf("Duration value from config (as minutes): %v\n", cfg.GetD("section:duration", Minute))
Output:
func (*Config) GetF ¶
GetF returns configuration value as floating number
Example ¶
cfg, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Floating number value from config: %g\n", cfg.GetF("section:float"))
Output:
func (*Config) GetI ¶
GetI returns configuration value as int
Example ¶
cfg, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Integer (int) value from config: %d\n", cfg.GetI("section:int"))
Output:
func (*Config) GetI64 ¶
GetI64 returns configuration value as int64
Example ¶
cfg, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Integer (int64) value from config: %d\n", cfg.GetI64("section:int64"))
Output:
func (*Config) GetM ¶
GetM returns configuration value as file mode
Example ¶
cfg, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("File mode value from config: %v\n", cfg.GetM("section:file-mode"))
Output:
func (*Config) GetS ¶
GetS returns configuration value as string
Example ¶
cfg, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("String value from config: %s\n", cfg.GetS("section:string"))
Output:
func (*Config) GetU ¶
GetU returns configuration value as uint
Example ¶
cfg, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Integer (uint) value from config: %d\n", cfg.GetU("section:uint"))
Output:
func (*Config) GetU64 ¶
GetU64 returns configuration value as uint64
Example ¶
cfg, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Integer (uint64) value from config: %d\n", cfg.GetU64("section:uint64"))
Output:
func (*Config) HasProp ¶
HasProp checks if property is defined and set
Example ¶
cfg, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Is property \"section:string\" exist: %t\n", cfg.HasProp("section:string"))
Output:
func (*Config) HasSection ¶
HasSection checks if section exists
Example ¶
cfg, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Is section \"main\" exist: %t\n", cfg.HasSection("main"))
Output:
func (*Config) Is ¶ added in v12.47.0
Is checks if given property contains given value
Example ¶
cfg, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("String value from config equals \"test\": %t\n", cfg.Is("section:string", "test")) fmt.Printf("Integer value from config equals \"123\": %t\n", cfg.Is("section:int", 123)) fmt.Printf("Floating number value from config equals \"12.34\": %t\n", cfg.Is("section:float", 12.34))
Output:
func (*Config) Props ¶
Props returns slice with properties names in some section
Example ¶
cfg, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } for index, prop := range cfg.Props("section") { fmt.Printf("%d: %s\n", index, prop) }
Output:
func (*Config) Reload ¶
Reload reloads configuration file
Example ¶
config, err := Read("/path/to/your/config.knf") if err != nil { fmt.Printf("Error: %v\n", err) return } changes, err := config.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:
type DurationMod ¶ added in v12.76.1
type DurationMod int64
DurationMod is type for duration modificator
type PropertyValidator ¶
PropertyValidator is default type of property validation function
type Validator ¶
type Validator struct { Property string // Property name Func PropertyValidator // Validation function Value any // 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) |