Ensure code DB path exists & exclude patterns option (#345)

Signed-off-by: Omkar Phansopkar <omkarphansopkar@gmail.com>
This commit is contained in:
Omkar Phansopkar 2025-02-07 21:51:42 +05:30 committed by GitHub
parent 1e9fae0330
commit a47764af5f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 18 additions and 0 deletions

View File

@ -2,6 +2,7 @@ package code
import (
"context"
"regexp"
"github.com/safedep/vet/internal/command"
"github.com/safedep/vet/internal/ui"
@ -15,6 +16,7 @@ var (
dbPath string
appDirs []string
importDirs []string
excludePatterns []string
skipDependencyUsagePlugin bool
)
@ -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")
@ -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{

View File

@ -3,6 +3,8 @@ package storage
import (
"context"
"fmt"
"os"
"path/filepath"
_ "github.com/mattn/go-sqlite3"
"github.com/safedep/vet/ent"
@ -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 {