From 937dc42f101be905e3af41b879901a4445b0223a Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 28 Sep 2023 10:04:05 -0400 Subject: [PATCH] Extract methods for file movement in `CLI::Upgrade` (#25120) --- .rubocop.yml | 6 ------ lib/mastodon/cli/upgrade.rb | 42 ++++++++++++++++++++++--------------- 2 files changed, 25 insertions(+), 23 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index c8a433c729..4684ef06db 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -75,12 +75,6 @@ Metrics/AbcSize: - 'lib/mastodon/cli/*.rb' - db/*migrate/**/* -# Reason: -# https://docs.rubocop.org/rubocop/cops_metrics.html#metricsblocknesting -Metrics/BlockNesting: - Exclude: - - 'lib/mastodon/cli/*.rb' - # Reason: Currently disabled in .rubocop_todo.yml # https://docs.rubocop.org/rubocop/cops_metrics.html#metricscyclomaticcomplexity Metrics/CyclomaticComplexity: diff --git a/lib/mastodon/cli/upgrade.rb b/lib/mastodon/cli/upgrade.rb index 52b5540c40..cf83986844 100644 --- a/lib/mastodon/cli/upgrade.rb +++ b/lib/mastodon/cli/upgrade.rb @@ -125,27 +125,12 @@ module Mastodon::CLI progress.log("Moving #{previous_path} to #{upgraded_path}") if options[:verbose] begin - unless dry_run? - FileUtils.mkdir_p(File.dirname(upgraded_path)) - FileUtils.mv(previous_path, upgraded_path) - - begin - FileUtils.rmdir(File.dirname(previous_path), parents: true) - rescue Errno::ENOTEMPTY - # OK - end - end + move_previous_to_upgraded rescue => e progress.log(pastel.red("Error processing #{previous_path}: #{e}")) success = false - unless dry_run? - begin - FileUtils.rmdir(File.dirname(upgraded_path), parents: true) - rescue Errno::ENOTEMPTY - # OK - end - end + remove_directory end end @@ -155,5 +140,28 @@ module Mastodon::CLI attachment.instance_write(:storage_schema_version, previous_storage_schema_version) success end + + def move_previous_to_upgraded(previous_path, upgraded_path) + return if dry_run? + + FileUtils.mkdir_p(File.dirname(upgraded_path)) + FileUtils.mv(previous_path, upgraded_path) + + begin + FileUtils.rmdir(File.dirname(previous_path), parents: true) + rescue Errno::ENOTEMPTY + # OK + end + end + + def remove_directory(path) + return if dry_run? + + begin + FileUtils.rmdir(File.dirname(path), parents: true) + rescue Errno::ENOTEMPTY + # OK + end + end end end