Class: SmallCage::Commands::Clean

Inherits:
Object
  • Object
show all
Defined in:
lib/smallcage/commands/clean.rb

Overview

‘smc clean’ command

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ Clean

Returns a new instance of Clean.



10
11
12
# File 'lib/smallcage/commands/clean.rb', line 10

def initialize(opts)
  @opts = opts
end

Class Method Details

.execute(opts) ⇒ Object



6
7
8
# File 'lib/smallcage/commands/clean.rb', line 6

def self.execute(opts)
  new(opts).execute
end

Instance Method Details

#executeObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/smallcage/commands/clean.rb', line 14

def execute
  start = Time.now
  count = 0

  target = Pathname.new(@opts[:path])
  fail 'target directory or file does not exist.: ' + target.to_s unless target.exist?

  loader = SmallCage::Loader.new(target)
  root = loader.root
  list = SmallCage::UpdateList.create(root, target)
  uris = list.expire
  uris.each do |uri|
    file = root + uri[1..-1]
    if file.exist?
      puts "D #{uri}" unless @opts[:quiet]
      file.delete
      count += 1
    end
  end

  tmpdir = root + '_smc/tmp'
  if tmpdir.exist?
    FileUtils.rm_r(tmpdir)
    puts 'D /_smc/tmp' unless @opts[:quiet]
    count += 1
  end

  elapsed  = Time.now - start
  puts "-- #{ count } files.  #{ sprintf('%.3f', elapsed) } sec." +
    "  #{ sprintf('%.3f', count == 0 ? 0 : elapsed / count) } sec/file." unless @opts[:quiet]
end