class Kanrisuru::Remote::Fstab

Public Class Methods

new(host, path = '/etc/fstab') click to toggle source
# File lib/kanrisuru/remote/fstab.rb, line 8
def initialize(host, path = '/etc/fstab')
  @host = host
  @path = path
  @file = nil
  @backup = nil

  init_from_os
end

Public Instance Methods

<<(entry) click to toggle source
# File lib/kanrisuru/remote/fstab.rb, line 36
def <<(entry)
  append(entry)
end
[](device) click to toggle source
# File lib/kanrisuru/remote/fstab.rb, line 17
def [](device)
  get_entry(device)
end
append(entry) click to toggle source
# File lib/kanrisuru/remote/fstab.rb, line 40
def append(entry)
  if entry.instance_of?(Kanrisuru::Remote::Fstab::Entry)
    return if @entries.key?(entry.device)
  elsif entry.instance_of?(String)
    entry = Kanrisuru::Remote::Fstab::Entry.new(host: @host, line: entry)
    return if @entries.key?(entry.device)
  else
    raise ArgumentError, 'Invalid entry type'
  end

  @entries[entry.device] = {
    entry: entry,
    new: true
  }

  nil
end
append_file!() click to toggle source

Only append new entries to file

# File lib/kanrisuru/remote/fstab.rb, line 67
def append_file!
  @file.append do |f|
    @entries.each do |_, entry|
      f << entry[:entry].to_s if entry[:new]
    end
  end

  reload!
end
each(&block) click to toggle source
# File lib/kanrisuru/remote/fstab.rb, line 60
def each(&block)
  @entries.each do |_, entry|
    block.call(entry[:entry])
  end
end
find_device(device) click to toggle source
# File lib/kanrisuru/remote/fstab.rb, line 58
def find_device(device); end
get_entry(device) click to toggle source
# File lib/kanrisuru/remote/fstab.rb, line 21
def get_entry(device)
  result = @entries[device]

  return result[:entry] if result

  result = nil

  ## Lookup by uuid or label
  @entries.each do |_, entry|
    result = entry[:entry] if !@result && (entry[:entry].uuid == device || entry[:entry].label == device)
  end

  result
end
inspect() click to toggle source
# File lib/kanrisuru/remote/fstab.rb, line 101
def inspect
  format('#<Kanrisuru::Remote::Fstab:0x%<object_id>s @path=%<path>s @entries=%<entries>s>',
         object_id: object_id, path: @path, entries: @entries)
end
reload!() click to toggle source
# File lib/kanrisuru/remote/fstab.rb, line 97
def reload!
  init_from_os
end
to_s() click to toggle source
# File lib/kanrisuru/remote/fstab.rb, line 88
def to_s
  lines = []
  @entries.each do |_, entry|
    lines << entry[:entry].to_s
  end

  lines.join("\n")
end
write_file!() click to toggle source

Rewrites entire fstab file with new and old entries

# File lib/kanrisuru/remote/fstab.rb, line 78
def write_file!
  @file.write do |f|
    @entries.each do |_, entry|
      f << entry[:entry].to_s
    end
  end

  reload!
end

Private Instance Methods

init_from_os() click to toggle source
# File lib/kanrisuru/remote/fstab.rb, line 108
def init_from_os
  @entries = {}

  raise 'Not implemented' unless @host.os && @host.os.kernel == 'Linux'

  initialize_linux
end
initialize_linux() click to toggle source
# File lib/kanrisuru/remote/fstab.rb, line 116
def initialize_linux
  if @file
    @file.reload!
  else
    @file = @host.file(@path)
  end

  raise ArgumentError, 'Invalid file' if !@file.exists? || !@file.file?

  @file.each do |line|
    next if line.strip.chomp.empty?
    next if line =~ /\s*#/

    entry = Fstab::Entry.new(host: @host, line: line)
    @entries[entry.device] = {
      entry: entry,
      new: false
    }
  end
end