Class: Wright::Provider::Symlink

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

Overview

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

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

#createvoid

This method returns an undefined value.

Creates or updates the symlink.


12
13
14
15
16
17
18
19
20
21
# File 'lib/wright/provider/symlink.rb', line 12

def create
  fail Errno::EEXIST, link_name_expanded if regular_file?

  symlink = symlink_to_s
  unless_uptodate(:create, "symlink already created: #{symlink}") do
    unless_dry_run("create symlink: #{symlink}") do
      Wright::Util::File.ln_sfn(link_to_expanded, link_name_expanded)
    end
  end
end

38
39
40
# File 'lib/wright/provider/symlink.rb', line 38

def link_name
  @resource.name
end

51
52
53
# File 'lib/wright/provider/symlink.rb', line 51

def link_name_expanded
  Wright::Util::File.expand_tilde_path(link_name)
end

42
43
44
# File 'lib/wright/provider/symlink.rb', line 42

def link_to
  @resource.to
end

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

def link_to_expanded
  return nil if link_to.nil?
  Wright::Util::File.expand_tilde_path(link_to)
end

#regular_file?Boolean (private)


69
70
71
72
# File 'lib/wright/provider/symlink.rb', line 69

def regular_file?
  ::File.exist?(link_name_expanded) &&
    !::File.symlink?(link_name_expanded)
end

#removevoid

This method returns an undefined value.

Removes the symlink.


26
27
28
29
30
31
32
33
34
# File 'lib/wright/provider/symlink.rb', line 26

def remove
  fail "'#{link_name_expanded}' is not a symlink" if regular_file?

  unless_uptodate(:remove, "symlink already removed: '#{link_name}'") do
    unless_dry_run("remove symlink: '#{link_name}'") do
      FileUtils.rm(link_name_expanded)
    end
  end
end

55
56
57
# File 'lib/wright/provider/symlink.rb', line 55

def symlink_to_s
  "'#{link_name}' -> '#{link_to}'"
end

#uptodate?(action) ⇒ Boolean (private)


59
60
61
62
63
64
65
66
67
# File 'lib/wright/provider/symlink.rb', line 59

def uptodate?(action)
  case action
  when :create
    ::File.symlink?(link_name_expanded) &&
      ::File.readlink(link_name_expanded) == link_to_expanded
  when :remove
    !::File.symlink?(link_name_expanded)
  end
end