config: golangci-lint cleanups.
This commit is contained in:
@@ -64,11 +64,7 @@ func LoadFile(path string) error {
|
|||||||
addLine(line)
|
addLine(line)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = scanner.Err(); err != nil {
|
return scanner.Err()
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadFileFor scans the ini file at path, loading the default section
|
// LoadFileFor scans the ini file at path, loading the default section
|
||||||
|
|||||||
@@ -42,69 +42,96 @@ func ParseReader(r io.Reader) (cfg ConfigMap, err error) {
|
|||||||
line string
|
line string
|
||||||
longLine bool
|
longLine bool
|
||||||
currentSection string
|
currentSection string
|
||||||
lineBytes []byte
|
|
||||||
isPrefix bool
|
|
||||||
)
|
)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
err = nil
|
line, longLine, err = readConfigLine(buf, line, longLine)
|
||||||
lineBytes, isPrefix, err = buf.ReadLine()
|
if err == io.EOF {
|
||||||
if io.EOF == err {
|
|
||||||
err = nil
|
err = nil
|
||||||
break
|
break
|
||||||
} else if err != nil {
|
} else if err != nil {
|
||||||
break
|
break
|
||||||
} else if isPrefix {
|
|
||||||
line += string(lineBytes)
|
|
||||||
|
|
||||||
longLine = true
|
|
||||||
continue
|
|
||||||
} else if longLine {
|
|
||||||
line += string(lineBytes)
|
|
||||||
longLine = false
|
|
||||||
} else {
|
|
||||||
line = string(lineBytes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if commentLine.MatchString(line) {
|
if line == "" {
|
||||||
continue
|
continue
|
||||||
} else if blankLine.MatchString(line) {
|
}
|
||||||
continue
|
|
||||||
} else if configSection.MatchString(line) {
|
currentSection, err = processConfigLine(cfg, line, currentSection)
|
||||||
section := configSection.ReplaceAllString(line,
|
if err != nil {
|
||||||
"$1")
|
|
||||||
if section == "" {
|
|
||||||
err = fmt.Errorf("invalid structure in file")
|
|
||||||
break
|
|
||||||
} else if !cfg.SectionInConfig(section) {
|
|
||||||
cfg[section] = make(map[string]string, 0)
|
|
||||||
}
|
|
||||||
currentSection = section
|
|
||||||
} else if configLine.MatchString(line) {
|
|
||||||
regex := configLine
|
|
||||||
if quotedConfigLine.MatchString(line) {
|
|
||||||
regex = quotedConfigLine
|
|
||||||
}
|
|
||||||
if currentSection == "" {
|
|
||||||
currentSection = DefaultSection
|
|
||||||
if !cfg.SectionInConfig(currentSection) {
|
|
||||||
cfg[currentSection] = map[string]string{}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
key := regex.ReplaceAllString(line, "$1")
|
|
||||||
val := regex.ReplaceAllString(line, "$2")
|
|
||||||
if key == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
cfg[currentSection][key] = val
|
|
||||||
} else {
|
|
||||||
err = fmt.Errorf("invalid config file")
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
lineBytes, isPrefix, err := buf.ReadLine()
|
||||||
|
if err != nil {
|
||||||
|
return "", false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if isPrefix {
|
||||||
|
return currentLine + string(lineBytes), true, nil
|
||||||
|
} else if longLine {
|
||||||
|
return currentLine + string(lineBytes), false, nil
|
||||||
|
}
|
||||||
|
return string(lineBytes), false, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// processConfigLine processes a single line and updates the configuration map.
|
||||||
|
func processConfigLine(cfg ConfigMap, line string, currentSection string) (string, error) {
|
||||||
|
if commentLine.MatchString(line) || blankLine.MatchString(line) {
|
||||||
|
return currentSection, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if configSection.MatchString(line) {
|
||||||
|
return handleSectionLine(cfg, line)
|
||||||
|
}
|
||||||
|
|
||||||
|
if configLine.MatchString(line) {
|
||||||
|
return handleConfigLine(cfg, line, currentSection)
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentSection, fmt.Errorf("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")
|
||||||
|
}
|
||||||
|
if !cfg.SectionInConfig(section) {
|
||||||
|
cfg[section] = make(map[string]string, 0)
|
||||||
|
}
|
||||||
|
return section, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// handleConfigLine processes a key=value configuration line.
|
||||||
|
func handleConfigLine(cfg ConfigMap, line string, currentSection string) (string, error) {
|
||||||
|
regex := configLine
|
||||||
|
if quotedConfigLine.MatchString(line) {
|
||||||
|
regex = quotedConfigLine
|
||||||
|
}
|
||||||
|
|
||||||
|
if currentSection == "" {
|
||||||
|
currentSection = DefaultSection
|
||||||
|
if !cfg.SectionInConfig(currentSection) {
|
||||||
|
cfg[currentSection] = map[string]string{}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
key := regex.ReplaceAllString(line, "$1")
|
||||||
|
val := regex.ReplaceAllString(line, "$2")
|
||||||
|
if key != "" {
|
||||||
|
cfg[currentSection][key] = val
|
||||||
|
}
|
||||||
|
|
||||||
|
return currentSection, nil
|
||||||
|
}
|
||||||
|
|
||||||
// SectionInConfig determines whether a section is in the configuration.
|
// SectionInConfig determines whether a section is in the configuration.
|
||||||
func (c ConfigMap) SectionInConfig(section string) bool {
|
func (c ConfigMap) SectionInConfig(section string) bool {
|
||||||
_, ok := c[section]
|
_, ok := c[section]
|
||||||
|
|||||||
Reference in New Issue
Block a user