class Cfgd::Files
provides some basic file operations
Public Class Methods
create(path) { || ... }
click to toggle source
self.create parent method for file creation
# File lib/cfgd/files.rb, line 15 def self.create(path) args = yield if block_given? raise ArgumentError, 'No arguments given' until block_given? # use path from args if given path = args[:path] until path.eql? args[:path] if Enum.new('template').check(args[:type]) template = args[:source] type = args[:type] debug "#{name}.#{__method__} => #{path}" begin send(type, template, path, args) true rescue StandardError false end else raise ArgumentError, 'Give a valid type' until block_given? end end
download(path) { || ... }
click to toggle source
self.download download a file via http/https
# File lib/cfgd/files.rb, line 39 def self.download(path) args = yield if block_given? raise ArgumentError, 'No arguments given' until block_given? source = args[:source] path = args[:path] until path.eql? args[:path] debug "#{name}.#{__method__} => #{path}" begin tempfile = URI.parse(source).open tempfile.close FileUtils.mv tempfile.path, path true rescue StandardError false end end
link(destination) { || ... }
click to toggle source
self.link parent method for file linking
# File lib/cfgd/files.rb, line 76 def self.link(destination) args = yield if block_given? raise ArgumentError, 'No arguments given' until block_given? exit 1 until defined?(args) destination = args[:destination] until destination.eql? args[:destination] if Enum.new('soft', 'hard').check(args[:type]) type = args[:type] source = args[:source] debug "#{name}.#{__method__} => #{source} to #{destination}" begin send(type, source, destination) true rescue StandardError false end else raise ArgumentError, 'Give a valid type' until block_given? end end
remove(path) { || ... }
click to toggle source
self.remove parent method for file deletion
# File lib/cfgd/files.rb, line 58 def self.remove(path) if block_given? args = yield path = args[:path] until path.eql? args[:path] end debug "#{name}.#{__method__} => #{path}" begin File.delete(path) if File.exist?(path) true rescue StandardError false # raise FileNotFound, path end end
Private Class Methods
hard(source, destination)
click to toggle source
# File lib/cfgd/files.rb, line 96 def self.hard(source, destination) FileUtils.ln(source, destination) end
soft(source, destination)
click to toggle source
# File lib/cfgd/files.rb, line 100 def self.soft(source, destination) FileUtils.ln_s(source, destination) end
template(template, path, args)
click to toggle source
# File lib/cfgd/files.rb, line 104 def self.template(template, path, args) if File.file?(template) File.open(path, 'w') do |fo| begin fo.puts ERB.new(File.read(template)).result(binding) true rescue StandardError false end end else warn "File not found: #{template}" false end end