Add CLI Base class for command line code (#25106)

This commit is contained in:
Matt Jankowski 2023-05-24 05:55:40 -04:00 committed by GitHub
parent d2e5430d4a
commit 384345b0de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
33 changed files with 238 additions and 155 deletions

View File

@ -1,18 +1,10 @@
# frozen_string_literal: true
require 'set'
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class Accounts < Thor
include Helper
def self.exit_on_failure?
true
end
class Accounts < Base
option :all, type: :boolean
desc 'rotate [USERNAME]', 'Generate and broadcast new keys'
long_desc <<-LONG_DESC

19
lib/mastodon/cli/base.rb Normal file
View File

@ -0,0 +1,19 @@
# frozen_string_literal: true
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require 'thor'
require_relative 'helper'
module Mastodon
module CLI
class Base < Thor
include CLI::Helper
def self.exit_on_failure?
true
end
end
end
end

View File

@ -1,17 +1,9 @@
# frozen_string_literal: true
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class Cache < Thor
include Helper
def self.exit_on_failure?
true
end
class Cache < Base
desc 'clear', 'Clear out the cache storage'
def clear
Rails.cache.clear

View File

@ -1,18 +1,10 @@
# frozen_string_literal: true
require 'concurrent'
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class CanonicalEmailBlocks < Thor
include Helper
def self.exit_on_failure?
true
end
class CanonicalEmailBlocks < Base
desc 'find EMAIL', 'Find a given e-mail address in the canonical e-mail blocks'
long_desc <<-LONG_DESC
When suspending a local user, a hash of a "canonical" version of their e-mail

View File

@ -1,18 +1,10 @@
# frozen_string_literal: true
require 'concurrent'
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class Domains < Thor
include Helper
def self.exit_on_failure?
true
end
class Domains < Base
option :concurrency, type: :numeric, default: 5, aliases: [:c]
option :verbose, type: :boolean, aliases: [:v]
option :dry_run, type: :boolean

View File

@ -1,18 +1,10 @@
# frozen_string_literal: true
require 'concurrent'
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class EmailDomainBlocks < Thor
include Helper
def self.exit_on_failure?
true
end
class EmailDomainBlocks < Base
desc 'list', 'List blocked e-mail domains'
def list
EmailDomainBlock.where(parent_id: nil).order(id: 'DESC').find_each do |entry|

View File

@ -1,16 +1,10 @@
# frozen_string_literal: true
require 'rubygems/package'
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class Emoji < Thor
def self.exit_on_failure?
true
end
class Emoji < Base
option :prefix
option :suffix
option :overwrite, type: :boolean

View File

@ -1,18 +1,11 @@
# frozen_string_literal: true
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class Feeds < Thor
include Helper
class Feeds < Base
include Redisable
def self.exit_on_failure?
true
end
option :all, type: :boolean, default: false
option :concurrency, type: :numeric, default: 5, aliases: [:c]
option :verbose, type: :boolean, aliases: [:v]

View File

@ -1,16 +1,10 @@
# frozen_string_literal: true
require 'rubygems/package'
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class IpBlocks < Thor
def self.exit_on_failure?
true
end
class IpBlocks < Base
option :severity, required: true, enum: %w(no_access sign_up_requires_approval sign_up_block), desc: 'Severity of the block'
option :comment, aliases: [:c], desc: 'Optional comment'
option :duration, aliases: [:d], type: :numeric, desc: 'Duration of the block in seconds'

View File

@ -1,29 +1,25 @@
# frozen_string_literal: true
require 'thor'
require_relative 'media'
require_relative 'emoji'
require_relative 'base'
require_relative 'accounts'
require_relative 'cache'
require_relative 'canonical_email_blocks'
require_relative 'domains'
require_relative 'email_domain_blocks'
require_relative 'emoji'
require_relative 'feeds'
require_relative 'ip_blocks'
require_relative 'maintenance'
require_relative 'media'
require_relative 'preview_cards'
require_relative 'search'
require_relative 'settings'
require_relative 'statuses'
require_relative 'domains'
require_relative 'preview_cards'
require_relative 'cache'
require_relative 'upgrade'
require_relative 'email_domain_blocks'
require_relative 'canonical_email_blocks'
require_relative 'ip_blocks'
require_relative 'maintenance'
require_relative '../version'
module Mastodon::CLI
class Main < Thor
def self.exit_on_failure?
true
end
class Main < Base
desc 'media SUBCOMMAND ...ARGS', 'Manage media files'
subcommand 'media', Media

View File

@ -1,18 +1,10 @@
# frozen_string_literal: true
require 'tty-prompt'
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class Maintenance < Thor
include Helper
def self.exit_on_failure?
true
end
class Maintenance < Base
MIN_SUPPORTED_VERSION = 2019_10_01_213028
MAX_SUPPORTED_VERSION = 2022_11_04_133904

View File

@ -1,20 +1,13 @@
# frozen_string_literal: true
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class Media < Thor
class Media < Base
include ActionView::Helpers::NumberHelper
include Helper
VALID_PATH_SEGMENTS_SIZE = [7, 10].freeze
def self.exit_on_failure?
true
end
option :days, type: :numeric, default: 7, aliases: [:d]
option :prune_profiles, type: :boolean, default: false
option :remove_headers, type: :boolean, default: false

View File

@ -1,18 +1,11 @@
# frozen_string_literal: true
require 'tty-prompt'
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class PreviewCards < Thor
class PreviewCards < Base
include ActionView::Helpers::NumberHelper
include Helper
def self.exit_on_failure?
true
end
option :days, type: :numeric, default: 180
option :concurrency, type: :numeric, default: 5, aliases: [:c]

View File

@ -1,13 +1,9 @@
# frozen_string_literal: true
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class Search < Thor
include Helper
class Search < Base
# Indices are sorted by amount of data to be expected in each, so that
# smaller indices can go online sooner
INDICES = [

View File

@ -1,15 +1,9 @@
# frozen_string_literal: true
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class Registrations < Thor
def self.exit_on_failure?
true
end
class Registrations < Base
desc 'open', 'Open registrations'
def open
Setting.registrations_mode = 'open'
@ -37,7 +31,7 @@ module Mastodon::CLI
end
end
class Settings < Thor
class Settings < Base
desc 'registrations SUBCOMMAND ...ARGS', 'Manage state of registrations'
subcommand 'registrations', Registrations
end

View File

@ -1,18 +1,11 @@
# frozen_string_literal: true
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class Statuses < Thor
include Helper
class Statuses < Base
include ActionView::Helpers::NumberHelper
def self.exit_on_failure?
true
end
option :days, type: :numeric, default: 90
option :batch_size, type: :numeric, default: 1_000, aliases: [:b], desc: 'Number of records in each batch'
option :continue, type: :boolean, default: false, desc: 'If remove is not completed, execute from the previous continuation'

View File

@ -1,17 +1,9 @@
# frozen_string_literal: true
require_relative '../../../config/boot'
require_relative '../../../config/environment'
require_relative 'helper'
require_relative 'base'
module Mastodon::CLI
class Upgrade < Thor
include Helper
def self.exit_on_failure?
true
end
class Upgrade < Base
CURRENT_STORAGE_SCHEMA_VERSION = 1
option :dry_run, type: :boolean, default: false

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
require 'mastodon/cli/accounts'
describe Mastodon::CLI::Accounts do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
require 'mastodon/cli/cache'
describe Mastodon::CLI::Cache do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
require 'mastodon/cli/canonical_email_blocks'
describe Mastodon::CLI::CanonicalEmailBlocks do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
require 'mastodon/cli/domains'
describe Mastodon::CLI::Domains do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
require 'mastodon/cli/email_domain_blocks'
describe Mastodon::CLI::EmailDomainBlocks do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
require 'mastodon/cli/emoji'
describe Mastodon::CLI::Emoji do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
require 'mastodon/cli/feeds'
describe Mastodon::CLI::Feeds do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
end

View File

@ -3,9 +3,15 @@
require 'rails_helper'
require 'mastodon/cli/ip_blocks'
RSpec.describe Mastodon::CLI::IpBlocks do
describe Mastodon::CLI::IpBlocks do
let(:cli) { described_class.new }
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
describe '#add' do
let(:ip_list) do
[

View File

@ -4,6 +4,12 @@ require 'rails_helper'
require 'mastodon/cli/main'
describe Mastodon::CLI::Main do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
describe 'version' do
it 'returns the Mastodon version' do
expect { described_class.new.invoke(:version) }.to output(

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
require 'mastodon/cli/maintenance'
describe Mastodon::CLI::Maintenance do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
require 'mastodon/cli/media'
describe Mastodon::CLI::Media do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
require 'mastodon/cli/preview_cards'
describe Mastodon::CLI::PreviewCards do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
require 'mastodon/cli/search'
describe Mastodon::CLI::Search do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
end

View File

@ -3,7 +3,13 @@
require 'rails_helper'
require 'mastodon/cli/settings'
RSpec.describe Mastodon::CLI::Settings do
describe Mastodon::CLI::Settings do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
describe 'subcommand "registrations"' do
let(:cli) { Mastodon::CLI::Registrations.new }

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
require 'mastodon/cli/statuses'
describe Mastodon::CLI::Statuses do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
end

View File

@ -0,0 +1,12 @@
# frozen_string_literal: true
require 'rails_helper'
require 'mastodon/cli/upgrade'
describe Mastodon::CLI::Upgrade do
describe '.exit_on_failure?' do
it 'returns true' do
expect(described_class.exit_on_failure?).to be true
end
end
end