mirror of
https://github.com/miniflux/v2.git
synced 2025-08-06 17:41:00 +00:00
refactor(config): minor improvements of the config parser
- Surface the faulty line number when trying to parse it - Use strings.Cut instead of strings.SplitN - Use strings.TrimSuffix instead of an if - Simplify parseStringList and make its code more compact
This commit is contained in:
parent
335dffbb75
commit
7c42e777ec
1 changed files with 14 additions and 19 deletions
|
@ -66,10 +66,12 @@ func (p *parser) parseFileContent(r io.Reader) (lines []string) {
|
||||||
func (p *parser) parseLines(lines []string) (err error) {
|
func (p *parser) parseLines(lines []string) (err error) {
|
||||||
var port string
|
var port string
|
||||||
|
|
||||||
for _, line := range lines {
|
for lineNum, line := range lines {
|
||||||
fields := strings.SplitN(line, "=", 2)
|
key, value, ok := strings.Cut(line, "=")
|
||||||
key := strings.TrimSpace(fields[0])
|
if !ok {
|
||||||
value := strings.TrimSpace(fields[1])
|
return fmt.Errorf("config: unable to parse configuration, invalid format on line %d", lineNum)
|
||||||
|
}
|
||||||
|
key, value = strings.TrimSpace(key), strings.TrimSpace(value)
|
||||||
|
|
||||||
switch key {
|
switch key {
|
||||||
case "LOG_FILE":
|
case "LOG_FILE":
|
||||||
|
@ -275,9 +277,7 @@ func parseBaseURL(value string) (string, string, string, error) {
|
||||||
return defaultBaseURL, defaultRootURL, "", nil
|
return defaultBaseURL, defaultRootURL, "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if value[len(value)-1:] == "/" {
|
value = strings.TrimSuffix(value, "/")
|
||||||
value = value[:len(value)-1]
|
|
||||||
}
|
|
||||||
|
|
||||||
parsedURL, err := url.Parse(value)
|
parsedURL, err := url.Parse(value)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -333,21 +333,16 @@ func parseStringList(value string, fallback []string) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
var strList []string
|
var strList []string
|
||||||
strMap := make(map[string]bool)
|
present := make(map[string]bool)
|
||||||
|
|
||||||
items := strings.Split(value, ",")
|
for item := range strings.SplitSeq(value, ",") {
|
||||||
for _, item := range items {
|
if itemValue := strings.TrimSpace(item); itemValue != "" {
|
||||||
itemValue := strings.TrimSpace(item)
|
if !present[itemValue] {
|
||||||
|
present[itemValue] = true
|
||||||
if itemValue == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if _, found := strMap[itemValue]; !found {
|
|
||||||
strMap[itemValue] = true
|
|
||||||
strList = append(strList, itemValue)
|
strList = append(strList, itemValue)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return strList
|
return strList
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue