discourse/migrations/lib/cli/exception_handler.rb
Gerhard Schlager 58558849ac
DEV: Add options to run only or skip some steps in converter and importer (#35127)
Examples:

* Run only the "badges" and "users" steps in the converter:
  ```
  migrations/bin/cli convert discourse --reset --only=badges,users
  ```

* Run all steps except "users" in the converter:
  ```
  migrations/bin/cli convert discourse --reset --skip=users
  ```

* Run only the "badges" step in importer:
   This also runs all steps that "badges" depends on (e.g. "users" and "uploads").
  ```
  migrations/bin/cli import --reset --only=badges
  ```

* Run only the "badges" step in importer and skip the "users" on which "badges" depends:
  ```
  migrations/bin/cli import --reset --only=badges --skip=users
  ```
2025-10-03 16:44:36 +02:00

39 lines
1 KiB
Ruby

# frozen_string_literal: true
module Migrations
module CLI
class ExceptionHandler
def self.handle_and_exit(&block)
block.call
rescue ClassFilter::UnknownClassNamesError => e
handle_unknown_class_names_error(e)
exit(1)
rescue => e
puts "An error occurred: #{e.message}".red
puts e.backtrace.join("\n")
exit(1)
end
private
def self.handle_unknown_class_names_error(error)
all_suggestions_found = true
error.missing_names.each do |missing_name|
suggestions =
DidYouMean::SpellChecker.new(dictionary: error.available_names).correct(missing_name)
puts "Unknown step '#{missing_name}'".red
if suggestions.any?
puts "Did you mean: #{suggestions.join(", ")}".yellow
else
all_suggestions_found = false
end
end
if !all_suggestions_found
puts "Available steps are: ".yellow + error.available_names.join(", ")
end
end
end
end
end