Class: Wright::Provider::File

Inherits:
Wright::Provider show all
Defined in:
lib/wright/provider/file.rb

Overview

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

Constant Summary

Constants inherited from Wright::Provider

PROVIDER_DIR

Instance Method Summary collapse

Methods inherited from Wright::Provider

#env, #exec_or_fail, #initialize, #unless_dry_run, #unless_uptodate, #updated?

Constructor Details

This class inherits a constructor from Wright::Provider

Instance Method Details

#checksum(content) ⇒ Object (private)


72
73
74
# File 'lib/wright/provider/file.rb', line 72

def checksum(content)
  Digest::SHA256.hexdigest(content)
end

#contentObject (private)


50
51
52
# File 'lib/wright/provider/file.rb', line 50

def content
  @resource.content
end

#content_uptodate?Boolean (private)

Returns:

  • (Boolean)

76
77
78
79
80
81
82
# File 'lib/wright/provider/file.rb', line 76

def content_uptodate?
  return false unless ::File.exist?(filename_expanded)
  target_content = content || ''
  target_checksum = checksum(target_content.to_s)
  current_checksum = checksum(::File.read(filename_expanded))
  current_checksum == target_checksum
end

#createvoid

This method returns an undefined value.

Creates or updates the file.

Raises:

  • (Errno::EISDIR)

    if there is already a directory with the specified name


19
20
21
22
23
24
25
26
27
28
# 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

#fail_if_directoryObject (private)


98
99
100
101
# File 'lib/wright/provider/file.rb', line 98

def fail_if_directory
  return unless ::File.directory?(filename_expanded)
  fail Errno::EISDIR, filename_expanded
end

#file_nameObject (private)


46
47
48
# File 'lib/wright/provider/file.rb', line 46

def file_name
  @resource.name
end

#filename_expandedObject (private)


94
95
96
# File 'lib/wright/provider/file.rb', line 94

def filename_expanded
  ::File.expand_path(file_name)
end

#move_tempfile(tempfile) ⇒ Object (private)


66
67
68
69
70
# 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

#permissionsObject (private)


54
55
56
# File 'lib/wright/provider/file.rb', line 54

def permissions
  Wright::Util::FilePermissions.create_from_resource(@resource, :file)
end

#removevoid

This method returns an undefined value.

Removes the file.

Raises:

  • (Errno::EISDIR)

    if there is a directory with the specified name


35
36
37
38
39
40
41
42
# 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

#uptodate?(action) ⇒ Boolean (private)

Returns:

  • (Boolean)

84
85
86
87
88
89
90
91
92
# File 'lib/wright/provider/file.rb', line 84

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_fileObject (private)


58
59
60
61
62
63
64
# 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