go-pathman/envpath/parse.go

98 lines
1.8 KiB
Go
Raw Permalink Normal View History

2019-07-22 05:21:47 +00:00
package envpath
import (
"fmt"
"strings"
)
type Warning struct {
LineNumber int
Line string
Message string
}
// Parse will return a list of paths from an export file
2019-07-28 08:32:57 +00:00
func Parse(b []byte, envname string) ([]string, []Warning) {
2019-07-22 05:21:47 +00:00
s := string(b)
s = strings.Replace(s, "\r\n", "\n", -1)
badlines := []Warning{}
newlines := []string{}
entries := make(map[string]bool)
lines := strings.Split(s, "\n")
for i := range lines {
line := strings.TrimPrefix(strings.TrimSpace(lines[i]), "export ")
if "" == line {
continue
}
if "# Generated for envman. Do not edit." == line {
continue
}
if '#' == line[0] {
badlines = append(badlines, Warning{
LineNumber: i,
Line: line,
Message: "comment",
})
continue
}
index := strings.Index(line, "=")
if index < 1 {
badlines = append(badlines, Warning{
LineNumber: i,
Line: line,
Message: "invalid assignment",
})
continue
}
env := line[:index]
if env != envname {
badlines = append(badlines, Warning{
LineNumber: i,
Line: line,
Message: fmt.Sprintf("wrong name (%s != %s)", env, envname),
})
continue
}
val := line[index+1:]
if len(val) < 2 || '"' != val[0] || '"' != val[len(val)-1] {
badlines = append(badlines, Warning{
LineNumber: i,
Line: line,
Message: "value not quoted",
})
continue
}
val = val[1 : len(val)-1]
if strings.Contains(val, `"`) {
badlines = append(badlines, Warning{
LineNumber: i,
Line: line,
Message: "invalid quotes",
})
continue
}
// TODO normalize $HOME
if entries[val] {
badlines = append(badlines, Warning{
LineNumber: i,
Line: line,
Message: "duplicate entry",
})
continue
}
entries[val] = true
newlines = append(newlines, val)
}
return newlines, badlines
}