Class: Fluke::CLI

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

Class Method Summary collapse

Class Method Details

.execute(stdout, stderr, arguments = []) ⇒ Object



5
6
7
8
9
10
11
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
# File 'lib/fluke/cli.rb', line 5

def self.execute(stdout, stderr, arguments=[])
  p = {
    :stdout => stdout, :stderr => stderr
  }

  parser = OptionParser.new do |opts|
    opts.banner = <<-BANNER.gsub(/^          /,'')
      A simple resource observer designed to detect change over time.

      Usage: #{File.basename($0)} [options]
             # run all watchers

             -OR-

             #{File.basename($0)} [options] arg1..argN
             # args are either files, in which case watcher info is
             # inferred based on file content, or watcher names
      Options are:
    BANNER
    opts.separator ""
    opts.on("-c", "--conf", String,
            "Config file location",
            "Default: #{Fluke::DEFAULT_CONF_PATH}") { |arg| p[:conf_path] = arg || nil }
    opts.on("-v", "--verbose",
            "Be chatty.",
            "Default: false") { |arg| Fluke.verbose = arg }
    opts.on("-h", "--help",
            "Show this help message.") { stderr.puts opts; exit }
    opts.parse!(arguments)
  end

  Fluke.configure! p

  if arguments.size > 0 then
    arguments.uniq.each do |name|
      if File.exists?(name) and !File.directory?(name) then
        path = File.expand_path(name)
        Fluke.class_eval(File.read(path), path)
      else
        Fluke.watch name
      end
    end
  else
    Watcher.all.each do |watcher|
      Fluke.watch watcher
    end
  end

  Fluke.run!
end