Skip to content

Commit b7cde79

Browse files
committed
fix: deal with various dirs
1 parent 6a624c5 commit b7cde79

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

nix/packages/cis-audit/scanner/cmd/cis-generate-spec/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ func run(cmd *cobra.Command, args []string) error {
9898
IncludeProcesses: includeProcess,
9999
ShallowDirs: shallowDirs,
100100
ShallowDepth: shallowDepth,
101+
ShallowDepthSet: cmd.Flags().Changed("shallow-depth"),
101102
}
102103
cfg, err := config.Load(configFile, cliOpts)
103104
if err != nil {

nix/packages/cis-audit/scanner/internal/config/loader.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ type CLIOptions struct {
5757
ShallowDirs []string
5858

5959
// ShallowDepth controls recursion depth in shallow directories (from CLI)
60+
// Use -1 to indicate "not set" (will use default), 0+ for explicit depth
6061
ShallowDepth int
62+
63+
// ShallowDepthSet indicates whether ShallowDepth was explicitly set via CLI
64+
ShallowDepthSet bool
6165
}
6266

6367
// Load reads configuration from defaults, optional config file, and CLI options.
@@ -105,12 +109,11 @@ func Load(configPath string, opts CLIOptions) (*Config, error) {
105109
}
106110

107111
// Set shallow depth from CLI (overrides config file and defaults)
108-
if opts.ShallowDepth > 0 {
112+
// ShallowDepthSet allows explicit 0 to be distinguished from "not set"
113+
if opts.ShallowDepthSet {
109114
cfg.ShallowDepth = opts.ShallowDepth
110-
}
111-
112-
// Default shallow depth to 1 if not set
113-
if cfg.ShallowDepth == 0 {
115+
} else if cfg.ShallowDepth == 0 {
116+
// Default shallow depth to 1 only if not explicitly set
114117
cfg.ShallowDepth = 1
115118
}
116119

nix/packages/cis-audit/scanner/internal/scanners/files.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ func (s *FileScanner) Scan(ctx context.Context, opts ScanOptions) (ScanStats, er
7474
}
7575

7676
// Handle shallow directories - limit recursion depth
77-
if d != nil && d.IsDir() {
78-
depth := cfg.GetShallowDirDepth(path)
79-
if depth >= 0 {
77+
depth := cfg.GetShallowDirDepth(path)
78+
if depth >= 0 {
79+
if d != nil && d.IsDir() {
8080
if depth == 0 && cfg.ShallowDepth == 0 {
8181
// Depth 0 means capture this directory entry but don't recurse into it
8282
info, err := d.Info()
@@ -95,6 +95,12 @@ func (s *FileScanner) Scan(ctx context.Context, opts ScanOptions) (ScanStats, er
9595
opts.Logger.Debug("Skipping directory beyond shallow depth", "path", path, "depth", depth, "max_depth", cfg.ShallowDepth)
9696
return filepath.SkipDir
9797
}
98+
} else if d != nil && d.Type().IsRegular() {
99+
// For files inside shallow dirs, skip if at or beyond shallow depth
100+
if depth > cfg.ShallowDepth {
101+
opts.Logger.Debug("Skipping file beyond shallow depth", "path", path, "depth", depth, "max_depth", cfg.ShallowDepth)
102+
return nil
103+
}
98104
}
99105
}
100106

0 commit comments

Comments
 (0)