Skip to content

Commit

Permalink
Adds forced regeneration flag
Browse files Browse the repository at this point in the history
  • Loading branch information
filip-debricked committed Dec 22, 2023
1 parent 41801a6 commit 57a3d6b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 24 deletions.
15 changes: 12 additions & 3 deletions internal/cmd/resolve/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ import (

var exclusions = file.Exclusions()
var verbose bool
var regenerate int

const (
ExclusionFlag = "exclusion"
VerboseFlag = "verbose"
ExclusionFlag = "exclusion"
VerboseFlag = "verbose"
RegenerateFlag = "regenerate"
)

func NewResolveCmd(resolver resolution.IResolver) *cobra.Command {
Expand Down Expand Up @@ -48,6 +50,13 @@ Exclude flags could alternatively be set using DEBRICKED_EXCLUSIONS="path1,path2
Example:
$ debricked resolve . `+exampleFlags)
cmd.Flags().BoolVar(&verbose, VerboseFlag, true, "set to false to disable extensive resolution error messages")
cmd.Flags().IntVar(&regenerate, RegenerateFlag, 0, `Toggles regeneration of lock files between 3 modes:
0: Default = Only generate new lock files for manifest files without matching lock files.
1: Regenerate Non-Native: Only generate new lock files for manifest files without matching lock files or matching debricked lock file.
2: Regenerate: Regenerate all existing lock files and generate new lock files for all manifest files.
Example:
$ debricked resolve . --regenerate=1`)

viper.MustBindEnv(ExclusionFlag)

Expand All @@ -59,7 +68,7 @@ func RunE(resolver resolution.IResolver) func(_ *cobra.Command, args []string) e
if len(args) == 0 {
args = append(args, ".")
}
_, err := resolver.Resolve(args, viper.GetStringSlice(ExclusionFlag), viper.GetBool(VerboseFlag))
_, err := resolver.Resolve(args, viper.GetStringSlice(ExclusionFlag), viper.GetBool(VerboseFlag), viper.GetInt(RegenerateFlag))

return err
}
Expand Down
10 changes: 10 additions & 0 deletions internal/cmd/scan/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var repositoryUrl string
var integrationName string
var exclusions = file.Exclusions()
var verbose bool
var regenerate int
var noResolve bool
var noFingerprint bool
var passOnDowntime bool
Expand All @@ -36,6 +37,7 @@ const (
IntegrationFlag = "integration"
ExclusionFlag = "exclusion"
VerboseFlag = "verbose"
RegenerateFlag = "regenerate"
NoResolveFlag = "no-resolve"
FingerprintFlag = "fingerprint"
PassOnTimeOut = "pass-on-timeout"
Expand Down Expand Up @@ -92,6 +94,13 @@ Exclude flags could alternatively be set using DEBRICKED_EXCLUSIONS="path1,path2
Examples:
$ debricked scan . `+exampleFlags)
cmd.Flags().BoolVar(&verbose, VerboseFlag, true, "set to false to disable extensive resolution error messages")
cmd.Flags().IntVar(&regenerate, RegenerateFlag, 0, `Toggles regeneration of lock files between 3 modes:
0: Default = Only generate new lock files for manifest files without matching lock files.
1: Regenerate Non-Native: Only generate new lock files for manifest files without matching lock files or matching debricked lock file.
2: Regenerate: Regenerate all existing lock files and generate new lock files for all manifest files.
Example:
$ debricked resolve . --regenerate=1`)
cmd.Flags().BoolVarP(&passOnDowntime, PassOnTimeOut, "p", false, "pass scan if there is a service access timeout")
cmd.Flags().BoolVar(&noResolve, NoResolveFlag, false, `disables resolution of manifest files that lack lock files. Resolving manifest files enables more accurate dependency scanning since the whole dependency tree will be analysed.
For example, if there is a "go.mod" in the target path, its dependencies are going to get resolved onto a lock file, and latter scanned.`)
Expand Down Expand Up @@ -124,6 +133,7 @@ func RunE(s *scan.IScanner) func(_ *cobra.Command, args []string) error {
Fingerprint: viper.GetBool(FingerprintFlag),
Exclusions: viper.GetStringSlice(ExclusionFlag),
Verbose: viper.GetBool(VerboseFlag),
Regenerate: viper.GetInt(RegenerateFlag),
RepositoryName: viper.GetString(RepositoryFlag),
CommitName: viper.GetString(CommitFlag),
BranchName: viper.GetString(BranchFlag),
Expand Down
45 changes: 37 additions & 8 deletions internal/resolution/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package resolution
import (
"os"
"path"
"regexp"

"github.com/debricked/cli/internal/file"
resolutionFile "github.com/debricked/cli/internal/resolution/file"
Expand All @@ -12,7 +13,7 @@ import (
)

type IResolver interface {
Resolve(paths []string, exclusions []string, verbose bool) (IResolution, error)
Resolve(paths []string, exclusions []string, verbose bool, regenerate int) (IResolution, error)
}

type Resolver struct {
Expand All @@ -36,12 +37,11 @@ func NewResolver(
}
}

func (r Resolver) Resolve(paths []string, exclusions []string, verbose bool) (IResolution, error) {
files, err := r.refinePaths(paths, exclusions)
func (r Resolver) Resolve(paths []string, exclusions []string, verbose bool, regenerate int) (IResolution, error) {
files, err := r.refinePaths(paths, exclusions, regenerate)
if err != nil {
return nil, err
}

pmBatches := r.batchFactory.Make(files)

var jobs []job.IJob
Expand All @@ -66,7 +66,7 @@ func (r Resolver) Resolve(paths []string, exclusions []string, verbose bool) (IR
return resolution, err
}

func (r Resolver) refinePaths(paths []string, exclusions []string) ([]string, error) {
func (r Resolver) refinePaths(paths []string, exclusions []string, regenerate int) ([]string, error) {
var fileSet = map[string]bool{}
var dirs []string
for _, arg := range paths {
Expand All @@ -89,7 +89,7 @@ func (r Resolver) refinePaths(paths []string, exclusions []string) ([]string, er
}
}

err := r.searchDirs(fileSet, dirs, exclusions)
err := r.searchDirs(fileSet, dirs, exclusions, regenerate)
if err != nil {
return nil, err
}
Expand All @@ -102,7 +102,7 @@ func (r Resolver) refinePaths(paths []string, exclusions []string) ([]string, er
return files, nil
}

func (r Resolver) searchDirs(fileSet map[string]bool, dirs []string, exclusions []string) error {
func (r Resolver) searchDirs(fileSet map[string]bool, dirs []string, exclusions []string, regenerate int) error {
for _, dir := range dirs {
fileGroups, err := r.finder.GetGroups(
dir,
Expand All @@ -114,11 +114,40 @@ func (r Resolver) searchDirs(fileSet map[string]bool, dirs []string, exclusions
return err
}
for _, fileGroup := range fileGroups.ToSlice() {
if fileGroup.HasFile() && !fileGroup.HasLockFiles() {
shouldGenerate := shouldGenerateLock(fileGroup, regenerate)
if shouldGenerate {
fileSet[fileGroup.ManifestFile] = true
}
}
}

return nil
}

func shouldGenerateLock(fileGroup file.Group, regenerate int) bool {
if !fileGroup.HasFile() {
return false
}
switch regenerate {
case 0:
return !fileGroup.HasLockFiles()
case 1:
return onlyNonNativeLockFiles(fileGroup.LockFiles)
case 2:
return true
}

return false
}

func onlyNonNativeLockFiles(lockFiles []string) bool {
debrickedLockFilePattern := regexp.MustCompile(`.*\.debricked\.lock`)
for _, lockFile := range lockFiles {
if !debrickedLockFilePattern.MatchString(lockFile) {
return false
}
}

return true

}
22 changes: 11 additions & 11 deletions internal/resolution/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestResolve(t *testing.T) {
NewScheduler(workers),
)

res, err := r.Resolve([]string{"../../go.mod"}, nil, true)
res, err := r.Resolve([]string{"../../go.mod"}, nil, true, 0)
assert.NotEmpty(t, res.Jobs())
assert.NoError(t, err)
}
Expand All @@ -53,7 +53,7 @@ func TestResolveInvokeError(t *testing.T) {
NewScheduler(workers),
)

_, err := r.Resolve([]string{"../../go.mod"}, nil, true)
_, err := r.Resolve([]string{"../../go.mod"}, nil, true, 0)
assert.NotNil(t, err)
}

Expand All @@ -65,7 +65,7 @@ func TestResolveStrategyError(t *testing.T) {
NewScheduler(workers),
)

res, err := r.Resolve([]string{"../../go.mod"}, nil, true)
res, err := r.Resolve([]string{"../../go.mod"}, nil, true, 0)
assert.Empty(t, res.Jobs())
assert.NoError(t, err)
}
Expand All @@ -79,7 +79,7 @@ func TestResolveScheduleError(t *testing.T) {
SchedulerMock{Err: errAssertion},
)

res, err := r.Resolve([]string{"../../go.mod"}, nil, true)
res, err := r.Resolve([]string{"../../go.mod"}, nil, true, 0)
assert.NotEmpty(t, res.Jobs())
assert.ErrorIs(t, err, errAssertion)
}
Expand All @@ -92,7 +92,7 @@ func TestResolveDirWithoutManifestFiles(t *testing.T) {
SchedulerMock{},
)

res, err := r.Resolve([]string{"."}, nil, true)
res, err := r.Resolve([]string{"."}, nil, true, 0)
assert.Empty(t, res.Jobs())
assert.NoError(t, err)
}
Expand All @@ -105,7 +105,7 @@ func TestResolveInvalidDir(t *testing.T) {
SchedulerMock{},
)

_, err := r.Resolve([]string{"invalid-dir"}, nil, true)
_, err := r.Resolve([]string{"invalid-dir"}, nil, true, 0)
assert.Error(t, err)
}

Expand All @@ -121,7 +121,7 @@ func TestResolveGetGroupsErr(t *testing.T) {
SchedulerMock{},
)

_, err := r.Resolve([]string{"."}, nil, true)
_, err := r.Resolve([]string{"."}, nil, true, 0)
assert.ErrorIs(t, testErr, err)
}

Expand All @@ -147,9 +147,9 @@ func TestResolveDirWithManifestFiles(t *testing.T) {
SchedulerMock{},
)

for _, dir := range cases {
for i, dir := range cases {
t.Run(fmt.Sprintf("Case: %s", dir), func(t *testing.T) {
res, err := r.Resolve([]string{dir}, nil, true)
res, err := r.Resolve([]string{dir}, nil, true, i%3) // i%3 to test the different regenerate values
assert.Len(t, res.Jobs(), 1)
j := res.Jobs()[0]
assert.False(t, j.Errors().HasError())
Expand All @@ -172,7 +172,7 @@ func TestResolveDirWithExclusions(t *testing.T) {
SchedulerMock{},
)

res, err := r.Resolve([]string{"."}, []string{"dir"}, true)
res, err := r.Resolve([]string{"."}, []string{"dir"}, true, 0)

assert.Len(t, res.Jobs(), 1)
j := res.Jobs()[0]
Expand All @@ -199,7 +199,7 @@ func TestResolveHasResolutionErrs(t *testing.T) {
schedulerMock,
)

res, err := r.Resolve([]string{""}, []string{""}, true)
res, err := r.Resolve([]string{""}, []string{""}, true, 0)

assert.NoError(t, err)
assert.Len(t, res.Jobs(), 1)
Expand Down
2 changes: 1 addition & 1 deletion internal/resolution/testdata/resolver_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type ResolverMock struct {
files []string
}

func (r *ResolverMock) Resolve(_ []string, _ []string, _ bool) (resolution.IResolution, error) {
func (r *ResolverMock) Resolve(_ []string, _ []string, _ bool, _ int) (resolution.IResolution, error) {
for _, f := range r.files {
createdFile, err := os.Create(f)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/scan/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type DebrickedOptions struct {
CallGraph bool
Exclusions []string
Verbose bool
Regenerate int
RepositoryName string
CommitName string
BranchName string
Expand Down Expand Up @@ -133,7 +134,7 @@ func (dScanner *DebrickedScanner) Scan(o IOptions) error {

func (dScanner *DebrickedScanner) scanResolve(options DebrickedOptions) error {
if options.Resolve {
_, resErr := dScanner.resolver.Resolve([]string{options.Path}, options.Exclusions, options.Verbose)
_, resErr := dScanner.resolver.Resolve([]string{options.Path}, options.Exclusions, options.Verbose, options.Regenerate)
if resErr != nil {
return resErr
}
Expand Down

0 comments on commit 57a3d6b

Please sign in to comment.