tailstrom 0.0.6 → 0.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4fc9291b2a7850cedd5567f7c78a94073695a49b
4
- data.tar.gz: ceb209f84de1c9c1104e0e5df763812eed71b894
3
+ metadata.gz: 6f926cdcaa4991ec047200693a400944e4923e26
4
+ data.tar.gz: 1739fde8a212c22dee1a2c2fb11c5dbbe4d5a67f
5
5
  SHA512:
6
- metadata.gz: e0a0ce1ff435b17b851d51516a0bf0137ed2d866618e855d9a3d35b7f45843ea664d10d123e310ff2a09254175160771244530f6495b4f76c4ee8cb83759f367
7
- data.tar.gz: fda93ef992bebf83c9c2828e4e09e91de049f07549de250b99c8836340a1166eb1978474d72b2d2c6b6b19c266b4aa08aeb2241c7442622a41f2c77d82554402
6
+ metadata.gz: 30e6c199b116df31de4235e51b689879e00ec01f3951eb754589250c534c4034a75ea14250eefda4bd4c790f9e4fef6508144db45c31e29a7e5c6cd716c2f43a
7
+ data.tar.gz: ecbe7d88dede8e5cfd9f217883da8125c0ed796c2f0cad64949b62d9d75ec06896a6f9ed3deb8ad1cb0f5976f6faf05ea865cd8d234d268f3a31f8b2db450afe
data/bin/tailstrom CHANGED
@@ -1,52 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
2
  $: << File.expand_path('../../lib', __FILE__)
3
- require 'optparse'
3
+ require 'tailstrom/option_parser'
4
4
 
5
- def file_or_string(value)
6
- File.exist?(value) ? File.read(value) : value
7
- end
8
-
9
- options = {
10
- :delimiter => "\t",
11
- :interval => 1,
12
- :mode => :stat,
13
- :order => :desc
14
- }
15
- OptionParser.new(ARGV) {|opt|
16
- opt.banner = <<-END
17
- tail -f access.log | #{$0} [OPTIONS]
18
- #{$0} [OPTIONS] [file]
19
- END
20
- opt.on('-f num', Integer, 'value field') {|v| options[:field] = v }
21
- opt.on('-k num', Integer, 'key field') {|v| options[:key] = v }
22
- opt.on('-d delimiter', String, 'delimiter') {|v| options[:delimiter] = v }
23
- opt.on('-i interval', Integer, 'interval for stat mode') {|v| options[:interval] = v }
24
- opt.on('-e file_or_string', '--in-filter file_or_string', String, 'input filtering') do |v|
25
- options[:in_filter] = file_or_string v
26
- end
27
- opt.on('--map file_or_string', String, 'input mapping') do |v|
28
- options[:map] = file_or_string v
29
- end
30
- opt.on('--out-filter file_or_string', String, 'output filtering') do |v|
31
- options[:out_filter] = file_or_string v
32
- end
33
- opt.on('--sort file_or_string', String, 'output sorting') do |v|
34
- options[:sort] = file_or_string v
35
- end
36
- opt.on('--order desc|asc', String, 'sorting order (default=desc)') do |v|
37
- options[:order] = v.to_s.downcase == 'asc' ? :asc : :desc
38
- end
39
- opt.on('--stat', 'statistics mode (default)') { options[:mode] = :stat }
40
- opt.on('--print', 'print line mode') { options[:mode] = :print }
41
- opt.on('--version', 'show version') do
42
- require 'tailstrom/version'
43
- puts Tailstrom::VERSION
44
- exit 0
45
- end
46
- }.order!
47
- if infile = ARGV.shift
48
- options[:static_infile] = open(infile, 'r')
49
- end
5
+ options = Tailstrom::OptionParser.new.parse ARGV
50
6
 
51
7
  require "tailstrom/command/#{options[:mode]}"
52
8
  cls = Tailstrom::Command.const_get options[:mode].to_s.capitalize
@@ -40,10 +40,6 @@ module Tailstrom
40
40
 
41
41
  print_counters
42
42
 
43
- if @counters.size > 1
44
- @table.puts
45
- end
46
-
47
43
  @counters.clear
48
44
  i = i + 1
49
45
  end while !reader.eof?
@@ -62,6 +58,7 @@ module Tailstrom
62
58
  end
63
59
 
64
60
  def print_counters
61
+ printed_lines = 0
65
62
  sorted_counters.each do |key, c|
