Class: GemGuard::CLI
- Inherits:
-
Thor
- Object
- Thor
- GemGuard::CLI
- Defined in:
- lib/gem_guard/cli.rb
Constant Summary collapse
- EXIT_SUCCESS =
Exit codes for CI/CD integration
0
- EXIT_VULNERABILITIES_FOUND =
1
- EXIT_ERROR =
2
Class Method Summary collapse
Instance Method Summary collapse
- #config ⇒ Object
- #fix ⇒ Object
- #interactive ⇒ Object
- #sbom ⇒ Object
- #scan ⇒ Object
- #typosquat ⇒ Object
- #version ⇒ Object
Class Method Details
.exit_on_failure? ⇒ Boolean
14 15 16 |
# File 'lib/gem_guard/cli.rb', line 14 def self.exit_on_failure? true end |
Instance Method Details
#config ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/gem_guard/cli.rb', line 173 def config config_file = Config.new([:path]) if [:init] if File.exist?([:path]) puts "Config file #{[:path]} already exists" exit EXIT_ERROR end # Create default config file default_config = { "lockfile" => "Gemfile.lock", "format" => "table", "fail_on_vulnerabilities" => true, "severity_threshold" => "low", "ignore_vulnerabilities" => [], "ignore_gems" => [], "output_file" => nil, "project_name" => config_file.send(:detect_project_name), "sbom" => { "format" => "spdx", "include_dev_dependencies" => false }, "scan" => { "sources" => ["osv", "ruby_advisory_db"], "timeout" => 30 } } File.write([:path], YAML.dump(default_config)) puts "Created #{[:path]} with default configuration" elsif [:show] if config_file.exists? puts File.read([:path]) else puts "No config file found at #{[:path]}" puts "Run 'gem_guard config --init' to create one" end else puts "Usage: gem_guard config [--init|--show] [--path PATH]" puts " --init Create a new .gemguard.yml config file" puts " --show Display current configuration" puts " --path Specify config file path (default: .gemguard.yml)" end end |
#fix ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/gem_guard/cli.rb', line 226 def fix config = Config.new([:config] || ".gemguard.yml") lockfile_path = [:lockfile] || config.lockfile_path gemfile_path = [:gemfile] || "Gemfile" dry_run = [:dry_run] || false interactive = [:interactive] || false create_backup = ![:no_backup] unless File.exist?(lockfile_path) puts "Error: #{lockfile_path} not found" verbose_diagnostics([lockfile_path]) exit EXIT_ERROR end unless File.exist?(gemfile_path) puts "Error: #{gemfile_path} not found. Auto-fix requires a Gemfile." verbose_diagnostics([gemfile_path]) exit EXIT_ERROR end begin # First, scan for vulnerabilities dependencies = Parser.new.parse(lockfile_path) vulnerabilities = VulnerabilityFetcher.new.fetch_for(dependencies) analysis = Analyzer.new.analyze(dependencies, vulnerabilities) if analysis.vulnerable_dependencies.empty? puts "✅ No vulnerabilities found. Nothing to fix!" exit EXIT_SUCCESS end # Apply fixes auto_fixer = AutoFixer.new(lockfile_path, gemfile_path) result = auto_fixer.fix_vulnerabilities( analysis.vulnerable_dependencies, dry_run: dry_run, interactive: interactive, backup: create_backup ) case result[:status] when :no_fixes_needed puts "ℹ️ #{result[:message]}" exit EXIT_SUCCESS when :dry_run puts "🔍 Dry run — no files will be modified." puts "=" * 40 result[:fixes].each do |fix| puts "✅ Would update #{fix[:gem_name]} #{fix[:current_version]} → #{fix[:target_version]}" end puts "\n#{result[:message]}" puts "Run without --dry-run to apply these fixes." exit EXIT_SUCCESS when :cancelled puts "❌ #{result[:message]}" exit EXIT_SUCCESS when :completed puts "🎉 #{result[:message]}" puts "\n📋 Applied Fixes:" result[:fixes].each do |fix| puts "✅ #{fix[:gem_name]}: #{fix[:current_version]} → #{fix[:target_version]}" end puts "\n💡 Run 'gem_guard scan' to verify fixes." exit EXIT_SUCCESS else puts "❌ Unexpected error during fix operation" exit EXIT_ERROR end rescue GemGuard::InvalidLockfileError => e puts "Invalid Gemfile.lock: #{e.}" puts "Tip: Run 'bundle install' to regenerate your lockfile." exit EXIT_ERROR rescue GemGuard::FileError => e puts "File error: #{e.}" exit EXIT_ERROR rescue => e puts "Error: #{e.}" exit EXIT_ERROR end end |
#interactive ⇒ Object
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 |
# File 'lib/gem_guard/cli.rb', line 317 def interactive config = Config.new([:config] || ".gemguard.yml") lockfile_path = [:lockfile] || config.lockfile_path # 1. Scan for vulnerabilities puts "Scanning for vulnerabilities..." dependencies = Parser.new.parse(lockfile_path) vulnerabilities = VulnerabilityFetcher.new.fetch_for(dependencies) analysis = Analyzer.new.analyze(dependencies, vulnerabilities) if analysis.vulnerable_dependencies.empty? puts "✅ No vulnerabilities found." exit EXIT_SUCCESS end # 2. Report vulnerabilities Reporter.new.report(analysis, format: "table") # 3. Ask to fix prompt = TTY::Prompt.new if prompt.yes?("\nWould you like to fix these vulnerabilities interactively?") invoke :fix, [], .slice("lockfile", "gemfile", "config").merge(interactive: true) end end |
#sbom ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/gem_guard/cli.rb', line 86 def sbom lockfile_path = [:lockfile] unless File.exist?(lockfile_path) puts "Error: #{lockfile_path} not found" verbose_diagnostics([lockfile_path]) exit EXIT_ERROR end dependencies = Parser.new.parse(lockfile_path) generator = SbomGenerator.new sbom_data = case [:format].downcase when "spdx" generator.generate_spdx(dependencies, [:project]) when "cyclone-dx", "cyclonedx" generator.generate_cyclone_dx(dependencies, [:project]) else puts "Error: Unsupported format '#{[:format]}'. Use 'spdx' or 'cyclone-dx'" exit EXIT_ERROR end output_json = JSON.pretty_generate(sbom_data) if [:output] File.write([:output], output_json) puts "SBOM written to #{[:output]}" else puts output_json end end |
#scan ⇒ Object
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 |
# File 'lib/gem_guard/cli.rb', line 25 def scan config = Config.new([:config]) # Override config with CLI options lockfile_path = [:lockfile] || config.lockfile_path format = [:format] || config.output_format fail_on_vulns = [:fail_on_vulnerabilities].nil? ? config.fail_on_vulnerabilities? : [:fail_on_vulnerabilities] severity_threshold = [:severity_threshold] || config.severity_threshold output_file = [:output] || config.output_file unless File.exist?(lockfile_path) puts "Error: #{lockfile_path} not found" verbose_diagnostics([lockfile_path]) exit EXIT_ERROR end begin dependencies = Parser.new.parse(lockfile_path) vulnerabilities = VulnerabilityFetcher.new.fetch_for(dependencies) # Filter vulnerabilities based on config filtered_vulnerabilities = filter_vulnerabilities(vulnerabilities, config) analysis = Analyzer.new.analyze(dependencies, filtered_vulnerabilities) # Filter analysis based on severity threshold filtered_analysis = filter_analysis_by_severity(analysis, severity_threshold, config) if output_file output_content = capture_report_output(filtered_analysis, format) File.write(output_file, output_content) puts "Report written to #{output_file}" else Reporter.new.report(filtered_analysis, format: format) end # Exit with appropriate code for CI/CD if filtered_analysis.has_vulnerabilities? && fail_on_vulns exit EXIT_VULNERABILITIES_FOUND else exit EXIT_SUCCESS end rescue GemGuard::InvalidLockfileError => e puts "Invalid Gemfile.lock: #{e.}" puts "Tip: Run 'bundle install' to regenerate your lockfile." exit EXIT_ERROR rescue GemGuard::FileError => e puts "File error: #{e.}" verbose_diagnostics([lockfile_path, output_file].compact) exit EXIT_ERROR rescue => e puts "Error: #{e.}" exit EXIT_ERROR end end |
#typosquat ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/gem_guard/cli.rb', line 123 def typosquat config = Config.new([:config] || ".gemguard.yml") lockfile_path = [:lockfile] || config.lockfile_path format = [:format] || config.output_format output_file = [:output] || config.output_file unless File.exist?(lockfile_path) puts "Error: #{lockfile_path} not found" verbose_diagnostics([lockfile_path]) exit EXIT_ERROR end begin dependencies = Parser.new.parse(lockfile_path) checker = TyposquatChecker.new suspicious_gems = checker.check_dependencies(dependencies) if output_file output_content = format_typosquat_output(suspicious_gems, format) File.write(output_file, output_content) puts "Typosquat report written to #{output_file}" else display_typosquat_results(suspicious_gems, format) end # Exit with appropriate code if suspicious_gems.any? { |sg| sg[:risk_level] == "critical" || sg[:risk_level] == "high" } exit EXIT_VULNERABILITIES_FOUND else exit EXIT_SUCCESS end rescue GemGuard::InvalidLockfileError => e puts "Invalid Gemfile.lock: #{e.}" puts "Tip: Run 'bundle install' to regenerate your lockfile." exit EXIT_ERROR rescue GemGuard::FileError => e puts "File error: #{e.}" verbose_diagnostics([lockfile_path, output_file].compact) exit EXIT_ERROR rescue => e puts "Error: #{e.}" exit EXIT_ERROR end end |