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