Class: SLA::CommandLine

Inherits:
SuperDocopt::Base
  • Object
show all
Includes:
Colsole
Defined in:
lib/sla/command_line.rb

Instance Method Summary collapse

Instance Method Details

#before_executeObject



9
10
11
12
# File 'lib/sla/command_line.rb', line 9

def before_execute
  WebCache.life = args['--cache']
  WebCache.dir  = args['--cache-dir'] if args['--cache-dir']
end

#checkObject



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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sla/command_line.rb', line 14

def check
  checker = Checker.new
  checker.max_depth = args['--depth'].to_i
  logfile = args['--log']
  start_url = args['DOMAIN']
  ignore = args['--ignore']
  ignore = ignore.split " " if ignore
  screen_width = terminal_width
  
  checker.check_external = args['--external']
  checker.ignore = ignore if ignore

  start_url = "http://#{start_url}" unless start_url[0..3] == 'http'

  File.unlink logfile if logfile and File.exist? logfile

  count = 1
  failed = 0

  log = []

  checker.check start_url do |link|
    status = link.status
    colored_status = color_status status
    if status != '200'
      failed +=1 
      resay "#{colored_status} #{link.ident}"
      log.push "#{status}  #{link.ident}" if logfile
    end

    message = "[#{failed}/#{count} @ #{link.depth}] #{status}"
    remaining_width = screen_width - message.size - 4
    trimmed_link = link.ident[0..remaining_width]
    
    resay "[#{failed}/#{count} @ #{link.depth}] #{colored_status} #{trimmed_link} "
    count += 1

    sleep ENV['SLA_SLEEP'].to_f if ENV['SLA_SLEEP']
  end

  color = failed > 0 ? '!txtred!' : '!txtgrn!'
  resay "#{color}Done checking #{count} links with #{failed} failures"

  if logfile      
    logstring = log.join("\n") + "\n"
    File.write logfile, logstring
  end

  if failed > 0 and !ENV['SLA_ALLOW_FAILS']
    raise BrokenLinks 
  end
end