lib/linux_admin/fstab.rb in linux_admin-0.5.4 vs lib/linux_admin/fstab.rb in linux_admin-0.5.5

- old
+ new

@@ -11,59 +11,97 @@ attr_accessor :mount_point attr_accessor :fs_type attr_accessor :mount_options attr_accessor :dumpable attr_accessor :fsck_order + attr_accessor :comment def initialize(args = {}) @device = args[:device] @mount_point = args[:mount_point] @fs_type = args[:fs_type] @mount_options = args[:mount_options] - @dumpable = args[:dumpable] - @fsck_order = args[:fsck_order] + @dumpable = args[:dumpable].to_i unless args[:dumpable].nil? + @fsck_order = args[:fsck_order].to_i unless args[:fsck_order].nil? + @comment = args[:comment] end def self.from_line(fstab_line) - columns = fstab_line.chomp.split + columns, comment = fstab_line.split('#') + comment = "##{comment}" unless comment.blank? + columns = columns.chomp.split + FSTabEntry.new :device => columns[0], :mount_point => columns[1], :fs_type => columns[2], :mount_options => columns[3], - :dumpable => columns[4].to_i, - :fsck_order => columns[5].to_i - + :dumpable => columns[4], + :fsck_order => columns[5], + :comment => comment end + + def has_content? + !self.columns.first.nil? + end + + def columns + [self.device, self.mount_point, self.fs_type, + self.mount_options, self.dumpable, self.fsck_order, self.comment] + end + + def column_lengths + self.columns.collect { |c| c ? c.size : 0 } + end + + def formatted_columns(max_lengths) + self.columns.collect. + with_index { |col, i| col.to_s.rjust(max_lengths[i]) }.join(" ") + end end class FSTab < LinuxAdmin include Singleton attr_accessor :entries + attr_accessor :maximum_column_lengths def initialize refresh end def write! content = '' + comment_index = 0 @entries.each do |entry| - content += "#{entry.device} #{entry.mount_point} #{entry.fs_type} #{entry.mount_options} #{entry.dumpable} #{entry.fsck_order}\n" + if entry.has_content? + content << entry.formatted_columns(@maximum_column_lengths) << "\n" + else + content << "#{entry.comment}" + end end + File.write('/etc/fstab', content) self end private def read - File.read('/etc/fstab').lines.find_all {|line| !line.blank? && !line.strip.starts_with?("#")} + File.read('/etc/fstab').lines end def refresh - @entries = - read.collect { |line| - FSTabEntry.from_line line - } + @entries = [] + @maximum_column_lengths = Array.new(7, 0) # # of columns + read.each do |line| + entry = FSTabEntry.from_line(line) + @entries << entry + + lengths = entry.column_lengths + lengths.each_index do |i| + @maximum_column_lengths[i] = + lengths[i] if lengths[i] > @maximum_column_lengths[i] + end + end end end end