Class: Chainsaw::CLI

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

Constant Summary collapse

CHRONIC_OPTIONS =
{ :context => :past, :guess => false }
<<-BANNER
Usage: chainsaw LOGFILE INTERVAL [OPTIONS]

Description:
  Parses a log file and returns lines matching the time period provided.

  Chronic is used to parse the time strings, so any format chronic
  supports, chainsaw supports. A list of supported formats can
  be found here: https://github.com/mojombo/chronic

Examples:

  > chainsaw access.log 1 hour ago                  # entries from one hour ago to now
  > chainsaw access.log august                      # entries from August to now
  > chainsaw access.log 2012-08-06                  # entries from August 6th to now
  > chainsaw access.log 2012-08-27 10:00            # entries from August 27th at 10:00 to now

  You can use a hypen to specify a time range (you can mix and match formats)

  > chainsaw access.log 2012-08-01 - 2012-09-17     # entries within August 1st and September 17th
  > chainsaw access.log august - yesterday          # entries within August and September

BANNER

Class Method Summary collapse

Class Method Details

.parse_optionsObject

Use OptionParser to parse options, then we remove them from ARGV to help ensure we’re parsing times correctly



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
# File 'lib/chainsaw/cli.rb', line 30

def self.parse_options
  @options = OpenStruct.new({
    :interactive => false,
    :colorize    => false
  })

  @opts = OptionParser.new do |opts|
    opts.banner = BANNER.gsub(/^ {4}/, '')

    opts.separator ''
    opts.separator 'Options:'

    opts.on('-f [FILTER]', 'Provide a regexp pattern to match on as well as the interval given') do |filter|
      @options.filter = filter
    end

    opts.on('-i', 'Work in interactive mode, one line at a time') do
      @options.interactive = true
    end

    opts.on('-c', 'Colorize output (dates and patterns given)') do
      @options.colorize = true
    end

    opts.on('-o [FILE]', 'Output the filtered lines to a file') do |file|
      @options.output_file = file
    end

    opts.on('-v', 'Print the version') do
      puts Chainsaw::VERSION
      exit
    end

    opts.on( '-h', '--help', 'Display this help.' ) do
      puts opts
      exit
    end
  end

  @opts.parse!
end

.parse_time_args(args) ⇒ Object

Check the leftover arguments to see if we’re given a range or not. If we have a range, parse them, if not, parse the single time arguments.

args - an Array of String arguments

Returns a Time object or Range representing the requested time



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/chainsaw/cli.rb', line 78

def self.parse_time_args(args)
  delimiter = args.index('-')

  if delimiter
    starting = Chronic.parse(args[0..(delimiter - 1)].join(' '), CHRONIC_OPTIONS).begin
    ending   = Chronic.parse(args[(delimiter + 1)..-1].join(' '), CHRONIC_OPTIONS).begin
  else
    starting = Chronic.parse(args.join(' '), CHRONIC_OPTIONS).begin
    ending   = Time.now
  end

  starting..ending
rescue
  puts "\033[31mUnable to parse `#{args.join(' ')}'. Check \033[0m\033[4mhttps://github.com/mojombo/chronic\033[0m\033[31m to see time formats that chronic supports.\033[0m"
  exit
end


95
96
97
98
# File 'lib/chainsaw/cli.rb', line 95

def self.print_usage_and_exit!
  puts @opts
  exit
end

.runObject

Called from the executable. Parses the command line arguments and passes them through to Filter.



102
103
104
105
106
107
108
109
110
111
# File 'lib/chainsaw/cli.rb', line 102

def self.run
  parse_options
  print_usage_and_exit! if ARGV.empty?

  logfile = ARGV.first
  time    = parse_time_args(ARGV[1..-1])

  trap(:INT) { exit }
  Filter.filter(logfile, time, @options)
end