Skip to content

Commit 74ec70d

Browse files
committed
Initial working solution
1 parent 7b20cf0 commit 74ec70d

File tree

17 files changed

+405
-80
lines changed

17 files changed

+405
-80
lines changed

internal/callgraph/cgexec/command.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,13 @@ func RunCommand(cmd ICommand, ctx IContext) error {
109109
go func() {
110110
err := cmd.Wait()
111111
if err != nil {
112-
err = fmt.Errorf("Command '%s' executed in folder '%s' gave the following error: \n%s\n%s", args, cmd.GetDir(), cmd.GetStdOut().String(), cmd.GetStdErr().String())
112+
err = fmt.Errorf(
113+
"Command '%s' executed in folder '%s' gave the following error: \n%s\n%s",
114+
args,
115+
cmd.GetDir(),
116+
cmd.GetStdOut().String(),
117+
cmd.GetStdErr().String(),
118+
)
113119
}
114120

115121
done <- err

internal/callgraph/language/java/callgraph.go

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package java
22

33
import (
4+
"regexp"
5+
46
"github.com/debricked/cli/internal/callgraph/cgexec"
57
ioFs "github.com/debricked/cli/internal/io"
68
)
@@ -13,11 +15,13 @@ type ICallgraph interface {
1315
type Callgraph struct {
1416
cmdFactory ICmdFactory
1517
filesystem ioFs.IFileSystem
18+
archive ioFs.IArchive
1619
workingDirectory string
1720
targetClasses []string
1821
targetDir string
1922
outputName string
2023
ctx cgexec.IContext
24+
sootHandler ISootHandler
2125
}
2226

2327
func NewCallgraph(
@@ -27,7 +31,9 @@ func NewCallgraph(
2731
targetDir string,
2832
outputName string,
2933
filesystem ioFs.IFileSystem,
34+
archive ioFs.IArchive,
3035
ctx cgexec.IContext,
36+
sootHandler ISootHandler,
3137
) Callgraph {
3238
return Callgraph{
3339
cmdFactory: cmdFactory,
@@ -36,30 +42,55 @@ func NewCallgraph(
3642
targetDir: targetDir,
3743
outputName: outputName,
3844
filesystem: filesystem,
45+
archive: archive,
3946
ctx: ctx,
47+
sootHandler: sootHandler,
4048
}
4149
}
4250

4351
func (cg *Callgraph) RunCallGraphWithSetup() error {
44-
tempDir, err := cg.filesystem.MkdirTemp("jar")
52+
version, err := cg.javaVersion(".")
4553
if err != nil {
4654
return err
4755
}
48-
defer cg.filesystem.RemoveAll(tempDir)
4956

50-
tempJarFile, err := initializeSootWrapper(cg.filesystem, tempDir)
57+
jarFile, err := cg.sootHandler.GetSootWrapper(version, cg.filesystem, cg.archive)
5158
if err != nil {
5259

5360
return err
5461
}
5562

56-
err = cg.RunCallGraph(tempJarFile)
63+
err = cg.RunCallGraph(jarFile)
5764

5865
return err
5966
}
6067

68+
func (cg *Callgraph) javaVersion(path string) (string, error) {
69+
osCmd, err := cg.cmdFactory.MakeJavaVersionCmd(path, cg.ctx)
70+
if err != nil {
71+
72+
return "", err
73+
}
74+
75+
cmd := cgexec.NewCommand(osCmd)
76+
err = cgexec.RunCommand(*cmd, cg.ctx)
77+
javaVersionRegex := regexp.MustCompile(`\b(\d+)\.\d+\.\d+\b`)
78+
match := javaVersionRegex.FindStringSubmatch(cmd.GetStdOut().String())
79+
if len(match) > 1 {
80+
return match[1], nil
81+
}
82+
return "", err
83+
}
84+
6185
func (cg *Callgraph) RunCallGraph(callgraphJarPath string) error {
62-
osCmd, err := cg.cmdFactory.MakeCallGraphGenerationCmd(callgraphJarPath, cg.workingDirectory, cg.targetClasses, cg.targetDir, cg.outputName, cg.ctx)
86+
osCmd, err := cg.cmdFactory.MakeCallGraphGenerationCmd(
87+
callgraphJarPath,
88+
cg.workingDirectory,
89+
cg.targetClasses,
90+
cg.targetDir,
91+
cg.outputName,
92+
cg.ctx,
93+
)
6394
if err != nil {
6495

6596
return err

internal/callgraph/language/java/callgraph_test.go

Lines changed: 9 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -13,57 +13,21 @@ func TestRunCallGraphWithSetupMock(t *testing.T) {
1313

1414
cmdMock := testdata.NewEchoCmdFactory()
1515
fsMock := ioTestData.FileSystemMock{}
16-
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, nil)
16+
arcMock := ioTestData.ArchiveMock{}
17+
swMock := testdata.MockSootHandler{}
18+
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, swMock)
1719

1820
err := cg.RunCallGraphWithSetup()
1921

2022
assert.Nil(t, err)
2123
}
2224

23-
func TestFsOpenEmbedError(t *testing.T) {
24-
cmdMock := testdata.NewEchoCmdFactory()
25-
fsMock := ioTestData.FileSystemMock{FsOpenEmbedError: fmt.Errorf("error")}
26-
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, nil)
27-
28-
err := cg.RunCallGraphWithSetup()
29-
30-
assert.NotNil(t, err)
31-
}
32-
33-
func TestMkdirTempError(t *testing.T) {
34-
cmdMock := testdata.NewEchoCmdFactory()
35-
fsMock := ioTestData.FileSystemMock{MkdirTempError: fmt.Errorf("error")}
36-
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, nil)
37-
38-
err := cg.RunCallGraphWithSetup()
39-
40-
assert.NotNil(t, err)
41-
}
42-
43-
func TestReadAllError(t *testing.T) {
44-
cmdMock := testdata.NewEchoCmdFactory()
45-
fsMock := ioTestData.FileSystemMock{FsReadAllError: fmt.Errorf("error")}
46-
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, nil)
47-
48-
err := cg.RunCallGraphWithSetup()
49-
50-
assert.NotNil(t, err)
51-
}
52-
53-
func TestWriteFileError(t *testing.T) {
54-
cmdMock := testdata.NewEchoCmdFactory()
55-
fsMock := ioTestData.FileSystemMock{FsWriteFileError: fmt.Errorf("error")}
56-
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, nil)
57-
58-
err := cg.RunCallGraphWithSetup()
59-
60-
assert.NotNil(t, err)
61-
}
62-
6325
func TestRunCallGraphMock(t *testing.T) {
6426
cmdMock := testdata.NewEchoCmdFactory()
6527
fsMock := ioTestData.FileSystemMock{}
66-
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, nil)
28+
arcMock := ioTestData.ArchiveMock{}
29+
swMock := testdata.MockSootHandler{}
30+
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, swMock)
6731

