Class: MDQT::CLI::Ln

Inherits:
Base
  • Object
show all
Defined in:
lib/mdqt/cli/ln.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
66
67
68
69
70
71
# File 'lib/mdqt/cli/ln.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 link to!") 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 create entityID hashed link!") 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

    linkname = file.linkname

    if filename == linkname
      if options.force
        hey("Warning: Cannot link file to itself, skipping! #{filename}")
        next
      else
        halt!("Cannot link file to itself! #{filename}")
        next
      end
      btw("Cannot link file to itself! #{filename}")
    end

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

    message = ""

    if File.exist?(linkname)
      if options.force
        File.delete(linkname)
      else
        old_target = File.readlink(linkname)
        message = old_target == filename ? "File exists" : "Conflicts with #{filename}"
        halt!("#{linkname} -> #{old_target} [#{file.entity_id}] #{message}. Use --force to override")
        next
      end
    end

    File.symlink(filename, linkname)
    hey("#{linkname} -> #{filename} [#{file.entity_id}] #{message}") if options.verbose
  end

end