Module: Machines::FileOperations
- Defined in:
- lib/machines/file_operations.rb
Instance Method Summary collapse
-
#append(text, options) ⇒ Object
Add a line of text to the end of a file unless it already exists.
-
#chmod(mode, path) ⇒ Object
Change permissions of a path.
-
#chown(user, path, options = {}) ⇒ Object
Change ownership of a path.
-
#copy(from, to) ⇒ Object
Copy a remote file or folder (will overwrite).
-
#create_from(erb_path, options) ⇒ Object
Write a file from an ERB template.
-
#link(target, link_name) ⇒ Object
Add a symlink.
-
#mkdir(*dirs) ⇒ Object
Create a path or paths on the remote host (Uses -p to be safe and create full path).
-
#remove(file) ⇒ Object
Remove a remote file or folder.
-
#remove_version_info(name) ⇒ Object
Take off the version numbers from a path name.
-
#rename(oldname, newname) ⇒ Object
Rename a remote file or folder.
-
#replace(regex, options) ⇒ Object
Replace some text in a file.
-
#write(text, options) ⇒ Object
(Over)write a file with the specified content.
Instance Method Details
#append(text, options) ⇒ Object
Add a line of text to the end of a file unless it already exists
7 8 9 10 |
# File 'lib/machines/file_operations.rb', line 7 def append text, text = text.gsub(/([\\$"`])/, '\\\\\1') Command.new("grep \"#{text}\" #{[:to]} || echo \"#{text}\" >> #{[:to]}", check_string(text, [:to])) end |
#chmod(mode, path) ⇒ Object
Change permissions of a path
15 16 17 |
# File 'lib/machines/file_operations.rb', line 15 def chmod mode, path Command.new("chmod #{mode} #{path}", check_perms(mode, path)) end |
#chown(user, path, options = {}) ⇒ Object
Change ownership of a path
24 25 26 27 28 |
# File 'lib/machines/file_operations.rb', line 24 def chown user, path, = {} recursive = '-R ' if [:recursive] user = "#{user}:#{user}" unless user.index(':') Command.new("chown #{recursive}#{user} #{path}", check_owner(user, path)) end |
#copy(from, to) ⇒ Object
Copy a remote file or folder (will overwrite)
33 34 35 |
# File 'lib/machines/file_operations.rb', line 33 def copy from, to Command.new("cp -rf #{from} #{to}", check_file(to)) end |
#create_from(erb_path, options) ⇒ Object
Write a file from an ERB template
42 43 44 45 46 47 |
# File 'lib/machines/file_operations.rb', line 42 def create_from erb_path, erb = ERB.new(File.read(erb_path), nil, '<>') binding = [:settings] ? [:settings].get_binding : nil [:name] = erb_path write erb.result(binding), end |
#link(target, link_name) ⇒ Object
Add a symlink
52 53 54 |
# File 'lib/machines/file_operations.rb', line 52 def link target, link_name Command.new("ln -sf #{target} #{link_name}", check_link(link_name)) end |
#mkdir(*dirs) ⇒ Object
Create a path or paths on the remote host (Uses -p to be safe and create full path)
58 59 60 61 62 |
# File 'lib/machines/file_operations.rb', line 58 def mkdir *dirs dirs.flatten.map do |dir| Command.new("mkdir -p #{dir}", check_dir(dir)) end end |
#remove(file) ⇒ Object
Remove a remote file or folder
73 74 75 |
# File 'lib/machines/file_operations.rb', line 73 def remove file Command.new("rm -rf #{file}", check_file(file, false)) end |
#remove_version_info(name) ⇒ Object
Take off the version numbers from a path name
79 80 81 |
# File 'lib/machines/file_operations.rb', line 79 def remove_version_info name Command.new("find . -maxdepth 1 -name \"#{name}*\" -a -type d | xargs -I xxx mv xxx #{name}", check_file(name)) end |
#rename(oldname, newname) ⇒ Object
Rename a remote file or folder
67 68 69 |
# File 'lib/machines/file_operations.rb', line 67 def rename oldname, newname Command.new("mv -f #{oldname} #{newname}", check_file(newname)) end |
#replace(regex, options) ⇒ Object
Replace some text in a file
88 89 90 91 92 |
# File 'lib/machines/file_operations.rb', line 88 def replace regex, , [:with, :in] with = [:with].gsub(/([\n\/\\$"`])/, '\\\\\1') Command.new("sed -i \"s/#{regex}/#{with}/\" #{[:in]}", check_string(with, [:in])) end |
#write(text, options) ⇒ Object
(Over)write a file with the specified content
99 100 101 |
# File 'lib/machines/file_operations.rb', line 99 def write text, Upload.new(NamedBuffer.new([:name], text), [:to], check_string(text, [:to])) end |