class Wright::Provider::File

File provider. Used as a provider for {Resource::File}.

Public Instance Methods

create() click to toggle source

Creates or updates the file.

@return [void] @raise [Errno::EISDIR] if there already is a directory with

the specified name
# File lib/wright/provider/file.rb, line 19
def create
  fail_if_directory
  file_permissions = permissions
  unless_uptodate(:create, "file already created: '#{file_name}'") do
    unless_dry_run("create file: '#{file_name}'") do
      write_content_to_file
      file_permissions.update unless file_permissions.uptodate?
    end
  end
end
remove() click to toggle source

Removes the file.

@return [void] @raise [Errno::EISDIR] if there is a directory with the

specified name
# File lib/wright/provider/file.rb, line 35
def remove
  fail_if_directory
  unless_uptodate(:remove, "file already removed: '#{file_name}'") do
    unless_dry_run("remove file: '#{file_name}'") do
      FileUtils.rm(filename_expanded)
    end
  end
end

Private Instance Methods

checksum(content) click to toggle source
# File lib/wright/provider/file.rb, line 72
def checksum(content)
  Digest::SHA256.hexdigest(content)
end
content() click to toggle source
# File lib/wright/provider/file.rb, line 50
def content
  resource.content
end
content_uptodate?() click to toggle source
# File lib/wright/provider/file.rb, line 76
def content_uptodate?
  return false unless ::File.exist?(filename_expanded)
  return true unless content
  target_content = content || ''
  target_checksum = checksum(target_content.to_s)
  current_checksum = checksum(::File.read(filename_expanded))
  current_checksum == target_checksum
end
fail_if_directory() click to toggle source
# File lib/wright/provider/file.rb, line 99
def fail_if_directory
  return unless ::File.directory?(filename_expanded)
  fail Errno::EISDIR, filename_expanded
end
file_name() click to toggle source
# File lib/wright/provider/file.rb, line 46
def file_name
  resource.name
end
filename_expanded() click to toggle source
# File lib/wright/provider/file.rb, line 95
def filename_expanded
  ::File.expand_path(file_name)
end
move_tempfile(tempfile) click to toggle source
# File lib/wright/provider/file.rb, line 66
def move_tempfile(tempfile)
  # do not overwrite existing files if content was not specified
  return if content.nil? && ::File.exist?(filename_expanded)
  FileUtils.mv(tempfile.path, filename_expanded)
end
permissions() click to toggle source
# File lib/wright/provider/file.rb, line 54
def permissions
  Wright::Util::FilePermissions.create_from_resource(resource, :file)
end
uptodate?(action) click to toggle source
# File lib/wright/provider/file.rb, line 85
def uptodate?(action)
  case action
  when :create
    content_uptodate? && permissions.uptodate?
  when :remove
    !::File.exist?(filename_expanded) &&
      !::File.symlink?(filename_expanded)
  end
end
write_content_to_file() click to toggle source
# File lib/wright/provider/file.rb, line 58
def write_content_to_file
  tempfile = Tempfile.new(::File.basename(filename_expanded))
  tempfile.write(content) if content
  move_tempfile(tempfile)
ensure
  tempfile.close!
end