github.com/noqcks/syft@v0.0.0-20230920222752-a9e2c4e288e5/syft/pkg/cataloger/java/parse_pom_properties.go (about)

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