Skip to content

Commit

Permalink
feat(gradle): add dep location support
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed Feb 8, 2024
1 parent 4f19ab4 commit a2742e0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
17 changes: 15 additions & 2 deletions pkg/gradle/lockfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package lockfile

import (
"bufio"
"fmt"
"strings"

dio "github.com/aquasecurity/go-dep-parser/pkg/io"
Expand All @@ -18,7 +19,9 @@ func NewParser() types.Parser {
func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, error) {
var libs []types.Library
scanner := bufio.NewScanner(r)
var lineNum int
for scanner.Scan() {
lineNum++
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "#") { // skip comments
continue
Expand All @@ -29,9 +32,19 @@ func (Parser) Parse(r dio.ReadSeekerAt) ([]types.Library, []types.Dependency, er
if len(dep) != 3 { // skip the last line with lists of empty configurations
continue
}

name := strings.Join(dep[:2], ":")
version := strings.Split(dep[2], "=")[0] // remove classPaths
libs = append(libs, types.Library{
Name: strings.Join(dep[:2], ":"),
Version: strings.Split(dep[2], "=")[0], // remove classPaths
ID: fmt.Sprintf("%s:%s", name, version),
Name: name,
Version: version,
Locations: []types.Location{
{
StartLine: lineNum,
EndLine: lineNum,
},
},
})

}
Expand Down
21 changes: 21 additions & 0 deletions pkg/gradle/lockfile/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,37 @@ func TestParser_Parse(t *testing.T) {
inputFile: "testdata/happy.lockfile",
want: []types.Library{
{
ID: "cglib:cglib-nodep:2.1.2",
Name: "cglib:cglib-nodep",
Version: "2.1.2",
Locations: []types.Location{
{
StartLine: 4,
EndLine: 4,
},
},
},
{
ID: "org.springframework:spring-asm:3.1.3.RELEASE",
Name: "org.springframework:spring-asm",
Version: "3.1.3.RELEASE",
Locations: []types.Location{
{
StartLine: 5,
EndLine: 5,
},
},
},
{
ID: "org.springframework:spring-beans:5.0.5.RELEASE",
Name: "org.springframework:spring-beans",
Version: "5.0.5.RELEASE",
Locations: []types.Location{
{
StartLine: 6,
EndLine: 6,
},
},
},
},
},
Expand Down

0 comments on commit a2742e0

Please sign in to comment.