Class: MDQT::CLI::Rename

Inherits:
Base
  • Object
show all
Defined in:
lib/mdqt/cli/rename.rb

Instance Method Summary collapse

Methods inherited from Base

#advise_on_xml_signing_support, #args, #btw, check_requirements, #colour_shell?, #explain, #extract_certificate_paths, #get_stdin, #halt!, #hey, #initialize, introduce, #options, #options=, #output, #pastel, #pipeable?, run, #say, #service_url, service_url, #yay

Constructor Details

This class inherits a constructor from MDQT::CLI::Base

Instance Method Details

#runObject



9
10
11
12
13
14
15
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mdqt/cli/rename.rb', line 9

def run

  options.validate = true

  advise_on_xml_signing_support
  halt!("Cannot check a metadata file without XML support: please install additional gems") unless MDQT::Client.verification_available?

  client = MDQT::Client.new(
    options.service,
    verbose: options.verbose,
    explain: options.explain ? true : false,
  )

  halt!("Please specify a file to rename!") if args.empty?

  args.each do |filename|

    next if File.symlink?(filename)

    file = client.(filename)

    halt!("Cannot access file #{filename}") unless file.readable?
    halt!("File #{filename} is a metadata aggregate, cannot rename to hashed entityID!") if file.aggregate?
    halt!("XML validation failed for #{filename}:\n#{file.validation_error}") unless file.valid?

    halt!("Cannot find entityID for #{filename}") unless file.entity_id

    newname = file.linkname # Using the same name as the link, not super-obvious
    next if filename == newname

    if file.turd?
      hey "Warning: will not process backup/turd files"
      next
    end

    message = ""

    if File.exist?(newname)
      if options.force
        File.delete(newname)
      else
        halt!("Cannot rename #{filename} to #{newname} - File exists! Use --force to override")
        next
      end
    end

    File.rename(filename, newname)

    if options.link
      File.delete(filename) if options.force && File.exist?(filename)
      File.symlink(newname, filename) unless newname == filename
    end

    hey("#{filename} renamed to #{newname} [#{file.entity_id}] #{message}") if options.verbose
  end

end