class Confinicky::ShellFile
A model that loads and represents a shell file.
Attributes
aliases[RW]
The alias commands stored in the shell file.
exports[RW]
A list of all exports in the shell file.
file_path[R]
Returns the file path for the current instance of the shell file class.
lines[R]
The preserved lines of code from the shell file which confinicky will write back to the new shell file in the order they were received.
Public Class Methods
new(file_path: "")
click to toggle source
Parses the configuration file if it exists.
# File lib/confinicky/shell_file.rb, line 28 def initialize(file_path: "") raise "Config file not found. Please set" if !File.exists?(@file_path = file_path) @exports = [] @aliases = [] @lines = [] file = File.new(@file_path, "r") command = nil while (line = file.gets) if !command.nil? && command.open? command.append line else command = Confinicky::Parsers::Command.new(line: line) end @lines << line if command.line? @exports << command.values_array if command.export? and command.closed? @aliases << command.values_array if command.alias? and command.closed? end file.close() end
Public Instance Methods
backup!()
click to toggle source
Copies the current shell file to a temporary location.
# File lib/confinicky/shell_file.rb, line 60 def backup! backup_name = @file_path+Time.now.getutc.to_i.to_s+".bak.tmp" FileUtils.cp(@file_path, backup_name) backup_name end
write!()
click to toggle source
Writes a new version of the configuration file.
# File lib/confinicky/shell_file.rb, line 68 def write! File.open(@file_path, "w") do |f| for line in @lines f.write line end f.puts @exports.map{|e| "export #{e.join("=")}"}.join("\n") if !@exports.nil? and @exports.length > 0 f.puts @aliases.map{|a| "alias #{a.join("=")}"}.join("\n") if !@aliases.nil? and @aliases.length > 0 end true end