forked from anchore/syft
/
parse_pom_properties.go
49 lines (37 loc) · 1.08 KB
/
parse_pom_properties.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package java
import (
"bufio"
"fmt"
"io"
"strings"
"github.com/mitchellh/mapstructure"
"github.com/kastenhq/syft/syft/pkg"
)
const pomPropertiesGlob = "*pom.properties"
func parsePomProperties(path string, reader io.Reader) (*pkg.PomProperties, error) {
var props pkg.PomProperties
propMap := make(map[string]string)
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
line := scanner.Text()
// ignore empty lines and comments
if strings.TrimSpace(line) == "" || strings.HasPrefix(strings.TrimLeft(line, " "), "#") {
continue
}
idx := strings.IndexAny(line, "=:")
if idx == -1 {
return nil, fmt.Errorf("unable to split pom.properties key-value pairs: %q", line)
}
key := strings.TrimSpace(line[0:idx])
value := strings.TrimSpace(line[idx+1:])
propMap[key] = value
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("unable to read pom.properties: %w", err)
}
if err := mapstructure.Decode(propMap, &props); err != nil {
return nil, fmt.Errorf("unable to parse pom.properties: %w", err)
}
props.Path = path
return &props, nil
}