Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Maven dependency.txt #31

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 95 additions & 0 deletions pkg/maven/parse.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package maven

import (
"bufio"
"fmt"
"io"
"strings"

"github.com/aquasecurity/go-dep-parser/pkg/types"
"golang.org/x/xerrors"
)

const (
separator = ":"
resolvedSection = "The following files have been resolved:"
)

type Artifact struct {
GroupID string
ArtifactID string
Type string
Classifier string
Version string
Scope string
}

func (a Artifact) getName() string {
return a.GroupID + separator + a.ArtifactID
}

func Parse(r io.Reader) ([]types.Library, error) {
scanner := bufio.NewScanner(r)

var libs []types.Library
var resolvedSectionFlag bool
for scanner.Scan() {

line := scanner.Text()
if line == resolvedSection {
resolvedSectionFlag = true
continue
}
if resolvedSectionFlag && line == "" {
break
}
if !resolvedSectionFlag {
continue
}

line = strings.TrimSpace(line)
if line == "none" {
return []types.Library{}, nil
}

artifact, err := parseArtifact(strings.Fields(line)[0])
if err != nil {
return nil, xerrors.Errorf("failed to parse artifact: %w", err)
}
libs = append(libs, types.Library{
Name: artifact.getName(),
Version: artifact.Version,
})
}
if err := scanner.Err(); err != nil {
return nil, xerrors.Errorf("scan error: %w", err)
}

return libs, nil
}

func parseArtifact(s string) (Artifact, error) {
ss := strings.Split(s, separator)
if len(ss) < 4 {
fmt.Println(s)
masahiro331 marked this conversation as resolved.
Show resolved Hide resolved
return Artifact{}, xerrors.New("invalid format error")
masahiro331 marked this conversation as resolved.
Show resolved Hide resolved
}
artifact := Artifact{
GroupID: ss[0],
ArtifactID: ss[1],
Type: ss[2],
}
switch len(ss) {
case 4:
artifact.Version = ss[3]
case 5:
artifact.Version = ss[3]
artifact.Scope = ss[4]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ss[4] might be a version.

case 6:
artifact.Classifier = ss[3]
artifact.Version = ss[4]
artifact.Scope = ss[5]
}

return artifact, nil
}
48 changes: 48 additions & 0 deletions pkg/maven/parse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package maven

import (
"os"
"path"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/aquasecurity/go-dep-parser/pkg/types"
)

func TestParse(t *testing.T) {
vectors := []struct {
file string // Test input file
want []types.Library
}{
{
file: "testdata/spring.txt",
want: MavenNormal,
},
{
file: "testdata/hadoop-hdfs-nfs.txt",
want: MavenHadoopHDFS,
},
{
file: "testdata/hadoop-cloud-storage.txt",
want: MavenHadoopCloudStorage,
},
{
file: "testdata/none.txt",
want: MavenNone,
},
}

for _, v := range vectors {
t.Run(path.Base(v.file), func(t *testing.T) {
f, err := os.Open(v.file)
require.NoError(t, err)

got, err := Parse(f)
require.NoError(t, err)

assert.Equal(t, v.want, got)
})
}
}
Loading