Skip to content

Commit

Permalink
Ensure code DB path exists & exclude patterns option
Browse files Browse the repository at this point in the history
Signed-off-by: Omkar Phansopkar <[email protected]>
  • Loading branch information
OmkarPh committed Feb 7, 2025
1 parent 1e9fae0 commit a08d1c6
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions cmd/code/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package code

import (
"context"
"regexp"

"github.com/safedep/vet/internal/command"
"github.com/safedep/vet/internal/ui"
Expand All @@ -15,6 +16,7 @@ var (
dbPath string
appDirs []string
importDirs []string
excludePatterns []string
skipDependencyUsagePlugin bool
)

Expand All @@ -31,6 +33,8 @@ func newScanCommand() *cobra.Command {
cmd.Flags().StringVar(&dbPath, "db", "", "Path to create the sqlite database")
cmd.Flags().StringArrayVar(&appDirs, "app", []string{"."}, "Directories to scan for application code files")
cmd.Flags().StringArrayVar(&importDirs, "import-dir", []string{}, "Directories to scan for import files")
cmd.Flags().StringArrayVarP(&excludePatterns, "exclude", "", []string{},
"Name patterns to ignore while scanning a codebase")
cmd.Flags().BoolVar(&skipDependencyUsagePlugin, "skip-dependency-usage-plugin", false, "Skip dependency usage plugin analysis")

_ = cmd.MarkFlagRequired("db")
Expand Down Expand Up @@ -59,9 +63,15 @@ func internalStartScan() error {
return err
}

excludePatternsRegexps := []*regexp.Regexp{}
for _, pattern := range excludePatterns {
excludePatternsRegexps = append(excludePatternsRegexps, regexp.MustCompile(pattern))
}

codeScanner, err := code.NewScanner(code.ScannerConfig{
AppDirectories: appDirs,
ImportDirectories: importDirs,
ExcludePatterns: excludePatternsRegexps,
Languages: allowedLanguages,
SkipDependencyUsagePlugin: skipDependencyUsagePlugin,
Callbacks: &code.ScannerCallbackRegistry{
Expand Down
8 changes: 8 additions & 0 deletions pkg/storage/ent_sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package storage
import (
"context"
"fmt"
"os"
"path/filepath"

_ "github.com/mattn/go-sqlite3"
"github.com/safedep/vet/ent"
Expand All @@ -29,6 +31,12 @@ func NewEntSqliteStorage(config EntSqliteClientConfig) (Storage[*ent.Client], er
mode = "ro"
}

// Ensure the path exists
dir := filepath.Dir(config.Path)
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return nil, fmt.Errorf("failed to create DB path %s: %w", dir, err)
}

dbConn := fmt.Sprintf("file:%s?mode=%s&cache=private&_fk=1", config.Path, mode)
client, err := ent.Open("sqlite3", dbConn)
if err != nil {
Expand Down

0 comments on commit a08d1c6

Please sign in to comment.