Class: Terraspace::Cloud::Upload::Tidy

Inherits:
Base show all
Defined in:
lib/terraspace/cloud/upload/tidy.rb

Instance Method Summary collapse

Methods inherited from Base

#artifacts_path, #initialize, #zip_path

Methods inherited from Base

#cancelled?, #changes?, #clean_cache2_stage, #cloud_upload, #initialize, #record?, #sh, #stage_attrs, #success_status

Methods included from Util::Pretty

#pretty_path, #pretty_time

Methods included from Util::Sure

#sure?

Methods included from Util::Logging

#logger

Methods included from Context

#setup_context

Methods included from Api::Validate

#validate

Methods included from Api::Concern

#api, #cloud_stack_name, #expander, #region

Methods included from Api::Concern::Errors

#error_message, #errors?

Methods inherited from Terraspace::CLI::Base

#initialize

Constructor Details

This class inherits a constructor from Terraspace::Cloud::Upload::Base

Instance Method Details

#always_removalsObject

These directories will be removed regardless of dir level



50
51
52
# File 'lib/terraspace/cloud/upload/tidy.rb', line 50

def always_removals
  %w[.git spec tmp]
end

#cleanupObject



3
4
5
6
7
8
9
# File 'lib/terraspace/cloud/upload/tidy.rb', line 3

def cleanup
  removals.each do |removal|
    removal = removal.sub(%r{^/},'') # remove leading slash
    path = "#{artifacts_path}/#{removal}"
    rm_rf(path)
  end
end

#get_removals(file) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/terraspace/cloud/upload/tidy.rb', line 22

def get_removals(file)
  path = file
  return [] unless File.exist?(path)

  removal = File.read(path).split("\n")
  removal.map {|i| i.strip}.reject {|i| i =~ /^#/ || i.empty?}
end

#removalsObject



11
12
13
14
15
16
17
18
19
20
# File 'lib/terraspace/cloud/upload/tidy.rb', line 11

def removals
  removals = always_removals
  removals += get_removals("#{artifacts_path}/.gitignore")
  removals = removals.reject do |p|
    tskeep.find do |keep|
      p.include?(keep)
    end
  end
  removals.uniq
end

#rm_rf(path) ⇒ Object



42
43
44
45
46
47
# File 'lib/terraspace/cloud/upload/tidy.rb', line 42

def rm_rf(path)
  exists = File.exist?("#{path}/.gitkeep") || File.exist?("#{path}/.keep")
  return if exists

  FileUtils.rm_rf(path)
end

#tskeepObject

We clean out ignored files pretty aggressively. So provide a way for users to keep files from being cleaned out.



32
33
34
35
36
37
38
39
40
# File 'lib/terraspace/cloud/upload/tidy.rb', line 32

def tskeep
  always_keep = %w[]
  path = "#{artifacts_path}/.tskeep"
  return always_keep unless File.exist?(path)

  keep = IO.readlines(path)
  keep = keep.map {|i| i.strip}.reject { |i| i =~ /^#/ || i.empty? }
  (always_keep + keep).uniq
end