Class: XmpToolkitRuby::CLI
- Inherits:
-
Thor
- Object
- Thor
- XmpToolkitRuby::CLI
- Defined in:
- lib/xmp_toolkit_ruby/cli.rb
Overview
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
Class Method Summary collapse
-
.exit_on_failure? ⇒ Boolean
Ensures that the CLI exits with a non-zero status code on failure.
Instance Method Summary collapse
- #override_xmp(file_path, xml_file_path) ⇒ Object
- #print_xmp(file_path, output_file_path = nil) ⇒ Object
- #version ⇒ Object
Class Method Details
.exit_on_failure? ⇒ Boolean
Ensures that the CLI exits with a non-zero status code on failure.
9 10 11 |
# File 'lib/xmp_toolkit_ruby/cli.rb', line 9 def self.exit_on_failure? true end |
Instance Method Details
#override_xmp(file_path, xml_file_path) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/xmp_toolkit_ruby/cli.rb', line 58 def override_xmp(file_path, xml_file_path) raise Thor::Error, "XML file not found: #{xml_file_path}" unless File.exist?(xml_file_path) raise Thor::Error, "XML file is not readable: #{xml_file_path}" unless File.readable?(xml_file_path) xml_content = File.read(xml_file_path) # Validate XML doc = Nokogiri::XML(xml_content) do |config| config.strict.nonet # More secure parsing options end unless doc.errors.empty? = doc.errors.map(&:message).join("\n - ") raise Thor::Error, "Invalid XML in #{xml_file_path}:\n - #{}" end # The XmpToolkitRuby.xmp_to_file method itself will call check_file! for file_path XmpToolkitRuby.xmp_to_file(file_path, xml_content, override: [:override]) mode = [:override] ? "overrode" : "merged" puts "Successfully #{mode} XMP metadata in #{file_path} with content from #{xml_file_path}." rescue XmpToolkitRuby::FileNotFoundError => e raise Thor::Error, "Error: #{e.}" rescue Thor::Error # Re-raise Thor errors directly raise rescue StandardError => e raise Thor::Error, "An unexpected error occurred: #{e.}\n#{e.backtrace.join("\n")}" end |
#print_xmp(file_path, output_file_path = nil) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/xmp_toolkit_ruby/cli.rb', line 16 def print_xmp(file_path, output_file_path = nil) = XmpToolkitRuby.xmp_from_file(file_path) key_to_print = [:raw] ? "xmp_data_orig" : "xmp_data" if && [key_to_print] xmp_content = [key_to_print] if output_file_path begin File.write(output_file_path, xmp_content) puts "XMP Metadata from #{file_path}#{" (raw)" if [:raw]} written to #{output_file_path}" rescue SystemCallError => e # Catches errors like EACCES, ENOENT etc. raise Thor::Error, "Error writing to output file #{output_file_path}: #{e.}" end else puts "XMP Metadata for: #{file_path}#{" (raw)" if [:raw]}" puts xmp_content end else = "No XMP metadata found (key: #{key_to_print}) or an error occurred for: #{file_path}" raise Thor::Error, if output_file_path puts end rescue XmpToolkitRuby::FileNotFoundError => e raise Thor::Error, "Error: #{e.}" rescue StandardError => e raise Thor::Error, "An unexpected error occurred: #{e.}" end |
#version ⇒ Object
89 90 91 |
# File 'lib/xmp_toolkit_ruby/cli.rb', line 89 def version puts "xmp_toolkit_ruby version #{XmpToolkitRuby::VERSION}" end |