6832
err := cg.RunCallGraph(".")
6933

@@ -74,7 +38,9 @@ func TestRunCallGraphErrorMock(t *testing.T) {
7438
cmdMock := testdata.NewEchoCmdFactory()
7539
cmdMock.CallGraphGenErr = fmt.Errorf("error")
7640
fsMock := ioTestData.FileSystemMock{}
77-
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, nil)
41+
arcMock := ioTestData.ArchiveMock{}
42+
swMock := testdata.MockSootHandler{}
43+
cg := NewCallgraph(cmdMock, ".", []string{"."}, ".", ".", fsMock, arcMock, nil, swMock)
7844

7945
err := cg.RunCallGraph(".")
8046

internal/callgraph/language/java/cmd_factory.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type ICmdFactory interface {
1010
MakeMvnCopyDependenciesCmd(workingDirectory string, targetDir string, ctx cgexec.IContext) (*exec.Cmd, error)
1111
MakeCallGraphGenerationCmd(callgraphJarPath string, workingDirectory string, targetClasses []string, dependencyClasses string, outputName string, ctx cgexec.IContext) (*exec.Cmd, error)
1212
MakeBuildMavenCmd(workingDirectory string, ctx cgexec.IContext) (*exec.Cmd, error)
13+
MakeJavaVersionCmd(workingDirectory string, ctx cgexec.IContext) (*exec.Cmd, error)
1314
}
1415

1516
type CmdFactory struct{}
@@ -71,3 +72,13 @@ func (_ CmdFactory) MakeBuildMavenCmd(workingDirectory string, ctx cgexec.IConte
7172

7273
return cgexec.MakeCommand(workingDirectory, path, args, ctx), err
7374
}
75+
76+
func (_ CmdFactory) MakeJavaVersionCmd(workingDirectory string, ctx cgexec.IContext) (*exec.Cmd, error) {
77+
path, err := exec.LookPath("java")
78+
args := []string{
79+
"java",
80+
"--version",
81+
}
82+
83+
return cgexec.MakeCommand(workingDirectory, path, args, ctx), err
84+
}

internal/callgraph/language/java/job.go

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,33 @@ const (
2222

2323
type Job struct {
2424
job.BaseJob
25-
cmdFactory ICmdFactory
26-
config conf.IConfig
27-
archive io.IArchive
28-
ctx cgexec.IContext
29-
fs ioFs.IFileSystem
25+
cmdFactory ICmdFactory
26+
config conf.IConfig
27+
archive io.IArchive
28+
ctx cgexec.IContext
29+
fs ioFs.IFileSystem
30+
sootHandler ISootHandler
3031
}
3132

32-
func NewJob(dir string, files []string, cmdFactory ICmdFactory, writer ioFs.IFileWriter, archive io.IArchive, config conf.IConfig, ctx cgexec.IContext, fs ioFs.IFileSystem) *Job {
33+
func NewJob(
34+
dir string,
35+
files []string,
36+
cmdFactory ICmdFactory,
37+
writer ioFs.IFileWriter,
38+
archive io.IArchive,
39+
config conf.IConfig,
40+
ctx cgexec.IContext,
41+
fs ioFs.IFileSystem,
42+
sootHandler ISootHandler,
43+
) *Job {
3344
return &Job{
34-
BaseJob: job.NewBaseJob(dir, files),
35-
cmdFactory: cmdFactory,
36-
config: config,
37-
archive: archive,
38-
ctx: ctx,
39-
fs: fs,
45+
BaseJob: job.NewBaseJob(dir, files),
46+
cmdFactory: cmdFactory,
47+
config: config,
48+
archive: archive,
49+
ctx: ctx,
50+
fs: fs,
51+
sootHandler: sootHandler,
4052
}
4153
}
4254

@@ -78,7 +90,9 @@ func (j *Job) Run() {
7890
targetDir,
7991
outputName,
8092
j.fs,
93+
j.archive,
8194
j.ctx,
95+
j.sootHandler,
8296
)
8397
j.SendStatus("generating call graph")
8498
j.runCallGraph(&callgraph)

0 commit comments

Comments
 (0)