Skip to content

Commit e48452d

Browse files
KunalSin9habhisek
andauthored
feat: show manifest relative path in summary report (#613)
* feat: show manifest relative path in summary report * fix: typos * fix: using standard color profile * fix: log message --------- Co-authored-by: Abhisek Datta <abhisek.datta@gmail.com>
1 parent 9f55120 commit e48452d

2 files changed

Lines changed: 90 additions & 0 deletions

File tree

pkg/reporter/summary.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@ import (
44
"cmp"
55
"fmt"
66
"os"
7+
"path/filepath"
78
"slices"
89
"strings"
910

1011
"github.com/jedib0t/go-pretty/v6/table"
1112
"github.com/jedib0t/go-pretty/v6/text"
13+
"github.com/safedep/dry/log"
1214
"github.com/safedep/dry/semver"
1315
"github.com/safedep/dry/utils"
1416
"github.com/safedep/vet/gen/insightapi"
@@ -568,6 +570,12 @@ func (r *summaryReporter) addRemediationAdviceTableRows(tbl table.Writer,
568570
return tagText
569571
}
570572

573+
currentWorkingDirectory, err := os.Getwd()
574+
if err != nil {
575+
log.Warnf("Error getting current working directory: %s", err.Error())
576+
currentWorkingDirectory = ""
577+
}
578+
571579
for idx, sp := range sortedPackages {
572580
if idx >= maxAdvice {
573581
break
@@ -637,6 +645,21 @@ func (r *summaryReporter) addRemediationAdviceTableRows(tbl table.Writer,
637645
}
638646
}
639647

648+
manifestPath, err := r.packageManifestRelativePath(currentWorkingDirectory, sp.pkg.Manifest.Path)
649+
if err != nil {
650+
log.Warnf("Error getting manifest relative path: %s", err.Error())
651+
manifestPath = sp.pkg.Manifest.GetDisplayPath()
652+
}
653+
654+
// Add Manifest Path information just below package name
655+
tbl.AppendRow(table.Row{
656+
"", // Ecosystem
657+
InfoText(manifestPath),
658+
"", // Latest Version
659+
"", // Score
660+
"", // Vulnerability Info
661+
})
662+
640663
tbl.AppendSeparator()
641664
}
642665
}
@@ -667,6 +690,15 @@ func (r *summaryReporter) packageVulnerabilityRiskText(pkg *models.Package) stri
667690
return WhiteBgText(" Unknown ")
668691
}
669692

693+
func (r *summaryReporter) packageManifestRelativePath(currentWorkingDirectory, manifestFullPath string) (string, error) {
694+
relPath, err := filepath.Rel(currentWorkingDirectory, manifestFullPath)
695+
if err != nil {
696+
return "", fmt.Errorf("error getting relative path: %w", err)
697+
}
698+
699+
return relPath, nil
700+
}
701+
670702
func (r *summaryReporter) packageVulnerabilitySampleText(pkg *models.Package) string {
671703
if _, ok := r.vulnerabilityInfo[pkg.Id()]; !ok {
672704
return ""

pkg/reporter/summary_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package reporter
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestManifestRelativePath(t *testing.T) {
10+
testCases := []struct {
11+
name string
12+
13+
currentWorkingDir string
14+
manifestFullPath string
15+
16+
expectError bool
17+
expectedRelativePath string
18+
}{
19+
{
20+
name: "valid current working directory",
21+
currentWorkingDir: "/home/user/work/company/project/source",
22+
manifestFullPath: "/home/user/work/company/project/source/package-lock.json",
23+
24+
expectError: false,
25+
expectedRelativePath: "package-lock.json",
26+
},
27+
{
28+
name: "valid current working directory with sub dir manifest path",
29+
currentWorkingDir: "/home/user/work/company/project/source",
30+
manifestFullPath: "/home/user/work/company/project/source/apps/cli/go.mod",
31+
32+
expectError: false,
33+
expectedRelativePath: "apps/cli/go.mod",
34+
},
35+
{
36+
name: "empty current working directory - error on os.Getwd()",
37+
currentWorkingDir: "", // os.Getwd() failed, then currentWorkingDir = ""
38+
manifestFullPath: "/home/user/work/company/project/source/package-lock.json",
39+
expectError: true,
40+
},
41+
}
42+
43+
for _, test := range testCases {
44+
t.Run(test.name, func(t *testing.T) {
45+
t.Parallel()
46+
47+
r := &summaryReporter{}
48+
relativePath, err := r.packageManifestRelativePath(test.currentWorkingDir, test.manifestFullPath)
49+
50+
if test.expectError {
51+
assert.Error(t, err)
52+
} else {
53+
assert.NoError(t, err)
54+
assert.Equal(t, relativePath, test.expectedRelativePath)
55+
}
56+
})
57+
}
58+
}

0 commit comments

Comments
 (0)