config: apply linting feedback.

This commit is contained in:
2025-11-15 15:47:29 -08:00
parent aba5e519a4
commit 571443c282
6 changed files with 99 additions and 97 deletions

View File

@@ -2,6 +2,7 @@ package iniconf
import (
"bufio"
"errors"
"fmt"
"io"
"os"
@@ -23,30 +24,31 @@ var (
var DefaultSection = "default"
// ParseFile attempts to load the named config file.
func ParseFile(fileName string) (cfg ConfigMap, err error) {
var file *os.File
file, err = os.Open(fileName)
func ParseFile(fileName string) (ConfigMap, error) {
file, err := os.Open(fileName)
if err != nil {
return
return nil, err
}
defer file.Close()
return ParseReader(file)
}
// ParseReader reads a configuration from an io.Reader.
func ParseReader(r io.Reader) (cfg ConfigMap, err error) {
cfg = ConfigMap{}
func ParseReader(r io.Reader) (ConfigMap, error) {
cfg := ConfigMap{}
buf := bufio.NewReader(r)
var (
line string
longLine bool
currentSection string
err error
)
for {
line, longLine, err = readConfigLine(buf, line, longLine)
if err == io.EOF {
if errors.Is(err, io.EOF) {
err = nil
break
} else if err != nil {
@@ -62,11 +64,12 @@ func ParseReader(r io.Reader) (cfg ConfigMap, err error) {
break
}
}
return
return cfg, err
}
// readConfigLine reads and assembles a complete configuration line, handling long lines.
func readConfigLine(buf *bufio.Reader, currentLine string, longLine bool) (line string, stillLong bool, err error) {
func readConfigLine(buf *bufio.Reader, currentLine string, longLine bool) (string, bool, error) {
lineBytes, isPrefix, err := buf.ReadLine()
if err != nil {
return "", false, err
@@ -94,14 +97,14 @@ func processConfigLine(cfg ConfigMap, line string, currentSection string) (strin
return handleConfigLine(cfg, line, currentSection)
}
return currentSection, fmt.Errorf("invalid config file")
return currentSection, errors.New("invalid config file")
}
// handleSectionLine processes a section header line.
func handleSectionLine(cfg ConfigMap, line string) (string, error) {
section := configSection.ReplaceAllString(line, "$1")
if section == "" {
return "", fmt.Errorf("invalid structure in file")
return "", errors.New("invalid structure in file")
}
if !cfg.SectionInConfig(section) {
cfg[section] = make(map[string]string, 0)
@@ -139,41 +142,39 @@ func (c ConfigMap) SectionInConfig(section string) bool {
}
// ListSections returns the list of sections in the config map.
func (c ConfigMap) ListSections() (sections []string) {
func (c ConfigMap) ListSections() []string {
sections := make([]string, 0, len(c))
for section := range c {
sections = append(sections, section)
}
return
return sections
}
// WriteFile writes out the configuration to a file.
func (c ConfigMap) WriteFile(filename string) (err error) {
func (c ConfigMap) WriteFile(filename string) error {
file, err := os.Create(filename)
if err != nil {
return
return err
}
defer file.Close()
for _, section := range c.ListSections() {
sName := fmt.Sprintf("[ %s ]\n", section)
_, err = file.Write([]byte(sName))
if err != nil {
return
if _, err = file.WriteString(sName); err != nil {
return err
}
for k, v := range c[section] {
line := fmt.Sprintf("%s = %s\n", k, v)
_, err = file.Write([]byte(line))
if err != nil {
return
if _, err = file.WriteString(line); err != nil {
return err
}
}
_, err = file.Write([]byte{0x0a})
if err != nil {
return
if _, err = file.Write([]byte{0x0a}); err != nil {
return err
}
}
return
return nil
}
// AddSection creates a new section in the config map.
@@ -197,27 +198,26 @@ func (c ConfigMap) AddKeyVal(section, key, val string) {
}
// GetValue retrieves the value from a key map.
func (c ConfigMap) GetValue(section, key string) (val string, present bool) {
func (c ConfigMap) GetValue(section, key string) (string, bool) {
if c == nil {
return
return "", false
}
if section == "" {
section = DefaultSection
}
_, ok := c[section]
if !ok {
return
if _, ok := c[section]; !ok {
return "", false
}
val, present = c[section][key]
return
val, present := c[section][key]
return val, present
}
// GetValueDefault retrieves the value from a key map if present,
// otherwise the default value.
func (c ConfigMap) GetValueDefault(section, key, value string) (val string) {
func (c ConfigMap) GetValueDefault(section, key, value string) string {
kval, ok := c.GetValue(section, key)
if !ok {
return value
@@ -226,7 +226,7 @@ func (c ConfigMap) GetValueDefault(section, key, value string) (val string) {
}
// SectionKeys returns the sections in the config map.
func (c ConfigMap) SectionKeys(section string) (keys []string, present bool) {
func (c ConfigMap) SectionKeys(section string) ([]string, bool) {
if c == nil {
return nil, false
}
@@ -235,13 +235,12 @@ func (c ConfigMap) SectionKeys(section string) (keys []string, present bool) {
section = DefaultSection
}
cm := c
s, ok := cm[section]
s, ok := c[section]
if !ok {
return nil, false
}
keys = make([]string, 0, len(s))
keys := make([]string, 0, len(s))
for key := range s {
keys = append(keys, key)
}