66
63
  key = (key == :nil ? nil : key)
67
64
  next unless out_filter(key, c)
@@ -71,7 +68,9 @@ module Tailstrom
71
68
  else
72
69
  @table.print_row c.count, c.min, c.max, c.avg, key
73
70
  end
71
+ printed_lines += 1
74
72
  end
73
+ @table.puts if printed_lines > 1
75
74
  end
76
75
 
77
76
  def sorted_counters
@@ -0,0 +1,93 @@
1
+ require 'optparse'
2
+
3
+ module Tailstrom
4
+ class OptionParser
5
+ DEFAULTS = {
6
+ :delimiter => "\t",
7
+ :interval => 1,
8
+ :mode => :stat,
9
+ :order => :desc
10
+ }.freeze
11
+
12
+ def initialize
13
+ @options = {}
14
+ @options_from_file = {}
15
+ end
16
+
17
+ def parse(argv)
18
+ parser.order! argv
19
+ if infile = argv.shift
20
+ @options[:static_infile] = open(infile, 'r')
21
+ end
22
+ @options = @options_from_file.merge @options
23
+ DEFAULTS.merge @options
24
+ end
25
+
26
+ def parser
27
+ ::OptionParser.new do |opt|
28
+ opt.banner = <<-END
29
+ tail -f access.log | #{$0} [OPTIONS]
30
+ #{$0} [OPTIONS] [file]
31
+ END
32
+ opt.on('-c file', '--config file', String, 'config file') do |v|
33
+ require 'tailstrom/config_loader'
34
+ @options_from_file = load_config v
35
+ end
36
+ opt.on('-f num', Integer, 'value field') do |v|
37
+ @options[:field] = v
38
+ end
39
+ opt.on('-k num', Integer, 'key field') do |v|
40
+ @options[:key] = v
41
+ end
42
+ opt.on('-d delimiter', String, 'delimiter') do |v|
43
+ @options[:delimiter] = v
44
+ end
45
+ opt.on('-i interval', Integer, 'interval for stat mode') do |v|
46
+ @options[:interval] = v
47
+ end
48
+ opt.on('-e file_or_string', '--in-filter file_or_string', String, 'input filtering') do |v|
49
+ @options[:in_filter] = file_or_string v
50
+ end
51
+ opt.on('--map file_or_string', String, 'input mapping') do |v|
52
+ @options[:map] = file_or_string v
53
+ end
54
+ opt.on('--out-filter file_or_string', String, 'output filtering') do |v|
55
+ @options[:out_filter] = file_or_string v
56
+ end
57
+ opt.on('--sort file_or_string', String, 'output sorting') do |v|
58
+ @options[:sort] = file_or_string v
59
+ end
60
+ opt.on('--order desc|asc', String, 'sorting order (default=desc)') do |v|
61
+ @options[:order] = v.to_s.downcase == 'asc' ? :asc : :desc
62
+ end
63
+ opt.on('--stat', 'statistics mode (default)') do
64
+ @options[:mode] = :stat
65
+ end
66
+ opt.on('--print', 'print line mode') do
67
+ @options[:mode] = :print
68
+ end
69
+ opt.on('--version', 'show version') do
70
+ require 'tailstrom/version'
71
+ puts Tailstrom::VERSION
72
+ exit 0
73
+ end
74
+ end
75
+ end
76
+
77
+ private
78
+ def file_or_string(value)
79
+ File.exist?(value) ? File.read(value) : value
80
+ end
81
+
82
+ def load_config(file)
83
+ argv = []
84
+ hash = YAML.load File.read(file)
85
+ hash.each do |k, v|
86
+ argv << (k.length > 1 ? "--#{k}" : "-#{k}")
87
+ argv << (v unless v.is_a? TrueClass)
88
+ end
89
+ parser = Tailstrom::OptionParser.new
90
+ parser.parse argv
91
+ end
92
+ end
93
+ end
@@ -1,3 +1,3 @@
1
1
  module Tailstrom
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tailstrom
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Issei Naruta
@@ -31,6 +31,7 @@ files:
31
31
  - lib/tailstrom/command/stat.rb
32
32
  - lib/tailstrom/counter.rb
33
33
  - lib/tailstrom/counter_collection.rb
34
+ - lib/tailstrom/option_parser.rb
34
35
  - lib/tailstrom/table.rb
35
36
  - lib/tailstrom/tail_reader.rb
36
37
  - lib/tailstrom/version.rb