Class: Britebox::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/britebox/cli.rb

Constant Summary collapse

MONITOR_EXTENSIONS =
['csv', 'txt']

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



8
9
10
# File 'lib/britebox/cli.rb', line 8

def initialize

end

Instance Method Details

#watchObject



12
13
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/britebox/cli.rb', line 12

def watch
  # Start WebUI
  if Britebox::Config.ui_enabled
    with_sanitized_stderr do
      Thread.new do
        logger = Logger.new(STDOUT)
        logger.level = Logger::ERROR
        Britebox::WebUI.run!(port: Britebox::Config.port, server_settings: {Logger: logger, AccessLog: []})
        exit!
      end
    end
    puts "Britebox web interface up and running, open http://localhost:#{Britebox::Config.port}/ in browser to see it"
    puts
  end


  @api_key = Config.api_key || raise("Please provide BriteVerify API Key")

  if Config.watch_dir
    @dir = File.expand_path Config.watch_dir
  else
    raise("Please provide directory-to-watch")
  end

  raise("Directory #{@dir} does not exist") unless File.exists? @dir

  if Config.out_dir
    @out_dir = File.expand_path Config.out_dir
  else
    @out_dir = File.expand_path 'verified', @dir
  end
  Config.out_dir = @out_dir

  FileUtils.mkdir_p(@out_dir) unless File.exists? @out_dir

  puts "Watching directory #{@dir}"
  puts "Output directory is #{@out_dir}"

  if Britebox::Config.simulate
    warn = "Running in simulation mode. No real requests to API will be made."
    EventLog.add 'WARN', warn
    puts warn
  end

  Thread.new do
    loop do
      sleep 20
      if Config.recheck_enabled
        Dir.glob(File.join(@out_dir, "*" + FileJob::FIRST_PASS + ".*")).each do |f|
          if ((Time.now - Config.recheck_interval * 60 - 60) .. (Time.now - Config.recheck_interval * 60)).cover? File.mtime(f)
            new_name = File.basename(f).sub(FileJob::FIRST_PASS, FileJob::SECOND_PASS)
            File.rename f, File.join(@dir, new_name)
          end
        end
      end
    end
  end

  puts "Press Ctrl-C to quit"
  puts

  $fj_pool = FileJobPool.new

  options = {filter: /\.(#{MONITOR_EXTENSIONS.join('|')})$/, ignore: /\//, polling_fallback_message: false}

  brite_client = BriteAPI::Client.new(@api_key)

  Listen.to(@dir, options) do |modified, added, removed|
    (modified + added).each do |file|
      $fj_pool.process_file!(file, @dir, @out_dir, brite_client)
    end
  end

  # Trigger change for existing files
  MONITOR_EXTENSIONS.each do |ext|
    FileUtils.touch Dir.glob(File.expand_path("*.#{ext}", @dir)).map{ |f| File.expand_path(f, "*.#{ext}")}
  end

  # Update current status in cycle
  loop do
    $fj_pool.print_status
    sleep 0.5
  end
end