Class: Hank::Validator

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/hank/validator.rb

Overview

Validates the integrity of symlinks

Defined Under Namespace

Classes: Issue

Instance Method Summary collapse

Constructor Details

#initialize(hankfile) ⇒ Validator

Returns a new instance of Validator.



18
19
20
21
# File 'lib/hank/validator.rb', line 18

def initialize(hankfile)
  @hankfile = T.let(hankfile, Hankfile)
  @base_dir = T.let(Pathname.new(Dir.pwd), Pathname)
end

Instance Method Details

#validateObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/hank/validator.rb', line 24

def validate
  issues = []

  @hankfile.mappings.each do |source_path, target_path|
    source = Pathname.new(source_path)
    target = @base_dir.join(target_path)

    if !source.exist?
      # Source path doesn't exist
      issues << Issue.new(
        type: :missing,
        source_path: source_path,
        target_path: target_path,
        current_target: nil
      )
    elsif !source.symlink?
      # Source path exists but is not a symlink
      issues << Issue.new(
        type: :not_symlink,
        source_path: source_path,
        target_path: target_path,
        current_target: nil
      )
    else
      # Source path is a symlink, check if it points to the correct target
      actual_target = Pathname.new(File.readlink(source.to_s))

      if actual_target != target && actual_target.expand_path != target.expand_path
        issues << Issue.new(
          type: :wrong_target,
          source_path: source_path,
          target_path: target_path,
          current_target: actual_target.to_s
        )
      end
    end
  end

  issues
end