Module: FiberAudit::CLI
- Defined in:
- lib/fiber_audit/cli.rb
Overview
rubocop:disable Metrics/ModuleLength
Class Method Summary collapse
- .analyze_project(options, cwd, stderr) ⇒ Object
- .color_enabled?(stdout, options) ⇒ Boolean
- .default_format(stdout, output_path) ⇒ Object
- .expand_target_map(targets, separator) ⇒ Object
- .explain_rule(argv, stdout:, stderr:) ⇒ Object
- .list_rules(stdout) ⇒ Object
- .parse_static_options(argv) ⇒ Object
- .print_help(output = $stdout) ⇒ Object
- .publish_report(report, output_path, stdout, cwd) ⇒ Object
- .reject_arguments!(argv) ⇒ Object
- .render_report(format, result, color:) ⇒ Object
- .reportable_findings?(findings, minimum) ⇒ Boolean
- .rule_targets(rule_class) ⇒ Object
- .rule_title(rule_class) ⇒ Object
- .run_static(argv, stdout:, stderr:, cwd:) ⇒ Object
- .start(argv, stdout: $stdout, stderr: $stderr, cwd: Dir.pwd) ⇒ Object
- .unknown_project_note(project) ⇒ Object
- .with_min_severity(configuration, severity) ⇒ Object
Class Method Details
.analyze_project(options, cwd, stderr) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/fiber_audit/cli.rb', line 80 def analyze_project(, cwd, stderr) project = Project.detect(start_path: cwd) stderr.puts unknown_project_note(project) unless project.known? config_path = project.config_path([:config]) if [:config] && !File.file?(config_path) raise ConfigurationError, "configuration file does not exist: #{config_path}" end configuration = Configuration.load(config_path) configuration = with_min_severity(configuration, [:min_severity]) if [:min_severity] result = Audit.new(configuration: configuration, root: project.root).call [configuration, result] end |
.color_enabled?(stdout, options) ⇒ Boolean
130 131 132 |
# File 'lib/fiber_audit/cli.rb', line 130 def color_enabled?(stdout, ) ![:no_color] && ![:out] && stdout.respond_to?(:tty?) && stdout.tty? end |
.default_format(stdout, output_path) ⇒ Object
124 125 126 127 128 |
# File 'lib/fiber_audit/cli.rb', line 124 def default_format(stdout, output_path) return 'json' if output_path stdout.respond_to?(:tty?) && stdout.tty? ? 'text' : 'json' end |
.expand_target_map(targets, separator) ⇒ Object
228 229 230 231 232 |
# File 'lib/fiber_audit/cli.rb', line 228 def (targets, separator) targets.flat_map do |constant, methods| Array(methods).map { |method| "#{constant}#{separator}#{method}" } end end |
.explain_rule(argv, stdout:, stderr:) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/fiber_audit/cli.rb', line 172 def explain_rule(argv, stdout:, stderr:) rule_id = argv.shift reject_arguments!(argv) unless rule_id stderr.puts 'Usage: fiber-audit explain <RULE_ID>' return 2 end rule_class = Static::Rules::BuiltIns.registry.find(rule_id) unless rule_class stderr.puts "Unknown rule: #{rule_id}" return 2 end stdout.puts "#{rule_class.id} — #{rule_title(rule_class)}" stdout.puts "Default severity: #{rule_class.default_severity}" stdout.puts "Default confidence: #{rule_class.default_confidence}" stdout.puts "Description: #{rule_class.description}" stdout.puts 'Targets:' rule_targets(rule_class).each { |target| stdout.puts " - #{target}" } stdout.puts "Remediation: #{rule_class.const_get(:REMEDIATION)}" 0 end |
.list_rules(stdout) ⇒ Object
162 163 164 165 166 167 168 169 170 |
# File 'lib/fiber_audit/cli.rb', line 162 def list_rules(stdout) Static::Rules::BuiltIns.registry.each do |rule_class| stdout.puts format('%<id>-7s %<severity>-8s %<description>s', id: rule_class.id, severity: rule_class.default_severity.to_s.upcase, description: rule_class.description) end 0 end |
.parse_static_options(argv) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/fiber_audit/cli.rb', line 95 def (argv) = { format: nil, config: nil, out: nil, min_severity: nil, no_color: false, help: false } parser = OptionParser.new do |opts| opts. = 'Usage: fiber-audit static [options]' opts.on('--format FORMAT', %w[text json], 'Output format: text or json') { |value| [:format] = value } opts.on('--config PATH', 'Path to configuration file') { |value| [:config] = value } opts.on('--out PATH', 'Write report to a file') { |value| [:out] = value } opts.on('--min-severity SEVERITY', Severity::LEVELS.map(&:to_s), 'Minimum reported severity') do |value| [:min_severity] = value.to_sym end opts.on('--no-color', 'Disable ANSI colors') { [:no_color] = true } opts.on('-h', '--help', 'Show static options') { [:help] = true } end parser.parse!(argv) reject_arguments!(argv) [, parser] end |
.print_help(output = $stdout) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/fiber_audit/cli.rb', line 50 def print_help(output = $stdout) output.puts <<~HELP Usage: fiber-audit <command> [options] Commands: static Run static fiber-compatibility analysis list-rules List all registered static rules explain ID Explain a specific rule version Print version help Show this help Run `fiber-audit static --help` for analysis options. HELP end |
.publish_report(report, output_path, stdout, cwd) ⇒ Object
145 146 147 148 149 150 151 152 153 154 |
# File 'lib/fiber_audit/cli.rb', line 145 def publish_report(report, output_path, stdout, cwd) unless output_path stdout.write(report) return end resolved_path = File.(output_path, cwd) File.write(resolved_path, report) stdout.puts "Report written to #{output_path}" end |
.reject_arguments!(argv) ⇒ Object
234 235 236 237 238 |
# File 'lib/fiber_audit/cli.rb', line 234 def reject_arguments!(argv) return if argv.empty? raise OptionParser::InvalidArgument, "unexpected arguments: #{argv.join(' ')}" end |
.render_report(format, result, color:) ⇒ Object
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/fiber_audit/cli.rb', line 134 def render_report(format, result, color:) case format when 'text' Reporters::Text.new(color: color).render(result) when 'json' Reporters::JSON.new(pretty: true).render(result) else raise OptionParser::InvalidArgument, "unsupported format: #{format}" end end |
.reportable_findings?(findings, minimum) ⇒ Boolean
156 157 158 159 160 |
# File 'lib/fiber_audit/cli.rb', line 156 def reportable_findings?(findings, minimum) findings.any? do |finding| Severity.index(finding.severity) <= Severity.index(minimum) end end |
.rule_targets(rule_class) ⇒ Object
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/fiber_audit/cli.rb', line 205 def rule_targets(rule_class) case rule_class.id when 'FA1001' (rule_class::TARGETS, '.') when 'FA1002' rule_class::TARGET_METHODS.map { |method| "Thread##{method}" } when 'FA1003' (rule_class::TARGETS, '#') when 'FA1004' rule_class::THREAD_VARIABLE_METHODS.map { |method| "Thread##{method}" } + rule_class::INDEX_METHODS.map { |method| "Thread.current.#{method}" } when 'FA1005' (rule_class::TARGETS.transform_values { |method| [method] }, '.') when 'FA1006' rule_class::EXACT.map { |constant| "#{constant}.new" } + ['IPSocket subclasses'] when 'FA1007' rule_class::NET_HTTP_METHODS.map { |method| "Net::HTTP.#{method}" } + rule_class::URI_METHODS.map { |constant, method| "#{constant}.#{method}" } else [] end end |
.rule_title(rule_class) ⇒ Object
196 197 198 199 200 201 202 203 |
# File 'lib/fiber_audit/cli.rb', line 196 def rule_title(rule_class) return 'Blocking subprocess call' if rule_class.id == 'FA1001' %i[TITLE RULE_TITLE].each do |name| return rule_class.const_get(name) if rule_class.const_defined?(name, false) end rule_class.name.split('::').last end |
.run_static(argv, stdout:, stderr:, cwd:) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/fiber_audit/cli.rb', line 65 def run_static(argv, stdout:, stderr:, cwd:) , parser = (argv) if [:help] stdout.puts parser return 0 end configuration, result = analyze_project(, cwd, stderr) format = [:format] || default_format(stdout, [:out]) report = render_report(format, result, color: color_enabled?(stdout, )) publish_report(report, [:out], stdout, cwd) reportable_findings?(result.findings, configuration.min_severity) ? 1 : 0 end |
.start(argv, stdout: $stdout, stderr: $stderr, cwd: Dir.pwd) ⇒ Object
19 20 21 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 |
# File 'lib/fiber_audit/cli.rb', line 19 def start(argv, stdout: $stdout, stderr: $stderr, cwd: Dir.pwd) args = argv.dup command = args.shift case command when 'static' run_static(args, stdout: stdout, stderr: stderr, cwd: cwd) when 'list-rules' reject_arguments!(args) list_rules(stdout) when 'explain' explain_rule(args, stdout: stdout, stderr: stderr) when 'version', '--version', '-v' reject_arguments!(args) stdout.puts "fiber-audit #{FiberAudit::VERSION}" 0 when nil print_help(stdout) 0 when 'help', '--help', '-h' reject_arguments!(args) print_help(stdout) 0 else raise OptionParser::InvalidArgument, "unknown command: #{command}" end rescue StandardError => e stderr.puts "fiber-audit: #{e.}" 2 end |
.unknown_project_note(project) ⇒ Object
240 241 242 |
# File 'lib/fiber_audit/cli.rb', line 240 def unknown_project_note(project) "Note: project root could not be detected; using #{project.root}" end |
.with_min_severity(configuration, severity) ⇒ Object
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/fiber_audit/cli.rb', line 113 def with_min_severity(configuration, severity) Configuration.new( static_include: configuration.static_include, static_exclude: configuration.static_exclude, rules_config: configuration.rules_config, report_formats: configuration.report_formats, min_severity: severity, suppressions_path: configuration.suppressions_path ) end |