Class: XmpToolkitRuby::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/xmp_toolkit_ruby/cli.rb

Overview

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Ensures that the CLI exits with a non-zero status code on failure.

Returns:

  • (Boolean)


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?
    error_messages = doc.errors.map(&:message).join("\n - ")
    raise Thor::Error, "Invalid XML in #{xml_file_path}:\n - #{error_messages}"
  end

  # The XmpToolkitRuby.xmp_to_file method itself will call check_file! for file_path
  XmpToolkitRuby.xmp_to_file(file_path, xml_content, override: options[:override])

  mode = options[: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.message}"
rescue Thor::Error # Re-raise Thor errors directly
  raise
rescue StandardError => e
  raise Thor::Error, "An unexpected error occurred: #{e.message}\n#{e.backtrace.join("\n")}"
end


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 = options[: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 options[: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.message}"
      end
    else
      puts "XMP Metadata for: #{file_path}#{" (raw)" if options[:raw]}"
      puts xmp_content
    end
  else
    message = "No XMP metadata found (key: #{key_to_print}) or an error occurred for: #{file_path}"
    raise Thor::Error, message if output_file_path

    puts message

  end
rescue XmpToolkitRuby::FileNotFoundError => e
  raise Thor::Error, "Error: #{e.message}"
rescue StandardError => e
  raise Thor::Error, "An unexpected error occurred: #{e.message}"
end

#versionObject



89
90
91
# File 'lib/xmp_toolkit_ruby/cli.rb', line 89

def version
  puts "xmp_toolkit_ruby version #{XmpToolkitRuby::VERSION}"
end