Class: Ace::Git::Secrets::Commands::ScanCommand

Inherits:
Object
  • Object
show all
Defined in:
lib/ace/git/secrets/commands/scan_command.rb

Overview

CLI command for scanning repository for tokens Requires gitleaks to be installed (brew install gitleaks)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ScanCommand

Returns a new instance of ScanCommand.



17
18
19
20
# File 'lib/ace/git/secrets/commands/scan_command.rb', line 17

def initialize(options)
  @options = options
  @config = Ace::Git::Secrets.config
end

Class Method Details

.execute(options) ⇒ Integer

Execute scan command

Parameters:

  • options (Hash)

    Command options

Returns:

  • (Integer)

    Exit code (0=clean, 1=tokens found, 2=error)



13
14
15
# File 'lib/ace/git/secrets/commands/scan_command.rb', line 13

def self.execute(options)
  new(options).execute
end

Instance Method Details

#executeObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ace/git/secrets/commands/scan_command.rb', line 22

def execute
  # Check gitleaks availability early with clear error
  Atoms::GitleaksRunner.ensure_available!

  auditor = Organisms::SecurityAuditor.new(
    repository_path: ".",
    gitleaks_config: Ace::Git::Secrets.gitleaks_config_path,
    output_format: output_format,
    whitelist: load_whitelist,
    exclusions: load_exclusions
  )

  # Quiet mode suppresses verbose and uses minimal output
  quiet = @options[:quiet]
  verbose = !quiet && @options[:verbose]

  report = auditor.audit(
    since: @options[:since],
    min_confidence: @options[:confidence] || "low",
    verbose: verbose
  )

  # Save report to file (new default behavior)
  report_format = @options[:report_format]&.to_sym || :json
  output_directory = @config.dig("output", "directory")
  report_path = report.save_to_file(format: report_format, directory: output_directory, quiet: quiet)

  # Print output based on mode
  if quiet
    # In quiet mode, only print summary for CI
    puts report.clean? ? "clean" : "found:#{report.token_count}"
  elsif verbose
    # In verbose mode, print full table report
    puts auditor.format_report(report)
    puts
    # Show whitelisted count if any
    if auditor.whitelisted_count > 0
      puts "Whitelisted: #{auditor.whitelisted_count} token(s) excluded by whitelist rules"
      puts
    end
    puts auditor.next_steps(report)
    puts
    puts "Report saved: #{report_path}"
  else
    # Default: summary to stdout, full report to file
    puts report.to_summary(report_path: report_path)
    # Show whitelisted count if any
    if auditor.whitelisted_count > 0
      puts "Whitelisted: #{auditor.whitelisted_count} token(s) excluded by whitelist rules"
    end
    unless report.clean?
      puts
      puts auditor.next_steps(report)
    end
  end

  # Return appropriate exit code
  report.clean? ? 0 : 1
rescue Atoms::GitleaksRunner::GitleaksNotFoundError => e
  puts "Error: #{e.message}"
  2
rescue => e
  puts "Error: #{e.message}"
  puts e.backtrace.first(5).join("\n") if ENV["DEBUG"]
  2
end