Documentation
¶
Index ¶
- Variables
- type Config
- func (c *Config) AddSection(sect string) error
- func (c *Config) CopySection(old, newName string, includeGroup bool) error
- func (c *Config) DelVal(sect, parm string) error
- func (c *Config) Delete()
- func (c *Config) DeleteParameterComment(sect, parm string) error
- func (c *Config) DeleteParameterEOT(sect, parm string) error
- func (c *Config) DeleteSection(sect string) error
- func (c *Config) DeleteSectionComment(sect string) error
- func (c *Config) Errors() []ParseError
- func (c *Config) Exists(sect, parm string) bool
- func (c *Config) GetFileName() string
- func (c *Config) GetParameterComment(sect, parm string) ([]string, bool)
- func (c *Config) GetParameterEOT(sect, parm string) string
- func (c *Config) GetParameterTrailingComment(sect, parm string) (string, bool)
- func (c *Config) GetSectionComment(sect string) ([]string, bool)
- func (c *Config) GetWriteMode() os.FileMode
- func (c *Config) GroupMembers(group string) []string
- func (c *Config) Groups() []string
- func (c *Config) IsImported(sect, parm string) bool
- func (c *Config) MyParameters(sect string) []string
- func (c *Config) NewVal(sect, parm string, vals ...string) error
- func (c *Config) Parameters(sect string) []string
- func (c *Config) PushVal(sect, parm string, vals ...string) error
- func (c *Config) Reload() error
- func (c *Config) RemoveGroupMember(sect string)
- func (c *Config) RenameSection(old, newName string, includeGroup bool) error
- func (c *Config) ResortSections(sects []string) error
- func (c *Config) Rewrite() error
- func (c *Config) SectionExists(sect string) bool
- func (c *Config) Sections() []string
- func (c *Config) SetFileName(name string) error
- func (c *Config) SetGroupMember(sect string)
- func (c *Config) SetParameterComment(sect, parm string, comments ...string) (int, error)
- func (c *Config) SetParameterEOT(sect, parm, eot string) error
- func (c *Config) SetParameterTrailingComment(sect, parm, cmt string) error
- func (c *Config) SetSectionComment(sect string, comments ...string) (int, error)
- func (c *Config) SetVal(sect, parm string, vals ...string) error
- func (c *Config) SetWriteMode(mode os.FileMode)
- func (c *Config) Val(sect, parm string) (string, bool)
- func (c *Config) ValWithDefault(sect, parm, def string) string
- func (c *Config) Vals(sect, parm string) ([]string, bool)
- func (c *Config) WriteFile(path string) error
- func (c *Config) WriteFileDelta(path string) error
- func (c *Config) WriteTo(w io.Writer) (int64, error)
- func (c *Config) WriteToDelta(w io.Writer) (int64, error)
- type Option
- func WithAllowContinue(b bool) Option
- func WithAllowEmpty(b bool) Option
- func WithAllowedCommentChars(rs []rune) Option
- func WithCommentChar(r rune) Option
- func WithDefault(name string) Option
- func WithFallback(name string) Option
- func WithImport(c *Config) Option
- func WithNegativeDeltas(b bool) Option
- func WithNoCase(b bool) Option
- func WithNoMultiline(b bool) Option
- func WithPHPCompat(b bool) Option
- func WithReloadWarn(b bool) Option
- func WithReloadWriter(w *os.File) Option
- func WithTrailingComment(b bool) Option
- type Options
- type ParseError
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadyExists = errors.New("inifiles: section already exists")
ErrAlreadyExists is returned when a creation operation targets a section or parameter that already exists.
var ErrEmptyArg = errors.New("inifiles: empty section or parameter name")
ErrEmptyArg is returned when a section, parameter, or other required argument is the empty string.
var ErrNotFound = errors.New("inifiles: section or parameter not found")
ErrNotFound is returned when a section or parameter does not exist.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config is a parsed .ini configuration, ported from Perl's Config::IniFiles. Use New, NewFromFile, NewFromReader, or NewFromBytes to create one.
func New ¶
New creates an empty Config without reading any file. Equivalent to Perl new() with no -file. Options may include WithFileName via... (use SetFileName after) -- callers pass opts for nocase/comment/etc.
func NewFromBytes ¶
NewFromBytes creates a Config from an in-memory byte slice. Convenience over NewFromReader(bytes.NewReader(b)).
func NewFromFile ¶
NewFromFile creates a Config by reading the file at path.
func NewFromReader ¶
NewFromReader creates a Config by reading from r. No file path is recorded, so WriteFile will require SetFileName first. The detected line ending (if any) is reused for subsequent writes. r is consumed entirely.
func (*Config) AddSection ¶
AddSection creates a section if it does not already exist. It is idempotent: re-adding an existing section is a no-op (returns nil). An empty sect returns ErrEmptyArg.
func (*Config) CopySection ¶
CopySection copies old into newName (no overwrite). When includeGroup is true, group members of old are recursively copied to counterparts with the name prefix replaced; existing counterparts are skipped and the rest of the group is still copied. An empty old or newName returns ErrEmptyArg. A missing old section returns ErrNotFound. An existing destination returns ErrAlreadyExists.
func (*Config) DelVal ¶
DelVal deletes sect.parm, recording a tombstone for delta output. Deleting a missing section or parameter is a no-op (returns nil). An empty sect or parm returns ErrEmptyArg.
func (*Config) Delete ¶
func (c *Config) Delete()
Delete removes every section from the configuration.
func (*Config) DeleteParameterComment ¶
DeleteParameterComment removes sect.parm's comment lines. Deleting the comment on a missing parameter is a no-op (returns nil). An empty sect or parm returns ErrEmptyArg.
func (*Config) DeleteParameterEOT ¶
DeleteParameterEOT removes the here-doc terminator marker from the parameter. A multi-valued param reverts to the default "EOT" marker on output. Deleting the marker on a missing parameter is a no-op (returns nil). An empty sect or parm returns ErrEmptyArg.
func (*Config) DeleteSection ¶
DeleteSection removes sect and all its parameters, recording a tombstone for delta output. Deleting a missing section is a no-op (returns nil). An empty sect returns ErrEmptyArg.
func (*Config) DeleteSectionComment ¶
DeleteSectionComment removes the comment lines above sect. Deleting the comment on a missing section is a no-op (returns nil). An empty sect returns ErrEmptyArg.
func (*Config) Errors ¶
func (c *Config) Errors() []ParseError
Errors returns the errors accumulated during the most recent read.
func (*Config) GetFileName ¶
GetFileName returns the configured file path (may be "").
func (*Config) GetParameterComment ¶
GetParameterComment returns sect.parm's comment lines, or nil,false if absent.
func (*Config) GetParameterEOT ¶
GetParameterEOT returns the here-doc terminator marker for the parameter, or "" if none was set. A multi-valued param auto-created without an explicit marker returns "EOT" (the default), mirroring Perl.
func (*Config) GetParameterTrailingComment ¶
GetParameterTrailingComment returns the end-of-line comment for sect.parm, or "" if none. The boolean reports whether the parameter exists.
func (*Config) GetSectionComment ¶
GetSectionComment returns the section's comment lines (verbatim, prefixed as read). Returns nil,false if the section or its comment is absent.
func (*Config) GetWriteMode ¶
GetWriteMode returns the configured file mode (permission bits only), or 0 if none has been set.
func (*Config) GroupMembers ¶
GroupMembers returns the section names belonging to group, or nil if group is unknown.
func (*Config) IsImported ¶
IsImported reports whether sect.parm originates from the imported config (i.e., the section is imported and the parameter has not been locally modified).
func (*Config) MyParameters ¶
MyParameters returns the ordered parameter names of sect that are either in a non-imported section or have been modified (touched).
func (*Config) NewVal ¶
NewVal creates sect.parm with the given values, creating the section if needed. If multiple values are given the parameter is multi-valued (here-doc on output). An empty sect or parm returns ErrEmptyArg.
func (*Config) Parameters ¶
Parameters returns the ordered parameter names of sect (including imported).
func (*Config) PushVal ¶
PushVal appends values to an existing multi-valued (or scalar -> array) parameter. Returns ErrNotFound if the parameter does not exist. An empty sect or parm returns ErrEmptyArg. Passing no values is a no-op.
func (*Config) Reload ¶
Reload re-reads the configured file (if any), discarding in-memory changes. Mirrors Perl's ReadConfig.
func (*Config) RemoveGroupMember ¶
RemoveGroupMember removes sect from its derived group (if any).
func (*Config) RenameSection ¶
RenameSection copies old to new and then deletes old.
func (*Config) ResortSections ¶
ResortSections reorders the given sections into the supplied order, keeping every section not listed at its relative position around the reordered block (mirrors Perl Config::IniFiles::ResortSections). Every name in sects must refer to an existing section; otherwise ResortSections returns ErrNotFound and leaves the order untouched. An empty sects list (or a single-element list) is a no-op.
func (*Config) SectionExists ¶
SectionExists reports whether a section named sect is present.
func (*Config) Sections ¶
Sections returns the ordered list of section names (including imported ones).
func (*Config) SetFileName ¶
SetFileName sets the path used by Reload and Rewrite. An empty name returns ErrEmptyArg.
func (*Config) SetGroupMember ¶
SetGroupMember enrolls sect in the group derived from its name (the first whitespace-delimited word, when the name contains a second word). This is invoked automatically by AddSection.
func (*Config) SetParameterComment ¶
SetParameterComment sets the comment lines appearing above sect.parm, prefixing any line lacking an allowed comment leader. Returns the count of comment lines. An empty sect or parm returns ErrEmptyArg. Passing no comments is a no-op (0, nil).
func (*Config) SetParameterEOT ¶
SetParameterEOT sets the here-doc terminator marker for the parameter. Once set, the parameter serializes as a here-doc block on write. An empty sect, parm, or eot returns ErrEmptyArg. A missing parameter returns ErrNotFound.
func (*Config) SetParameterTrailingComment ¶
SetParameterTrailingComment sets the end-of-line comment for sect.parm. The parameter must exist; a missing parameter returns ErrNotFound. An empty sect or parm returns ErrEmptyArg. An empty cmt is a no-op (returns nil).
func (*Config) SetSectionComment ¶
SetSectionComment sets the comment lines appearing above sect, prefixing any line that lacks an allowed comment leader with "<commentChar> ". The section is created if it does not exist. Returns the count of comment lines. An empty sect returns ErrEmptyArg. Passing no comments is a no-op (0, nil).
func (*Config) SetVal ¶
SetVal sets an existing parameter's value(s). Returns ErrNotFound if the parameter does not already exist (use NewVal to create). An empty sect or parm returns ErrEmptyArg.
func (*Config) SetWriteMode ¶
SetWriteMode sets the file mode applied on WriteFile (only the permission bits are used). Pass 0 to clear the recorded mode.
func (*Config) Val ¶
Val returns the value of sect.parm joined with newlines if multi-valued. The boolean reports whether the parameter (or a default-section fallback) was found.
func (*Config) ValWithDefault ¶
ValWithDefault returns the value of sect.parm, or def when not found (mirrors Perl val's third argument default).
func (*Config) Vals ¶
Vals returns the value of sect.parm as a slice (one element for scalar values, multiple for here-doc/accumulated). The boolean reports whether the parameter (or a default-section fallback) was found. A missing parameter returns nil, false (mirrors Perl list-context val returning an empty list).
func (*Config) WriteFile ¶
WriteFile writes the configuration to path atomically (temp file + rename) and applies the recorded file mode.
func (*Config) WriteFileDelta ¶
WriteFileDelta writes a delta file to path atomically.
type Option ¶
type Option func(*Options)
Option configures Options.
func WithAllowContinue ¶
WithAllowContinue controls whether a trailing backslash acts as a line-continuation marker on read.
func WithAllowEmpty ¶
WithAllowEmpty controls whether an empty config file is accepted or treated as an error.
func WithAllowedCommentChars ¶
WithAllowedCommentChars sets extra runes recognized as comment leaders in addition to CommentChar.
func WithCommentChar ¶
WithCommentChar sets the primary comment character used to prefix synthetic comment lines. Must be a single non-alphanumeric, non-[]:= rune.
func WithDefault ¶
WithDefault sets the section name consulted by Val when a parameter is missing in the requested section.
func WithFallback ¶
WithFallback sets the section into which parameters appearing before any section header are filed.
func WithImport ¶
WithImport sets an existing *Config whose data is deep-copied as a base before reading the local file; local values shadow imported ones.
func WithNegativeDeltas ¶
WithNegativeDeltas controls whether "; [sect] is deleted" / "; parm is deleted" comments trigger deletions during read.
func WithNoCase ¶
WithNoCase controls case-insensitive section and parameter key matching.
func WithNoMultiline ¶
WithNoMultiline controls whether multi-valued parameters are serialized as repeated single lines instead of here-doc blocks.
func WithPHPCompat ¶
WithPHPCompat controls PHP-style "name[]" array-stripping and quote unwrapping on read.
func WithReloadWarn ¶
WithReloadWarn controls whether a reload notice is printed on each Reload after the first.
func WithReloadWriter ¶
WithReloadWriter sets the *os.File that receives reload warning messages. Defaults to os.Stderr.
func WithTrailingComment ¶
WithTrailingComment controls whether "value ; comment" is parsed into a value plus a trailing comment slot.
type Options ¶
type Options struct {
// Import is an existing *Config whose data is deep-copied as a base
// before reading the local file (local values shadow imported ones).
Import *Config
// Default is the section name consulted by Val when a parameter is
// missing in the requested section.
Default string
// Fallback is the section into which parameters appearing before any
// section header are filed. Without it, such parameters are a parse
// error. The fallback section header is suppressed on write when used.
Fallback string
// NoCase lowercases section and parameter keys (values keep original
// case).
NoCase bool
// ReloadWarn prints a reload notice to ReloadWriter (default os.Stderr)
// on each Reload after the first.
ReloadWarn bool
// NoMultiline outputs multi-valued parameters as repeated single lines
// instead of here-doc blocks. Affects output only.
NoMultiline bool
// AllowContinue honors a trailing backslash as a line-continuation
// marker on read.
AllowContinue bool
// AllowEmpty permits empty config files; otherwise they are an error.
AllowEmpty bool
// NegativeDeltas parses "; [sect] is deleted" / "; parm is deleted"
// comments as deletions during read. Auto-enabled when Import is set.
NegativeDeltas bool
// CommentChar is the primary comment character used to prefix synthetic
// comment lines. Must be a single non-alphanumeric, non-[]:= rune.
CommentChar rune
// AllowedCommentChars are extra runes recognized as comment leaders in
// addition to CommentChar. Each must be printable and not alphanumeric
// or one of [ ] : =.
AllowedCommentChars []rune
// HandleTrailingComment parses "value ; comment" into value plus a
// trailing comment slot.
HandleTrailingComment bool
// PHPCompat strips trailing "[]" from parameter names and unwraps
// quotes from values on read.
PHPCompat bool
// ReloadWriter receives the reload warning message when ReloadWarn is
// true. Defaults to os.Stderr.
ReloadWriter *os.File
// contains filtered or unexported fields
}
Options controls parsing and writing behavior, mirroring the Perl Config::IniFiles constructor flags.
type ParseError ¶
ParseError describes an error encountered while reading a config.
func (*ParseError) Error ¶
func (e *ParseError) Error() string
Error returns the error string, including the line number when available.
func (*ParseError) Unwrap ¶
func (e *ParseError) Unwrap() error