From 35c1c3e57a416370fdd62720d85c54490d6c8e20 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 1 Jun 2023 08:31:24 -0400 Subject: [PATCH] Add CLI area progress bar helper (#25208) --- lib/mastodon/cli/base.rb | 27 +++++++++++++++-- .../cli/{helper.rb => progress_helper.rb} | 30 +++++++++---------- 2 files changed, 39 insertions(+), 18 deletions(-) rename lib/mastodon/cli/{helper.rb => progress_helper.rb} (80%) diff --git a/lib/mastodon/cli/base.rb b/lib/mastodon/cli/base.rb index f3c9fea921..32aff2fcc5 100644 --- a/lib/mastodon/cli/base.rb +++ b/lib/mastodon/cli/base.rb @@ -4,16 +4,39 @@ require_relative '../../../config/boot' require_relative '../../../config/environment' require 'thor' -require_relative 'helper' +require_relative 'progress_helper' module Mastodon module CLI class Base < Thor - include CLI::Helper + include ProgressHelper def self.exit_on_failure? true end + + private + + def pastel + @pastel ||= Pastel.new + end + + def dry_run? + options[:dry_run] + end + + def dry_run_mode_suffix + dry_run? ? ' (DRY RUN)' : '' + end + + def reset_connection_pools! + ActiveRecord::Base.establish_connection( + ActiveRecord::Base.configurations.configs_for(env_name: Rails.env).first.configuration_hash + .dup + .tap { |config| config['pool'] = options[:concurrency] + 1 } + ) + RedisConfiguration.establish_pool(options[:concurrency]) + end end end end diff --git a/lib/mastodon/cli/helper.rb b/lib/mastodon/cli/progress_helper.rb similarity index 80% rename from lib/mastodon/cli/helper.rb rename to lib/mastodon/cli/progress_helper.rb index 78931b9a22..1fa2745c18 100644 --- a/lib/mastodon/cli/helper.rb +++ b/lib/mastodon/cli/progress_helper.rb @@ -9,23 +9,19 @@ HttpLog.configuration.logger = dev_null Paperclip.options[:log] = false Chewy.logger = dev_null -module Mastodon::CLI - module Helper - def dry_run? - options[:dry_run] - end +require 'ruby-progressbar/outputs/null' - def dry_run_mode_suffix - dry_run? ? ' (DRY RUN)' : '' - end +module Mastodon::CLI + module ProgressHelper + PROGRESS_FORMAT = '%c/%u |%b%i| %e' def create_progress_bar(total = nil) - ProgressBar.create(total: total, format: '%c/%u |%b%i| %e') - end - - def reset_connection_pools! - ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[Rails.env].dup.tap { |config| config['pool'] = options[:concurrency] + 1 }) - RedisConfiguration.establish_pool(options[:concurrency]) + ProgressBar.create( + { + total: total, + format: PROGRESS_FORMAT, + }.merge(progress_output_options) + ) end def parallelize_with_progress(scope) @@ -82,8 +78,10 @@ module Mastodon::CLI [total.value, aggregate.value] end - def pastel - @pastel ||= Pastel.new + private + + def progress_output_options + Rails.env.test? ? { output: ProgressBar::Outputs::Null } : {} end end end