Skip to content

Commit 6d63a31

Browse files
authored
Merge pull request #175 from snyk/refactor/all-projects-use-flowconfig
refactor: use FlowConfig for allProjects flag
2 parents 3993dfc + ec5fdc6 commit 6d63a31

File tree

4 files changed

+24
-22
lines changed

4 files changed

+24
-22
lines changed

internal/commands/ostest/routing.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ type FlowConfig struct {
132132
RequiresLegacy bool
133133
Unmanaged bool
134134
TargetPackage string
135+
AllProjects bool
135136
}
136137

137138
func doesPathExist(path string) (bool, error) {
@@ -159,6 +160,7 @@ func ParseFlowConfig(cfg configuration.Configuration) (*FlowConfig, error) {
159160
sbomReachabilityTest := reachability && sbom != ""
160161
reachabilityFilter := cfg.GetString(flags.FlagReachabilityFilter)
161162
unmanaged := cfg.GetBool(flags.FlagUnmanaged)
163+
allProjects := cfg.GetBool(flags.FlagAllProjects)
162164

163165
experimentalFlagSet := cfg.GetBool(configuration.FLAG_EXPERIMENTAL)
164166
experimentalUvSupport := experimentalFlagSet && cfg.GetBool(constants.EnableExperimentalUvSupportEnvVar)
@@ -201,11 +203,12 @@ func ParseFlowConfig(cfg configuration.Configuration) (*FlowConfig, error) {
201203
RequiresLegacy: requiresLegacy,
202204
Unmanaged: unmanaged,
203205
TargetPackage: targetPackage,
206+
AllProjects: allProjects,
204207
}, nil
205208
}
206209

207210
// ShouldUseLegacyFlow determines if the command should route to legacy CLI based on flags.
208-
func ShouldUseLegacyFlow(ctx context.Context, allProjectsFlagSet bool, fc *FlowConfig, inputDirs []string) (bool, error) {
211+
func ShouldUseLegacyFlow(ctx context.Context, fc *FlowConfig, inputDirs []string) (bool, error) {
209212
errFactory := cmdctx.ErrorFactory(ctx)
210213
logger := cmdctx.Logger(ctx)
211214

@@ -214,7 +217,7 @@ func ShouldUseLegacyFlow(ctx context.Context, allProjectsFlagSet bool, fc *FlowC
214217
}
215218

216219
// Check if UV support should trigger, only if env var is set and uv.lock exists.
217-
uvSupportWithLockFile := fc.ExperimentalUvSupport && util.HasUvLockFileInAnyDir(inputDirs, allProjectsFlagSet, logger)
220+
uvSupportWithLockFile := fc.ExperimentalUvSupport && util.HasUvLockFileInAnyDir(inputDirs, fc.AllProjects, logger)
218221

219222
hasNewFeatures := fc.RiskScoreTest || fc.Reachability || fc.SBOM != "" || fc.ReachabilityFilter != "" || uvSupportWithLockFile
220223
useLegacy := fc.ForceLegacyTest || fc.RequiresLegacy || !hasNewFeatures

internal/commands/ostest/routing_test.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func Test_ShouldUseLegacyFlow(t *testing.T) {
9191

9292
flowCfg, err := ostest.ParseFlowConfig(cfg)
9393
require.NoError(t, err)
94-
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, false, flowCfg, []string{"."})
94+
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, flowCfg, []string{"."})
9595
require.NoError(t, err)
9696

9797
assert.True(t, useLegacy)
@@ -107,7 +107,7 @@ func Test_ShouldUseLegacyFlow(t *testing.T) {
107107

108108
flowCfg, err := ostest.ParseFlowConfig(testCfg)
109109
require.NoError(t, err)
110-
_, err = ostest.ShouldUseLegacyFlow(ctx, false, flowCfg, []string{"."})
110+
_, err = ostest.ShouldUseLegacyFlow(ctx, flowCfg, []string{"."})
111111

112112
assert.Error(t, err)
113113
})
@@ -131,7 +131,7 @@ func Test_ShouldUseLegacyFlow(t *testing.T) {
131131

132132
flowCfg, err := ostest.ParseFlowConfig(testCfg)
133133
require.NoError(t, err)
134-
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, false, flowCfg, []string{"."})
134+
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, flowCfg, []string{"."})
135135
require.NoError(t, err)
136136

137137
assert.True(t, useLegacy)
@@ -148,7 +148,7 @@ func Test_ShouldUseLegacyFlow(t *testing.T) {
148148

149149
flowCfg, err := ostest.ParseFlowConfig(testCfg)
150150
require.NoError(t, err)
151-
_, err = ostest.ShouldUseLegacyFlow(ctx, false, flowCfg, []string{"."})
151+
_, err = ostest.ShouldUseLegacyFlow(ctx, flowCfg, []string{"."})
152152

153153
assert.Error(t, err)
154154
})
@@ -169,7 +169,7 @@ func Test_ShouldUseLegacyFlow(t *testing.T) {
169169

170170
flowCfg, err := ostest.ParseFlowConfig(cfg)
171171
require.NoError(t, err)
172-
_, err = ostest.ShouldUseLegacyFlow(ctx, false, flowCfg, []string{"."})
172+
_, err = ostest.ShouldUseLegacyFlow(ctx, flowCfg, []string{"."})
173173
require.Error(t, err)
174174
var catalogErr snyk_errors.Error
175175
require.ErrorAs(t, err, &catalogErr)
@@ -191,7 +191,7 @@ func Test_ShouldUseLegacyFlow(t *testing.T) {
191191

192192
flowCfg, err := ostest.ParseFlowConfig(cfg)
193193
require.NoError(t, err)
194-
_, err = ostest.ShouldUseLegacyFlow(ctx, false, flowCfg, []string{"."})
194+
_, err = ostest.ShouldUseLegacyFlow(ctx, flowCfg, []string{"."})
195195
require.Error(t, err)
196196
var catalogErr snyk_errors.Error
197197
require.ErrorAs(t, err, &catalogErr)
@@ -209,7 +209,7 @@ func Test_ShouldUseLegacyFlow(t *testing.T) {
209209

210210
flowCfg, err := ostest.ParseFlowConfig(cfg)
211211
require.NoError(t, err)
212-
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, false, flowCfg, []string{"."})
212+
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, flowCfg, []string{"."})
213213
require.NoError(t, err)
214214

215215
assert.True(t, useLegacy)
@@ -227,7 +227,7 @@ func Test_ShouldUseLegacyFlow(t *testing.T) {
227227

228228
flowCfg, err := ostest.ParseFlowConfig(cfg)
229229
require.NoError(t, err)
230-
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, false, flowCfg, []string{"."})
230+
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, flowCfg, []string{"."})
231231
require.NoError(t, err)
232232

