class Balmora::Command::Link

Public Instance Methods

_equals?(file1, file2) click to toggle source
# 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) click to toggle source
# File lib/balmora/command/link.rb, line 35
def _exists?(file)
  status, _ = @shell.run(['test', '-e', file], verbose: false)
  return status == 0
end
init() click to toggle source
Calls superclass method Balmora::Command#init
# File lib/balmora/command/link.rb, line 5
def init()
  super()

  @link = @variables.inject(@link)
  @source = @variables.inject(@source)
  @storage = @variables.inject(@storage)
end
options() click to toggle source
Calls superclass method Balmora::Command#options
# File lib/balmora/command/link.rb, line 13
def options()
  return super().concat([:link, :source, :storage])
end
run() click to toggle source
# 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
verify() click to toggle source
# 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

Protected Instance Methods

_create_target_path() click to toggle source
# File lib/balmora/command/link.rb, line 91
def _create_target_path()
  @shell.run!(
    ['mkdir', '-p', ::File.dirname(@shell.expand(@link))],
    verbose: false
  )
end
_expr(string) click to toggle source
# File lib/balmora/command/link.rb, line 87
def _expr(string)
  return @shell.expression(string)
end
_source() click to toggle source
# File lib/balmora/command/link.rb, line 98
def _source()
  return Balmora::Command::File.resolve_path(@shell, @source, @storage, @link)
end
_target() click to toggle source
# File lib/balmora/command/link.rb, line 102
def _target()
  return @shell.expand(@link)
end