Class: Balmora::Command::Link

Inherits:
Balmora::Command show all
Defined in:
lib/balmora/command/link.rb

Defined Under Namespace

Classes: Error

Instance Method Summary collapse

Methods inherited from Balmora::Command

#_execute, #execute, #initialize, #option

Constructor Details

This class inherits a constructor from Balmora::Command

Instance Method Details

#_equals?(file1, file2) ⇒ Boolean

Returns:

  • (Boolean)


73
74
75
76
77
78
79
80
81
82
83
# File 'lib/balmora/command/link.rb', line 73

def _equals?(file1, file2)
  command = [
    'test', '-e', file1, _expr('&&'),
    *@shell.sudo(), 'test', '-e', file2, _expr('&&'),
    _expr('[ "$('), *@shell.sudo(), 'cat', file1, _expr('| md5sum'),
      _expr(')" = '),
    _expr('"$('), *@shell.sudo(), 'cat', file2, _expr('| md5sum)" ]')
  ]

  return @shell.run(command, verbose: false)[0] == 0
end

#_exists?(file) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
38
# File 'lib/balmora/command/link.rb', line 35

def _exists?(file)
  status, _ = @shell.run(['test', '-e', file], verbose: false)
  return status == 0
end

Returns:

  • (Boolean)


40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/balmora/command/link.rb', line 40

def _link_exists?(d = false)
  stat_status, stat_result = @shell.run(['stat', _target()],
    verbose: false)

  if stat_status != 0
    return false
  end

  if d
    p([_target, stat_status, stat_result])
  end

  if !stat_result.include?(_source())
    return false
  end

  return true
end

#initObject



5
6
7
8
9
10
11
# File 'lib/balmora/command/link.rb', line 5

def init()
  super()

  @link = @variables.inject(@link)
  @source = @variables.inject(@source)
  @storage = @variables.inject(@storage)
end

#optionsObject



13
14
15
# File 'lib/balmora/command/link.rb', line 13

def options()
  return super().concat([:link, :source, :storage])
end

#runObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/balmora/command/link.rb', line 17

def run()
  if _exists?(_target())
    if _link_exists?()
      return
    end

    if !_exists?(_source())
      @shell.run!(['mkdir', '-p', ::File.dirname(_source())], change: true)
      @shell.run!(['mv', _target(), _source()], change: true)
    elsif _equals?(_source(), _target())
      @shell.run!(['rm', '-rf', _target()], change: true)
    end
  end

  _create_target_path()
  @shell.run!(['ln', @options || '-s', _source(), _target()], change: true)
end

#verifyObject



59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/balmora/command/link.rb', line 59

def verify()
  if @link.nil?()
    raise Error.new('"link" should be defined')
  end

  if !@storage.nil?() && !@source.nil?()
    raise Error.new('"storage" and "source" could not be defined together')
  end

  if @storage.nil?() && @source.nil?()
    raise Error.new('"storage" or "source" should be defined')
  end
end