diff --git a/.github/workflows/debricked.yml b/.github/workflows/debricked.yml index ee16785e..75ecf2f4 100644 --- a/.github/workflows/debricked.yml +++ b/.github/workflows/debricked.yml @@ -26,4 +26,4 @@ jobs: - run: | printf "$(go mod graph)\n\n$(go list -mod=readonly -e -m all)" > .debricked-go-dependencies.txt - run: | - go run cmd/debricked/main.go scan . -t ${{ secrets.DEBRICKED_TOKEN }} -e "**/testdata/**" + go run cmd/debricked/main.go scan -t ${{ secrets.DEBRICKED_TOKEN }} -e "**/testdata/**" diff --git a/pkg/ci/service.go b/pkg/ci/service.go index 8e309d18..ccc58fa0 100644 --- a/pkg/ci/service.go +++ b/pkg/ci/service.go @@ -46,7 +46,7 @@ func (s *Service) Find() (env.Env, error) { for _, ci := range s.cis { if ci.Identify() { m, err := ci.Map() - fmt.Println("Integration", m.Integration) + fmt.Println("Integration:", m.Integration) return m, err } } diff --git a/pkg/cmd/files/find/find.go b/pkg/cmd/files/find/find.go index d0a59962..8c1a5ef5 100644 --- a/pkg/cmd/files/find/find.go +++ b/pkg/cmd/files/find/find.go @@ -2,12 +2,10 @@ package find import ( "encoding/json" - "errors" "fmt" "github.com/debricked/cli/pkg/file" "github.com/spf13/cobra" "github.com/spf13/viper" - "os" "path/filepath" ) @@ -27,7 +25,6 @@ func NewFindCmd(finder file.IFinder) *cobra.Command { Short: "Find all dependency files in inputted path", Long: `Find all dependency files in inputted path. Related files are grouped together. For example ` + "`package.json`" + ` with ` + "`package-lock.json`.", - Args: validateArgs, PreRun: func(cmd *cobra.Command, _ []string) { _ = viper.BindPFlags(cmd.Flags()) }, @@ -70,8 +67,11 @@ Format: func RunE(f file.IFinder) func(_ *cobra.Command, args []string) error { return func(_ *cobra.Command, args []string) error { - directoryPath := args[0] - fileGroups, err := f.GetGroups(directoryPath, viper.GetStringSlice(ExclusionFlag), viper.GetBool(LockfileOnlyFlag)) + path := "" + if len(args) > 0 { + path = args[0] + } + fileGroups, err := f.GetGroups(path, viper.GetStringSlice(ExclusionFlag), viper.GetBool(LockfileOnlyFlag)) if err != nil { return err } @@ -87,22 +87,3 @@ func RunE(f file.IFinder) func(_ *cobra.Command, args []string) error { return nil } } - -func validateArgs(_ *cobra.Command, args []string) error { - if len(args) < 1 { - return errors.New("requires path") - } - if isValidFilepath(args[0]) { - return nil - } - return fmt.Errorf("invalid path specified: %s", args[0]) -} - -func isValidFilepath(path string) bool { - _, err := os.ReadDir(path) - if err != nil { - return false - } - - return true -} diff --git a/pkg/cmd/files/find/find_test.go b/pkg/cmd/files/find/find_test.go index 9048dbc2..62632d67 100644 --- a/pkg/cmd/files/find/find_test.go +++ b/pkg/cmd/files/find/find_test.go @@ -6,7 +6,6 @@ import ( "github.com/debricked/cli/pkg/file" "github.com/debricked/cli/pkg/file/testdata" "github.com/spf13/viper" - "strings" "testing" ) @@ -63,6 +62,18 @@ func TestRunE(t *testing.T) { } } +func TestRunENoPath(t *testing.T) { + f := testdata.NewFinderMock() + groups := file.Groups{} + groups.Add(file.Group{}) + f.SetGetGroupsReturnMock(groups, nil) + runE := RunE(f) + err := runE(nil, []string{}) + if err != nil { + t.Fatal("failed to assert that no error occurred. Error:", err) + } +} + func TestRunENoFiles(t *testing.T) { f := testdata.NewFinderMock() groups := file.Groups{} @@ -86,28 +97,3 @@ func TestRunEError(t *testing.T) { t.Fatal("failed to assert that error occured") } } - -func TestValidateArgs(t *testing.T) { - err := validateArgs(nil, []string{"."}) - if err != nil { - t.Error("failed to assert that no error occurred") - } -} - -func TestValidateArgsInvalidArgs(t *testing.T) { - err := validateArgs(nil, []string{}) - if err == nil { - t.Error("failed to assert that an error occurred") - } - if !strings.Contains(err.Error(), "requires path") { - t.Error("failed to assert error message") - } - - err = validateArgs(nil, []string{"invalid-path"}) - if err == nil { - t.Error("failed to assert that an error occurred") - } - if !strings.Contains(err.Error(), "invalid path specified") { - t.Error("failed to assert error message") - } -} diff --git a/pkg/cmd/scan/scan.go b/pkg/cmd/scan/scan.go index 1b59aad8..139ec70a 100644 --- a/pkg/cmd/scan/scan.go +++ b/pkg/cmd/scan/scan.go @@ -10,7 +10,6 @@ import ( "github.com/fatih/color" "github.com/spf13/cobra" "github.com/spf13/viper" - "os" "path/filepath" ) @@ -46,7 +45,6 @@ func NewScanCmd(c *client.IDebClient) *cobra.Command { Short: "Start a Debricked dependency scan", Long: `All supported dependency files will be scanned and analysed. If the given path contains a git repository all flags but "integration" will be resolved. Otherwise they have to specified.`, - Args: ValidateArgs, PreRun: func(cmd *cobra.Command, _ []string) { _ = viper.BindPFlags(cmd.Flags()) }, @@ -85,9 +83,12 @@ $ debricked scan . `+exampleFlags) func RunE(s *scan.IScanner) func(_ *cobra.Command, args []string) error { return func(_ *cobra.Command, args []string) error { - directoryPath := args[0] + path := "" + if len(args) > 0 { + path = args[0] + } options := scan.DebrickedOptions{ - DirectoryPath: directoryPath, + Path: path, Exclusions: viper.GetStringSlice(ExclusionFlag), RepositoryName: viper.GetString(RepositoryFlag), CommitName: viper.GetString(CommitFlag), @@ -109,22 +110,3 @@ func RunE(s *scan.IScanner) func(_ *cobra.Command, args []string) error { return scanCmdError } } - -func ValidateArgs(_ *cobra.Command, args []string) error { - if len(args) < 1 { - return errors.New("requires directory path") - } - if isValidFilepath(args[0]) { - return nil - } - return fmt.Errorf("invalid directory path specified: %s", args[0]) -} - -func isValidFilepath(path string) bool { - _, err := os.ReadDir(path) - if err != nil { - return false - } - - return true -} diff --git a/pkg/cmd/scan/scan_test.go b/pkg/cmd/scan/scan_test.go index 32745396..7b656399 100644 --- a/pkg/cmd/scan/scan_test.go +++ b/pkg/cmd/scan/scan_test.go @@ -45,38 +45,21 @@ func TestNewScanCmd(t *testing.T) { } } -func TestValidateArgs(t *testing.T) { - err := ValidateArgs(nil, []string{"/"}) +func TestRunE(t *testing.T) { + var s scan.IScanner + s = &scannerMock{} + runE := RunE(&s) + err := runE(nil, []string{"."}) if err != nil { - t.Error("failed to assert that no error occurred. Error:", err) - } -} - -func TestValidateArgsMissingArg(t *testing.T) { - err := ValidateArgs(nil, []string{}) - if err == nil { - t.Error("failed to assert that an error occurred") - } - if !strings.Contains(err.Error(), "requires directory path") { - t.Error("failed assert error") - } -} - -func TestValidateArgsInvalidArg(t *testing.T) { - err := ValidateArgs(nil, []string{"invalid-path"}) - if err == nil { - t.Error("failed to assert that an error occurred") - } - if !strings.Contains(err.Error(), "invalid directory path specified") { - t.Error("failed assert error") + t.Fatal("failed to assert that no error occurred. Error:", err) } } -func TestRunE(t *testing.T) { +func TestRunENoPath(t *testing.T) { var s scan.IScanner s = &scannerMock{} runE := RunE(&s) - err := runE(nil, []string{"."}) + err := runE(nil, []string{}) if err != nil { t.Fatal("failed to assert that no error occurred. Error:", err) } diff --git a/pkg/file/finder.go b/pkg/file/finder.go index 2517fdb9..556a10a8 100644 --- a/pkg/file/finder.go +++ b/pkg/file/finder.go @@ -36,6 +36,9 @@ func (finder *Finder) GetGroups(rootPath string, exclusions []string, lockfileOn if err != nil { return groups, err } + if len(rootPath) == 0 { + rootPath = filepath.Base("") + } // Traverse files to find dependency file groups err = filepath.Walk( diff --git a/pkg/file/finder_test.go b/pkg/file/finder_test.go index ba592744..3d12d13a 100644 --- a/pkg/file/finder_test.go +++ b/pkg/file/finder_test.go @@ -110,13 +110,13 @@ func TestGetSupportedFormatsFailed(t *testing.T) { func TestGetGroups(t *testing.T) { setUp(true) - directoryPath := "." + path := "" exclusions := []string{"testdata/go/*.mod", "testdata/misc/**"} excludedFiles := []string{"testdata/go/go.mod", "testdata/misc/requirements.txt"} const nbrOfGroups = 2 - fileGroups, err := finder.GetGroups(directoryPath, exclusions, false) + fileGroups, err := finder.GetGroups(path, exclusions, false) if err != nil { t.Fatal("failed to assert that no error occurred. Error:", err) } @@ -124,7 +124,7 @@ func TestGetGroups(t *testing.T) { t.Error(fmt.Sprintf("failed to assert that %d groups were created. %d was found", nbrOfGroups, fileGroups.Size())) } for _, fileGroup := range fileGroups.ToSlice() { - hasContent := fileGroup.CompiledFormat != nil && (strings.Contains(fileGroup.FilePath, directoryPath) || len(fileGroup.RelatedFiles) > 0) + hasContent := fileGroup.CompiledFormat != nil && (strings.Contains(fileGroup.FilePath, path) || len(fileGroup.RelatedFiles) > 0) if !hasContent { t.Error("failed to assert that format had content") } diff --git a/pkg/git/git.go b/pkg/git/git.go index 4ce53e42..55e9df49 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -80,8 +80,8 @@ func FindRemoteUrl(repository *git.Repository) (string, error) { return remoteURL, err } -func FindRepositoryName(repository *git.Repository, directoryPath string) (string, error) { - absolutePath, _ := filepath.Abs(directoryPath) +func FindRepositoryName(repository *git.Repository, path string) (string, error) { + absolutePath, _ := filepath.Abs(path) repositoryName := filepath.Base(absolutePath) gitRemoteUrl, err := FindRemoteUrl(repository) if err != nil { @@ -108,8 +108,8 @@ func ParseGitRemoteUrl(gitRemoteUrl string) (string, error) { return gitRemoteUrl, errors.New("failed to parse git remote URL. git/https regular expressions had no matches") } -func FindRepository(directoryPath string) (*git.Repository, error) { - return git.PlainOpen(directoryPath) +func FindRepository(path string) (*git.Repository, error) { + return git.PlainOpen(path) } func FindBranch(repository *git.Repository) (string, error) { diff --git a/pkg/git/meta_object.go b/pkg/git/meta_object.go index 899cfd5a..c7034b9a 100644 --- a/pkg/git/meta_object.go +++ b/pkg/git/meta_object.go @@ -13,14 +13,14 @@ type MetaObject struct { Author string } -// NewMetaObject returns MetaObject based on git repository existing on directoryPath. Otherwise, inputted arguments are used -func NewMetaObject(directoryPath string, repositoryName string, commit string, branchName string, commitAuthor string, url string) (*MetaObject, error) { - repository, err := FindRepository(directoryPath) +// NewMetaObject returns MetaObject based on git repository existing on path. Otherwise, inputted arguments are used +func NewMetaObject(path string, repositoryName string, commit string, branchName string, commitAuthor string, url string) (*MetaObject, error) { + repository, err := FindRepository(path) if err == nil { isSet := func(attribute string) bool { return len(attribute) > 0 } if !isSet(repositoryName) { - repositoryName, err = FindRepositoryName(repository, directoryPath) + repositoryName, err = FindRepositoryName(repository, path) if err != nil { log.Println(err.Error()) } diff --git a/pkg/scan/scanner.go b/pkg/scan/scanner.go index 2ebf0474..27508205 100644 --- a/pkg/scan/scanner.go +++ b/pkg/scan/scanner.go @@ -31,7 +31,7 @@ type DebrickedScanner struct { } type DebrickedOptions struct { - DirectoryPath string + Path string Exclusions []string RepositoryName string CommitName string @@ -70,7 +70,7 @@ func (dScanner *DebrickedScanner) Scan(o IOptions) error { MapEnvToOptions(&dOptions, e) gitMetaObject, err := git.NewMetaObject( - dOptions.DirectoryPath, + dOptions.Path, dOptions.RepositoryName, dOptions.CommitName, dOptions.BranchName, @@ -81,7 +81,7 @@ func (dScanner *DebrickedScanner) Scan(o IOptions) error { return err } - fileGroups, err := dScanner.finder.GetGroups(dOptions.DirectoryPath, dOptions.Exclusions, false) + fileGroups, err := dScanner.finder.GetGroups(dOptions.Path, dOptions.Exclusions, false) if err != nil { return err } @@ -128,9 +128,8 @@ func MapEnvToOptions(o *DebrickedOptions, env env.Env) { o.IntegrationName = env.Integration } } - - if len(o.DirectoryPath) == 0 { - o.DirectoryPath = env.Filepath + if len(env.Filepath) > 0 { + o.Path = env.Filepath } } diff --git a/pkg/scan/scanner_test.go b/pkg/scan/scanner_test.go index 274da792..effbb13d 100644 --- a/pkg/scan/scanner_test.go +++ b/pkg/scan/scanner_test.go @@ -57,11 +57,11 @@ func TestScan(t *testing.T) { var ciService ci.IService ciService = ci.NewService(nil) scanner, _ := NewDebrickedScanner(&debClient, ciService) - directoryPath := "testdata/yarn" - repositoryName := directoryPath + path := "testdata/yarn" + repositoryName := path commitName := "testdata/yarn-commit" opts := DebrickedOptions{ - DirectoryPath: directoryPath, + Path: path, Exclusions: nil, RepositoryName: repositoryName, CommitName: commitName, @@ -91,9 +91,9 @@ func TestScanFailingMetaObject(t *testing.T) { travis.Ci{}, }) scanner, _ := NewDebrickedScanner(&debClient, ciService) - directoryPath := "testdata/yarn" + path := "testdata/yarn" opts := DebrickedOptions{ - DirectoryPath: directoryPath, + Path: path, Exclusions: nil, RepositoryName: "", CommitName: "", @@ -107,7 +107,7 @@ func TestScanFailingMetaObject(t *testing.T) { t.Error("failed to assert that RepositoryNameError occurred") } - opts.RepositoryName = directoryPath + opts.RepositoryName = path err = scanner.Scan(opts) if err != git.CommitNameError { t.Error("failed to assert that CommitNameError occurred") @@ -129,9 +129,9 @@ func TestScanFailingNoFiles(t *testing.T) { travis.Ci{}, }) scanner, _ := NewDebrickedScanner(&debClient, ciService) - directoryPath := "." + path := "." opts := DebrickedOptions{ - DirectoryPath: directoryPath, + Path: path, Exclusions: []string{"testdata/**"}, RepositoryName: "name", CommitName: "commit", @@ -158,7 +158,7 @@ func TestScanBadOpts(t *testing.T) { func TestMapEnvToOptions(t *testing.T) { dOptionsTemplate := DebrickedOptions{ - DirectoryPath: "path", + Path: "path", Exclusions: nil, RepositoryName: "repository", CommitName: "commit", @@ -191,7 +191,7 @@ func TestMapEnvToOptions(t *testing.T) { { name: "CI env set", template: DebrickedOptions{ - DirectoryPath: "env-path", + Path: "env-path", Exclusions: nil, RepositoryName: "env-repository", CommitName: "env-commit", @@ -201,7 +201,7 @@ func TestMapEnvToOptions(t *testing.T) { IntegrationName: github.Integration, }, opts: DebrickedOptions{ - DirectoryPath: "", + Path: "input-path", Exclusions: nil, RepositoryName: "", CommitName: "", @@ -220,12 +220,45 @@ func TestMapEnvToOptions(t *testing.T) { Filepath: "env-path", }, }, + { + name: "CI env set without directory path", + template: DebrickedOptions{ + Path: "input-path", + Exclusions: nil, + RepositoryName: "env-repository", + CommitName: "env-commit", + BranchName: "env-branch", + CommitAuthor: "author", + RepositoryUrl: "env-url", + IntegrationName: github.Integration, + }, + opts: DebrickedOptions{ + Path: "input-path", + Exclusions: nil, + RepositoryName: "", + CommitName: "", + BranchName: "", + CommitAuthor: "author", + RepositoryUrl: "", + IntegrationName: "CLI", + }, + env: env.Env{ + Repository: "env-repository", + Commit: "env-commit", + Branch: "env-branch", + Author: "env-author", + RepositoryUrl: "env-url", + Integration: github.Integration, + Filepath: "", + }, + }, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { MapEnvToOptions(&c.opts, c.env) - if c.opts.DirectoryPath != c.template.DirectoryPath { - t.Errorf("Failed to assert that %s was equal to %s", c.opts.DirectoryPath, c.template.DirectoryPath) + strings.EqualFold(c.opts.Path, c.template.Path) + if !strings.EqualFold(c.opts.Path, c.template.Path) { + t.Errorf("Failed to assert that %s was equal to %s", c.opts.Path, c.template.Path) } if c.opts.Exclusions != nil { t.Errorf("Failed to assert that Exclusions was nil")