-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add contract for code scanner and repository
- Loading branch information
Showing
4 changed files
with
119 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package code | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/safedep/code/plugin/depsusage" | ||
"github.com/safedep/vet/ent" | ||
) | ||
|
||
// Currently we only need this in CodeScanner | ||
type writerRepository interface { | ||
SaveDependencyUsage(context.Context, *depsusage.UsageEvidence) error | ||
} | ||
|
||
// Repository exposed to rest of the vet to query code analysis data | ||
// persisted in the storage. This is a contract to the rest of the system | ||
type ReaderRepository interface { | ||
// Stuff like GetEvidenceByPackageName(...) | ||
} | ||
|
||
type writerRepositoryImpl struct { | ||
client *ent.Client | ||
} | ||
|
||
func newWriterRepository(client *ent.Client) (writerRepository, error) { | ||
return &writerRepositoryImpl{ | ||
client: client, | ||
}, nil | ||
} | ||
|
||
func (r *writerRepositoryImpl) SaveDependencyUsage(ctx context.Context, evidence *depsusage.UsageEvidence) error { | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package code | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/safedep/code/core" | ||
"github.com/safedep/vet/ent" | ||
"github.com/safedep/vet/pkg/storage" | ||
) | ||
|
||
// User define configuration for the scanner | ||
type ScannerConfig struct { | ||
// First party application code directories | ||
AppDirectories []string | ||
|
||
// 3rd party imported code directories (e.g. Python virtual env, `node_modules` etc.) | ||
ImportDirectories []string | ||
|
||
// Languages to scan | ||
Languages []core.Language | ||
|
||
// Define callbacks if required | ||
Callbacks *ScannerCallbackRegistry | ||
|
||
// Plugin specific configuration | ||
SkipDependencyUsagePlugin bool | ||
} | ||
|
||
type ScannerCallbackRegistry struct { | ||
// On start of scan | ||
OnScanStart func() error | ||
|
||
// On end of scan | ||
OnScanEnd func() error | ||
} | ||
|
||
// Scanner defines the contract for implementing a code scanner. The purpose | ||
// of code scanner is to scan configured directories for code files, | ||
// parse them, process them with plugins, persist the plugin results. It | ||
// should also expose the necessary callbacks for interactive applications | ||
// to show progress to user. | ||
type Scanner interface { | ||
Scan(context.Context) error | ||
} | ||
|
||
type scanner struct { | ||
config ScannerConfig | ||
storage storage.Storage[*ent.Client] | ||
writer writerRepository | ||
} | ||
|
||
func NewScanner(config ScannerConfig, storage storage.Storage[*ent.Client]) (Scanner, error) { | ||
client, err := storage.Client() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get ent client: %w", err) | ||
} | ||
|
||
writer, err := newWriterRepository(client) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create writer repository: %w", err) | ||
} | ||
|
||
return &scanner{ | ||
config: config, | ||
storage: storage, | ||
writer: writer, | ||
}, nil | ||
} | ||
|
||
func (s *scanner) Scan(ctx context.Context) error { | ||
// Create the file system walker with config | ||
|
||
// Initialize the plugins | ||
|
||
// Start the tree walker with plugins | ||
|
||
// Handle results from plugins | ||
|
||
// Use repository to persist the results | ||
|
||
return nil | ||
} |