tairb 0.0.1
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 +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +39 -0
- data/Rakefile +7 -0
- data/bin/tairb +33 -0
- data/lib/tairb.rb +65 -0
- data/lib/tairb/configuration.rb +41 -0
- data/lib/tairb/version.rb +3 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/tailrb_spec.rb +11 -0
- data/tairb.gemspec +25 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: f623702bdb0e1db71af0be0c44d7774dc4ab7b00
|
4
|
+
data.tar.gz: 3f89346e355f0373aa6521b2dda8996cd8d9b625
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 45e8c51585ae9eae8f18e4b92d34bca296842d4371810f5f86135f3088b5ba23f46af611b2b0c190b1f3f3b08cecbc640f0daeae10411dfdb7f45408847a78c5
|
7
|
+
data.tar.gz: eebdb85f00defab50226dc30838d7ec871953652adc068c9f663f8fe432f364ba3c1cd64b370559337435945ab68c16a7ef7bf3c51683d5d80d7cf0ae3756f3d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Arai Hiroki
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# tairb
|
2
|
+
|
3
|
+
tairb is tiny tool to read file like as `tail -f`.
|
4
|
+
|
5
|
+
## Getting started
|
6
|
+
|
7
|
+
You can install by following command.
|
8
|
+
|
9
|
+
$ gem install tairb
|
10
|
+
|
11
|
+
And can use as following command.
|
12
|
+
|
13
|
+
$ tairb log/development.log
|
14
|
+
|
15
|
+
## Custom output
|
16
|
+
|
17
|
+
You can intercept output for each lines.
|
18
|
+
|
19
|
+
$ tairb log/development.log --eval 'puts $_.strip.downcase'
|
20
|
+
|
21
|
+
## Filtering
|
22
|
+
|
23
|
+
tairb includes filtering feature as follwoing.
|
24
|
+
|
25
|
+
$ tairb log/development.log --filter '$_ =~ /DEBUG/'
|
26
|
+
|
27
|
+
## TSV support
|
28
|
+
|
29
|
+
tairb supports TSV (Tab-separated values) file.
|
30
|
+
|
31
|
+
$ tairb log/development.log --tsv -f '$_[:severity] == "DEBUG"'
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
1. Fork it ( https://github.com/hiroara/tairb/fork )
|
36
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
37
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
38
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
39
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/bin/tairb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'tairb'
|
4
|
+
|
5
|
+
require 'optparse'
|
6
|
+
|
7
|
+
BANNER = <<-EOT
|
8
|
+
tairb is tiny tool to read file like as `tail -f`.
|
9
|
+
Usage: tairb <options> <file>
|
10
|
+
EOT
|
11
|
+
|
12
|
+
config = {}
|
13
|
+
opt = OptionParser.new(BANNER) do |opt|
|
14
|
+
opt.on('-b <bytesize>', Integer, '--bytes', 'byte size of contents that already persisted to read') { |size| config[:bytes] = size }
|
15
|
+
opt.on('-t', '--tsv', 'parse file as TSV') { config[:type] = :tsv }
|
16
|
+
opt.on('-e <script>', '--eval', String, 'script for each lines') { |script| config[:script] = proc { |line| $_ = line; instance_eval script } }
|
17
|
+
opt.on('-f <script>', '--filter', String, 'script to filter lines') { |script| config[:filter] = proc { |line| $_ = line; instance_eval script } }
|
18
|
+
opt.parse! ARGV
|
19
|
+
end
|
20
|
+
|
21
|
+
unless ARGV.length == 1
|
22
|
+
STDERR.puts opt.help
|
23
|
+
exit 1
|
24
|
+
end
|
25
|
+
|
26
|
+
path = ARGV.first
|
27
|
+
|
28
|
+
begin
|
29
|
+
Tairb.new(path).run config
|
30
|
+
rescue RuntimeError => e
|
31
|
+
STDERR.puts e
|
32
|
+
exit 1
|
33
|
+
end
|
data/lib/tairb.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require 'tairb/version'
|
2
|
+
require 'json'
|
3
|
+
|
4
|
+
class Tairb
|
5
|
+
|
6
|
+
def initialize path
|
7
|
+
@path = File.expand_path path
|
8
|
+
end
|
9
|
+
|
10
|
+
def run config={}
|
11
|
+
config = Configuration.new self, config
|
12
|
+
|
13
|
+
self.send config.tailf_method, config.bytes, config.filter, &config.script
|
14
|
+
end
|
15
|
+
|
16
|
+
def tailf bytes=10000, filter=nil
|
17
|
+
seeked_file(bytes) do |file|
|
18
|
+
buf = ''
|
19
|
+
loop do
|
20
|
+
next unless str = file.read_nonblock(10) rescue !sleep(0.1)
|
21
|
+
next print str unless block_given?
|
22
|
+
buf += str
|
23
|
+
lines = buf.split /\r\n?|\n/, -1
|
24
|
+
while line = lines.shift
|
25
|
+
break buf = line if lines.empty?
|
26
|
+
next if filter && !filter.call(line)
|
27
|
+
yield line
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
rescue Interrupt
|
32
|
+
# exit
|
33
|
+
end
|
34
|
+
|
35
|
+
def tailf_tsv bytes=10000, filter=nil
|
36
|
+
tailf(bytes) do |line|
|
37
|
+
line.split("\t").inject({}) do |memo, token|
|
38
|
+
key, val = token.split(':', 2)
|
39
|
+
memo[key.intern] = val
|
40
|
+
memo
|
41
|
+
end.tap do |dat|
|
42
|
+
next if filter && !filter.call(dat)
|
43
|
+
block_given? ? yield(dat) : puts(to_tsv(dat))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
def seeked_file bytes, &block
|
50
|
+
open(@path, 'r') do |file|
|
51
|
+
raise "Not supported file type: #{file.stat.ftype}." unless file.stat.ftype == 'file'
|
52
|
+
file.seek(file.stat.size.tap { |size| break size > bytes ? size - bytes : 0 })
|
53
|
+
block.call file
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def to_s line
|
58
|
+
line.to_s
|
59
|
+
end
|
60
|
+
def to_tsv dat
|
61
|
+
dat.map{ |k, v| "#{k}:#{v}" }.join("\t")
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
require 'tairb/configuration'
|
@@ -0,0 +1,41 @@
|
|
1
|
+
class Tairb
|
2
|
+
class Configuration
|
3
|
+
DEFAULTS = {
|
4
|
+
bytes: 10000
|
5
|
+
}
|
6
|
+
|
7
|
+
attr_accessor :bytes, :script, :type
|
8
|
+
attr_reader :filter
|
9
|
+
|
10
|
+
def initialize context, config={}
|
11
|
+
@context = context
|
12
|
+
config = DEFAULTS.merge config
|
13
|
+
%i[ type bytes filter script ].each do |key|
|
14
|
+
self.send "#{key}=", config[key] if config.key? key
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def filter= filter
|
19
|
+
self.script ||= default_script if filter
|
20
|
+
@filter = filter
|
21
|
+
end
|
22
|
+
|
23
|
+
def tailf_method
|
24
|
+
case self.type
|
25
|
+
when :tsv then :tailf_tsv
|
26
|
+
else :tailf
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def default_script
|
31
|
+
serialize = @context.method default_serialize
|
32
|
+
proc { |dat| puts serialize.call(dat) }
|
33
|
+
end
|
34
|
+
def default_serialize
|
35
|
+
case self.type
|
36
|
+
when :tsv then :to_tsv
|
37
|
+
else :to_s
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/spec/spec_helper.rb
ADDED
data/spec/tailrb_spec.rb
ADDED
data/tairb.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'tairb/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "tairb"
|
8
|
+
spec.version = Tairb::VERSION
|
9
|
+
spec.authors = ["Arai Hiroki"]
|
10
|
+
spec.email = ["hiroara62@gmail.com"]
|
11
|
+
spec.summary = %q{tairb is tiny tool to read file like as `tail -f`.}
|
12
|
+
spec.description = %q{tairb is tiny tool to read file like as `tail -f`.}
|
13
|
+
spec.homepage = "https://github.com/hiroara/tairb"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "pry"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tairb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Arai Hiroki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-02 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: tairb is tiny tool to read file like as `tail -f`.
|
70
|
+
email:
|
71
|
+
- hiroara62@gmail.com
|
72
|
+
executables:
|
73
|
+
- tairb
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".travis.yml"
|
80
|
+
- Gemfile
|
81
|
+
- LICENSE.txt
|
82
|
+
- README.md
|
83
|
+
- Rakefile
|
84
|
+
- bin/tairb
|
85
|
+
- lib/tairb.rb
|
86
|
+
- lib/tairb/configuration.rb
|
87
|
+
- lib/tairb/version.rb
|
88
|
+
- spec/spec_helper.rb
|
89
|
+
- spec/tailrb_spec.rb
|
90
|
+
- tairb.gemspec
|
91
|
+
homepage: https://github.com/hiroara/tairb
|
92
|
+
licenses:
|
93
|
+
- MIT
|
94
|
+
metadata: {}
|
95
|
+
post_install_message:
|
96
|
+
rdoc_options: []
|
97
|
+
require_paths:
|
98
|
+
- lib
|
99
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - ">="
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
requirements: []
|
110
|
+
rubyforge_project:
|
111
|
+
rubygems_version: 2.4.6
|
112
|
+
signing_key:
|
113
|
+
specification_version: 4
|
114
|
+
summary: tairb is tiny tool to read file like as `tail -f`.
|
115
|
+
test_files:
|
116
|
+
- spec/spec_helper.rb
|
117
|
+
- spec/tailrb_spec.rb
|