module R10K::Util::Purgeable

Mixin for purging stale directory contents.

@abstract Classes using this mixin need to implement {#managed_directory} and

{#desired_contents}

Public Instance Methods

current_contents() click to toggle source

@return [Array<String>] The present directory entries in `self.managed_directory`

# File lib/r10k/util/purgeable.rb, line 27
def current_contents
  dir = self.managed_directory
  glob_exp = File.join(dir, '*')

  Dir.glob(glob_exp).map do |fname|
    File.basename(fname)
  end
end
pending_contents() click to toggle source

@return [Array<String>] Directory contents that are expected but not present

# File lib/r10k/util/purgeable.rb, line 37
def pending_contents
  desired_contents - current_contents
end
purge!() click to toggle source

Forcibly remove all unmanaged content in `self.managed_directory`

# File lib/r10k/util/purgeable.rb, line 47
def purge!
  if stale_contents.empty?
    logger.debug1 "No unmanaged contents in #{managed_directory}, nothing to purge"
  else
    stale_contents.each do |fname|
      fpath = File.join(self.managed_directory, fname)
      logger.info "Removing unmanaged path #{fpath}"
      FileUtils.rm_rf(fpath, :secure => true)
    end
  end
end
stale_contents() click to toggle source

@return [Array<String>] Directory contents that are present but not expected

# File lib/r10k/util/purgeable.rb, line 42
def stale_contents
  current_contents - desired_contents
end