Module: Prepper::Tools::File
- Included in:
- Package
- Defined in:
- lib/prepper/tools/file.rb
Overview
Helper methods for file and directory management
Instance Method Summary collapse
-
#chown(path, owner, flags = "") ⇒ Object
Changes ownership of a path.
-
#directory(path, opts = {}) ⇒ Object
Create a directory unless it already exists.
-
#file(path, opts = {}) ⇒ Object
Create a file unless it already exists.
-
#has_directory?(path) ⇒ Boolean
returns a verifier command to test if a directory exists.
-
#has_file?(path) ⇒ Boolean
returns a verifier command to test if a file exists.
-
#has_symlink?(link, file = nil) ⇒ Boolean
returns a verifier command to test if symlink exists.
-
#matches_content?(path, content) ⇒ Boolean
returns a verifier command to test if as a matching content.
-
#render_template(template, locals) ⇒ Object
render an ERB template.
-
#symlink(link, target, opts = {}) ⇒ Object
creates a symlink.
Instance Method Details
#chown(path, owner, flags = "") ⇒ Object
Changes ownership of a path
12 13 14 |
# File 'lib/prepper/tools/file.rb', line 12 def chown(path, owner, flags = "") @commands << Command.new("chown #{flags} #{owner} #{path}", sudo: true) end |
#directory(path, opts = {}) ⇒ Object
Create a directory unless it already exists
21 22 23 24 25 |
# File 'lib/prepper/tools/file.rb', line 21 def directory(path, opts = {}) @commands << Command.new("mkdir -p #{path}", opts.merge(sudo: true, verifier: has_directory?(path))) @commands << Command.new("chown #{opts[:owner]} #{path}", sudo: true) if opts[:owner] @commands << Command.new("chmod #{opts[:mode]} #{path}", sudo: true) if opts[:mode] end |
#file(path, opts = {}) ⇒ Object
Create a file unless it already exists. The contents can be set to a string or a template can be rendered with the provided locals
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/prepper/tools/file.rb', line 44 def file(path, opts = {}) opts[:locals] ||= {} opts[:verify_content] ||= true content = opts[:content] || render_template(opts[:template], opts[:locals]) verifier = if opts[:verify_content] matches_content?(path, content) else has_file?(path) end io = StringIO.new(content) @commands << Command.new("put!", {params: [io, path, {owner: opts[:owner], mode: opts[:mode]}], verifier: verifier}) end |
#has_directory?(path) ⇒ Boolean
returns a verifier command to test if a directory exists
29 30 31 |
# File 'lib/prepper/tools/file.rb', line 29 def has_directory?(path) Command.new("test -d #{path}", sudo: true) end |
#has_file?(path) ⇒ Boolean
returns a verifier command to test if a file exists
59 60 61 |
# File 'lib/prepper/tools/file.rb', line 59 def has_file?(path) Command.new("test -f #{path}", sudo: true) end |
#has_symlink?(link, file = nil) ⇒ Boolean
returns a verifier command to test if symlink exists
106 107 108 109 110 111 112 |
# File 'lib/prepper/tools/file.rb', line 106 def has_symlink?(link, file = nil) if file Command.new("'#{file}' = `readlink #{link}`") else Command.new("test -L #{link}", sudo: true) end end |
#matches_content?(path, content) ⇒ Boolean
returns a verifier command to test if as a matching content
66 67 68 69 |
# File 'lib/prepper/tools/file.rb', line 66 def matches_content?(path, content) md5 = Digest::MD5.hexdigest(content) Command.new("md5sum #{path} | cut -f1 -d' '`\" = \"#{md5}\"", sudo: true, verifier: has_file?(path)) end |
#render_template(template, locals) ⇒ Object
render an ERB template
117 118 119 |
# File 'lib/prepper/tools/file.rb', line 117 def render_template(template, locals) ERB.new(::File.read("./templates/#{template}")).result_with_hash(locals) end |