233233
assert.True(t, useLegacy)
@@ -246,7 +246,7 @@ func Test_ShouldUseLegacyFlow(t *testing.T) {
246246

247247
flowCfg, err := ostest.ParseFlowConfig(cfg)
248248
require.NoError(t, err)
249-
_, err = ostest.ShouldUseLegacyFlow(ctx, false, flowCfg, []string{"."})
249+
_, err = ostest.ShouldUseLegacyFlow(ctx, flowCfg, []string{"."})
250250
require.Error(t, err)
251251

252252
var catalogErr snyk_errors.Error
@@ -271,7 +271,7 @@ func Test_ShouldUseLegacyFlow(t *testing.T) {
271271

272272
flowCfg, err := ostest.ParseFlowConfig(cfg)
273273
require.NoError(t, err)
274-
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, false, flowCfg, []string{tempDir})
274+
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, flowCfg, []string{tempDir})
275275
require.NoError(t, err)
276276

277277
assert.False(t, useLegacy, "should use new flow when experimental flag and UV support are enabled and uv.lock exists")
@@ -294,7 +294,7 @@ func Test_ShouldUseLegacyFlow(t *testing.T) {
294294

295295
flowCfg, err := ostest.ParseFlowConfig(cfg)
296296
require.NoError(t, err)
297-
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, false, flowCfg, []string{tempDir})
297+
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, flowCfg, []string{tempDir})
298298
require.NoError(t, err)
299299

300300
assert.True(t, useLegacy, "should use legacy flow when UV support is enabled but uv.lock is missing")
@@ -317,7 +317,7 @@ func Test_ShouldUseLegacyFlow(t *testing.T) {
317317

318318
flowCfg, err := ostest.ParseFlowConfig(cfg)
319319
require.NoError(t, err)
320-
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, false, flowCfg, []string{tempDir})
320+
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, flowCfg, []string{tempDir})
321321
require.NoError(t, err)
322322

323323
assert.True(t, useLegacy, "should use legacy flow when experimental flag is not set regardless of UV support")
@@ -338,7 +338,7 @@ func Test_ShouldUseLegacyFlow(t *testing.T) {
338338

339339
flowCfg, err := ostest.ParseFlowConfig(cfg)
340340
require.NoError(t, err)
341-
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, false, flowCfg, []string{tempDir})
341+
useLegacy, err := ostest.ShouldUseLegacyFlow(ctx, flowCfg, []string{tempDir})
342342
require.NoError(t, err)
343343

344344
assert.True(t, useLegacy, "should use legacy flow when UV support is disabled regardless of uv.lock presence")

internal/commands/ostest/workflow.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,7 @@ func OSWorkflow(
320320
return nil, err
321321
}
322322

323-
allProjectsFlagSet := cfg.GetBool(flags.FlagAllProjects)
324-
useLegacy, err := ShouldUseLegacyFlow(ctx, allProjectsFlagSet, flowCfg, inputDirs)
323+
useLegacy, err := ShouldUseLegacyFlow(ctx, flowCfg, inputDirs)
325324
if err != nil {
326325
return nil, err
327326
}

internal/util/uv.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ var ExcludedUVLockFileDirs = map[string]bool{
2020
}
2121

2222
// HasUvLockFile checks if the specified directory contains a uv.lock file.
23-
// If allProjectsFlagSet is true, the function will check if the directory contains a uv.lock file recursively.
23+
// If allProjects is true, the function will check if the directory contains a uv.lock file recursively.
2424
// Otherwise, it will only check if the directory contains a uv.lock file.
25-
func HasUvLockFile(dir string, allProjectsFlagSet bool, logger *zerolog.Logger) bool {
26-
if allProjectsFlagSet {
25+
func HasUvLockFile(dir string, allProjects bool, logger *zerolog.Logger) bool {
26+
if allProjects {
2727
return HasUvLockFileRecursive(dir, logger)
2828
}
2929
return HasUvLockFileSingle(dir, logger)
@@ -95,9 +95,9 @@ func HasUvLockFileRecursive(dir string, logger *zerolog.Logger) bool {
9595
}
9696

9797
// HasUvLockFileInAnyDir checks if any of the input directories contains a uv.lock file.
98-
func HasUvLockFileInAnyDir(inputDirs []string, allProjectsFlagSet bool, logger *zerolog.Logger) bool {
98+
func HasUvLockFileInAnyDir(inputDirs []string, allProjects bool, logger *zerolog.Logger) bool {
9999
for _, inputDir := range inputDirs {
100-
if HasUvLockFile(inputDir, allProjectsFlagSet, logger) {
100+
if HasUvLockFile(inputDir, allProjects, logger) {
101101
return true
102102
}
103103
}

0 commit comments

Comments
 (0)