Skip to content

Commit

Permalink
move fingerprint to root command
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwareus committed Oct 17, 2023
1 parent 18ef92b commit fa28ac1
Show file tree
Hide file tree
Showing 17 changed files with 64 additions and 34 deletions.
4 changes: 1 addition & 3 deletions internal/cmd/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package files

import (
"github.com/debricked/cli/internal/cmd/files/find"
"github.com/debricked/cli/internal/cmd/files/fingerprint"
"github.com/debricked/cli/internal/file"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

func NewFilesCmd(finder file.IFinder, fingerprinter file.IFingerprint) *cobra.Command {
func NewFilesCmd(finder file.IFinder) *cobra.Command {
cmd := &cobra.Command{
Use: "files",
Short: "Analyze files",
Expand All @@ -19,7 +18,6 @@ func NewFilesCmd(finder file.IFinder, fingerprinter file.IFingerprint) *cobra.Co
}

cmd.AddCommand(find.NewFindCmd(finder))
cmd.AddCommand(fingerprint.NewFingerprintCmd(fingerprinter))

return cmd
}
6 changes: 3 additions & 3 deletions internal/cmd/files/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (

func TestNewFilesCmd(t *testing.T) {
finder, _ := file.NewFinder(nil)
cmd := NewFilesCmd(finder, nil)
cmd := NewFilesCmd(finder)
commands := cmd.Commands()
nbrOfCommands := 2
nbrOfCommands := 1
assert.Lenf(t, commands, nbrOfCommands, "failed to assert that there were %d sub commands connected", nbrOfCommands)
}

func TestPreRun(t *testing.T) {
cmd := NewFilesCmd(nil, nil)
cmd := NewFilesCmd(nil)
cmd.PreRun(cmd, nil)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path/filepath"

"github.com/debricked/cli/internal/file"
"github.com/debricked/cli/internal/fingerprint"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
Expand All @@ -15,10 +16,10 @@ const (
ExclusionFlag = "exclusion-fingerprint"
)

func NewFingerprintCmd(fingerprinter file.IFingerprint) *cobra.Command {
func NewFingerprintCmd(fingerprinter fingerprint.IFingerprint) *cobra.Command {

short := fmt.Sprintf("Fingerprint files for identification in a given path and writes it to %s. [beta feature]", file.OutputFileNameFingerprints)
long := fmt.Sprintf("Fingerprint files for identification in a given path and writes it to %s. [beta feature]\nThis hashes all files and matches them against the Debricked knowledge base.", file.OutputFileNameFingerprints)
short := "Fingerprints files to match against the Debricked knowledge base. [beta feature]"
long := fmt.Sprintf("Fingerprint files for identification in a given path and writes it to %s. [beta feature]\nThis hashes all files and matches them against the Debricked knowledge base.", fingerprint.OutputFileNameFingerprints)
cmd := &cobra.Command{
Use: "fingerprint [path]",
Short: short,
Expand Down Expand Up @@ -48,7 +49,7 @@ $ debricked files fingerprint . `+exampleFlags)
return cmd
}

func RunE(f file.IFingerprint) func(_ *cobra.Command, args []string) error {
func RunE(f fingerprint.IFingerprint) func(_ *cobra.Command, args []string) error {
return func(_ *cobra.Command, args []string) error {
path := ""
if len(args) > 0 {
Expand All @@ -61,7 +62,7 @@ func RunE(f file.IFingerprint) func(_ *cobra.Command, args []string) error {
return err
}

err = output.ToFile(file.OutputFileNameFingerprints)
err = output.ToFile(fingerprint.OutputFileNameFingerprints)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"os"
"testing"

"github.com/debricked/cli/internal/file"
"github.com/debricked/cli/internal/file/testdata"
"github.com/debricked/cli/internal/fingerprint"
"github.com/debricked/cli/internal/fingerprint/testdata"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)

func TestNewFingerprintCmd(t *testing.T) {
var f file.IFingerprint
var f fingerprint.IFingerprint
cmd := NewFingerprintCmd(f)

commands := cmd.Commands()
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestNewFingerprintCmd(t *testing.T) {

func TestRunE(t *testing.T) {
defer func() {
os.Remove(file.OutputFileNameFingerprints)
os.Remove(fingerprint.OutputFileNameFingerprints)
}()
fingerprintMock := testdata.NewFingerprintMock()
runE := RunE(fingerprintMock)
Expand Down
4 changes: 3 additions & 1 deletion internal/cmd/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package root

import (
"github.com/debricked/cli/internal/cmd/files"
"github.com/debricked/cli/internal/cmd/fingerprint"
"github.com/debricked/cli/internal/cmd/report"
"github.com/debricked/cli/internal/cmd/resolve"
"github.com/debricked/cli/internal/cmd/scan"
Expand Down Expand Up @@ -40,8 +41,9 @@ Read more: https://portal.debricked.com/administration-47/how-do-i-generate-an-a
debClient.SetAccessToken(&accessToken)

rootCmd.AddCommand(report.NewReportCmd(container.LicenseReporter(), container.VulnerabilityReporter()))
rootCmd.AddCommand(files.NewFilesCmd(container.Finder(), container.Fingerprinter()))
rootCmd.AddCommand(files.NewFilesCmd(container.Finder()))
rootCmd.AddCommand(scan.NewScanCmd(container.Scanner()))
rootCmd.AddCommand(fingerprint.NewFingerprintCmd(container.Fingerprinter()))
rootCmd.AddCommand(resolve.NewResolveCmd(container.Resolver()))

rootCmd.CompletionOptions.DisableDefaultCmd = true
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/root/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestNewRootCmd(t *testing.T) {
cmd := NewRootCmd("v0.0.0", wire.GetCliContainer())
commands := cmd.Commands()
nbrOfCommands := 4
nbrOfCommands := 5
if len(commands) != nbrOfCommands {
t.Errorf("failed to assert that there were %d sub commands connected", nbrOfCommands)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/file/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (finder *Finder) GetGroups(rootPath string, exclusions []string, lockfileOn
if err != nil {
return err
}
if !fileInfo.IsDir() && !excluded(exclusions, path) {
if !fileInfo.IsDir() && !Excluded(exclusions, path) {
for _, format := range formats {
if groups.Match(format, path, lockfileOnly) {

Expand Down Expand Up @@ -97,7 +97,7 @@ func (finder *Finder) GetSupportedFormats() ([]*CompiledFormat, error) {
return compiledDependencyFileFormats, nil
}

func excluded(exclusions []string, path string) bool {
func Excluded(exclusions []string, path string) bool {
for _, exclusion := range exclusions {
ex := filepath.Clean(exclusion)
matched, _ := doublestar.PathMatch(ex, path)
Expand Down
2 changes: 1 addition & 1 deletion internal/file/finder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func TestExclude(t *testing.T) {
t.Run(c.name, func(t *testing.T) {
var excludedFiles []string
for _, file := range files {
if excluded(c.exclusions, file) {
if Excluded(c.exclusions, file) {
excludedFiles = append(excludedFiles, file)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package file
package fingerprint

import (
"bufio"
Expand All @@ -9,6 +9,7 @@ import (
"path/filepath"
"strings"

"github.com/debricked/cli/internal/file"
"github.com/debricked/cli/internal/tui"
)

Expand Down Expand Up @@ -174,7 +175,7 @@ func shouldProcessFile(fileInfo os.FileInfo, exclusions []string, path string) b
return false
}

if excluded(exclusions, path) {
if file.Excluded(exclusions, path) {
return false
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package file
package fingerprint

import (
"fmt"
Expand Down Expand Up @@ -71,9 +71,24 @@ func TestShouldProcessFile(t *testing.T) {
if err != nil {
t.Fatalf("Failed to get file info for %s: %v", testLink, err)
}

isSymlink, err := isSymlink(testLink)
if err != nil {
t.Fatalf("Failed to check if %s is a symbolic link: %v", testLink, err)
}
if !isSymlink {
t.Fatalf("Expected %s to be a symbolic link", testLink)
}

if shouldProcessFile(linkInfo, []string{}, testLink) {
t.Errorf("Expected shouldProcessFile to return false for %s, but it returned true", testLink)
}

// Test Excluded
if shouldProcessFile(fileInfo, []string{"**/test.py"}, testFile) {
t.Errorf("Expected shouldProcessFile to return true for %s, but it returned false", testFile)
}

}

func TestNewFingerprinter(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package testdata

import (
"github.com/debricked/cli/internal/file"
"github.com/debricked/cli/internal/fingerprint"
)

type FingerprintMock struct {
Expand All @@ -14,6 +14,6 @@ func NewFingerprintMock() *FingerprintMock {
}
}

func (f *FingerprintMock) FingerprintFiles(rootPath string, exclusions []string) (file.Fingerprints, error) {
return file.Fingerprints{}, f.error
func (f *FingerprintMock) FingerprintFiles(rootPath string, exclusions []string) (fingerprint.Fingerprints, error) {
return fingerprint.Fingerprints{}, f.error
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net6.0</TargetFrameworks>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Test.Package.1" Version="1.0.0" />
<PackageReference Include="Test.Package.2" Version="2.0.0" />
</ItemGroup>
</Project>
7 changes: 4 additions & 3 deletions internal/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/debricked/cli/internal/ci/env"
"github.com/debricked/cli/internal/client"
"github.com/debricked/cli/internal/file"
"github.com/debricked/cli/internal/fingerprint"
"github.com/debricked/cli/internal/git"
"github.com/debricked/cli/internal/resolution"
"github.com/debricked/cli/internal/tui"
Expand All @@ -34,7 +35,7 @@ type DebrickedScanner struct {
uploader *upload.IUploader
ciService ci.IService
resolver resolution.IResolver
fingerprint file.IFingerprint
fingerprint fingerprint.IFingerprint
}

type DebrickedOptions struct {
Expand All @@ -57,7 +58,7 @@ func NewDebrickedScanner(
uploader upload.IUploader,
ciService ci.IService,
resolver resolution.IResolver,
fingerprint file.IFingerprint,
fingerprint fingerprint.IFingerprint,
) *DebrickedScanner {
return &DebrickedScanner{
c,
Expand Down Expand Up @@ -138,7 +139,7 @@ func (dScanner *DebrickedScanner) scanFingerprint(options DebrickedOptions) erro
if err != nil {
return err
}
err = fingerprints.ToFile(file.OutputFileNameFingerprints)
err = fingerprints.ToFile(fingerprint.OutputFileNameFingerprints)

return err
}
Expand Down
5 changes: 3 additions & 2 deletions internal/scan/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/debricked/cli/internal/client"
"github.com/debricked/cli/internal/client/testdata"
"github.com/debricked/cli/internal/file"
"github.com/debricked/cli/internal/fingerprint"
"github.com/debricked/cli/internal/git"
"github.com/debricked/cli/internal/resolution"
resolveTestdata "github.com/debricked/cli/internal/resolution/testdata"
Expand All @@ -52,7 +53,7 @@ func TestNewDebrickedScanner(t *testing.T) {
var finder file.IFinder
var uploader upload.IUploader
var resolver resolution.IResolver
var fingerprint file.IFingerprint
var fingerprint fingerprint.IFingerprint
s := NewDebrickedScanner(&debClient, finder, uploader, cis, resolver, fingerprint)

assert.NotNil(t, s)
Expand Down Expand Up @@ -633,7 +634,7 @@ func TestScanWithFingerprint(t *testing.T) {
resolverMock.SetFiles([]string{"yarn.lock"})

scanner := makeScanner(clientMock, &resolverMock)
scanner.fingerprint = file.NewFingerprinter()
scanner.fingerprint = fingerprint.NewFingerprinter()

cwd, _ := os.Getwd()
defer resetWd(t, cwd)
Expand Down
7 changes: 4 additions & 3 deletions internal/wire/cli_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/debricked/cli/internal/ci"
"github.com/debricked/cli/internal/client"
"github.com/debricked/cli/internal/file"
"github.com/debricked/cli/internal/fingerprint"
licenseReport "github.com/debricked/cli/internal/report/license"
vulnerabilityReport "github.com/debricked/cli/internal/report/vulnerability"
"github.com/debricked/cli/internal/resolution"
Expand Down Expand Up @@ -47,7 +48,7 @@ func (cc *CliContainer) wire() error {
}
cc.finder = finder

fingerprinter := file.NewFingerprinter()
fingerprinter := fingerprint.NewFingerprinter()

cc.fingerprinter = fingerprinter

Expand Down Expand Up @@ -88,7 +89,7 @@ type CliContainer struct {
retryClient *retryablehttp.Client
debClient client.IDebClient
finder file.IFinder
fingerprinter file.IFingerprint
fingerprinter fingerprint.IFingerprint
uploader upload.IUploader
ciService ci.IService
scanner scan.IScanner
Expand Down Expand Up @@ -124,7 +125,7 @@ func (cc *CliContainer) VulnerabilityReporter() vulnerabilityReport.Reporter {
return cc.vulnerabilityReporter
}

func (cc *CliContainer) Fingerprinter() file.IFingerprint {
func (cc *CliContainer) Fingerprinter() fingerprint.IFingerprint {
return cc.fingerprinter
}

Expand Down

0 comments on commit fa28ac1

Please sign in to comment.