From 157850577ffbf2c5d1496fde361d68a3e293a6ed Mon Sep 17 00:00:00 2001 From: Lerk Date: Mon, 27 Apr 2020 22:17:49 +0200 Subject: [PATCH] Add `tootctl emoji export` (#13534) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add emoji export command to cli * fix codeclimate issues * add error when no matching category was found * add other suggestions * exit 1 when no matching category is found * changes according to suggestions * 👀 * RubyNein Y u always autoformat :c --- lib/mastodon/emoji_cli.rb | 44 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/mastodon/emoji_cli.rb b/lib/mastodon/emoji_cli.rb index dbaf120182f..da8fd6a0dc2 100644 --- a/lib/mastodon/emoji_cli.rb +++ b/lib/mastodon/emoji_cli.rb @@ -23,7 +23,7 @@ module Mastodon Existing emoji will be skipped unless the --overwrite option is provided, in which case they will be overwritten. - You can specifiy a --category under which the emojis will be + You can specify a --category under which the emojis will be grouped together. With the --prefix option, a prefix can be added to all @@ -72,6 +72,48 @@ module Mastodon say("Imported #{imported}, skipped #{skipped}, failed to import #{failed}", color(imported, skipped, failed)) end + option :category + option :overwrite, type: :boolean + desc 'export PATH', 'Export emoji to a TAR GZIP archive at PATH' + long_desc <<-LONG_DESC + Exports custom emoji to 'export.tar.gz' at PATH. + + The --category option dumps only the specified category. + If this option is not specified, all emoji will be exported. + + The --overwrite option will overwrite an existing archive. + LONG_DESC + def export(path) + exported = 0 + category = CustomEmojiCategory.find_by(name: options[:category]) + export_file_name = File.join(path, 'export.tar.gz') + + if File.file?(export_file_name) && !options[:overwrite] + say("Archive already exists! Use '--overwrite' to overwrite it!") + exit 1 + end + if category.nil? && options[:category] + say("Unable to find category '#{options[:category]}'!") + exit 1 + end + + File.open(export_file_name, 'wb') do |file| + Zlib::GzipWriter.wrap(file) do |gzip| + Gem::Package::TarWriter.new(gzip) do |tar| + scope = !options[:category] || category.nil? ? CustomEmoji.local : category.emojis + scope.find_each do |emoji| + say("Adding '#{emoji.shortcode}'...") + tar.add_file_simple(emoji.shortcode + File.extname(emoji.image_file_name), 0o644, emoji.image_file_size) do |io| + io.write Paperclip.io_adapters.for(emoji.image).read + exported += 1 + end + end + end + end + end + say("Exported #{exported}") + end + option :remote_only, type: :boolean desc 'purge', 'Remove all custom emoji' long_desc <<-LONG_DESC