lf 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.rspec +2 -0
- data/.rubocop.yml +11 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +120 -0
- data/Rakefile +19 -0
- data/bin/lf +15 -0
- data/example.ltsv +2 -0
- data/features/cli.feature +35 -0
- data/features/filter.feature +57 -0
- data/features/support/env.rb +16 -0
- data/lf.gemspec +32 -0
- data/lib/lf/cli.rb +23 -0
- data/lib/lf/filter/eq.rb +11 -0
- data/lib/lf/filter/ignore.rb +13 -0
- data/lib/lf/filter/label.rb +13 -0
- data/lib/lf/filter/regexp.rb +12 -0
- data/lib/lf/filter.rb +47 -0
- data/lib/lf/formatter/ltsv.rb +13 -0
- data/lib/lf/formatter/table.rb +14 -0
- data/lib/lf/formatter.rb +20 -0
- data/lib/lf/option.rb +81 -0
- data/lib/lf/row.rb +22 -0
- data/lib/lf/stream.rb +21 -0
- data/lib/lf/version.rb +3 -0
- data/lib/lf.rb +4 -0
- data/spec/lib/lf/row_spec.rb +24 -0
- data/spec/spec_helper.rb +98 -0
- metadata +219 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: aa582324d98d742bb355286148125bf7f0c136be
|
4
|
+
data.tar.gz: e9a36b319f87ec8584336c393d14902e2e44e493
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 56f9558d583a45153ac55474400966919b094e49619991fde33b32b56a5111a4723177bb51f90881cfea0566b23b07652fb61d214b701f2004014ea20d8520a3
|
7
|
+
data.tar.gz: 8359bc5d40a1e0c38d3542faf6135c2eb56ae669caeb2b9812433c98d1b8a1ba4fb2c8750b9e6e9ca411619be639ff90d1ac6a852d624b0b98f24429970216b0
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Sho Kusano
|
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,120 @@
|
|
1
|
+
# lf
|
2
|
+
|
3
|
+
__lf__ is a lightweight command-line LTSV processor.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Install it yourself as:
|
8
|
+
|
9
|
+
$ gem install lf
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```
|
14
|
+
$ lf -h
|
15
|
+
Usage: lf [options] <filters ...> [file]
|
16
|
+
-r, --require FILE Require a file
|
17
|
+
-c, --[no-]color Colored output
|
18
|
+
-f, --format FORMAT Choose a format(ltsv, table)
|
19
|
+
-b, --[no-]buffered Flush the output after each LTSV row
|
20
|
+
-h, --help Show this message
|
21
|
+
-v, --version Show version
|
22
|
+
```
|
23
|
+
|
24
|
+
### Input
|
25
|
+
|
26
|
+
Specify LTSV file path:
|
27
|
+
|
28
|
+
```
|
29
|
+
$ lf example.ltsv
|
30
|
+
```
|
31
|
+
|
32
|
+
Or input via pipe:
|
33
|
+
|
34
|
+
```
|
35
|
+
$ tail -f example.ltsv | lf
|
36
|
+
```
|
37
|
+
|
38
|
+
### Filters
|
39
|
+
|
40
|
+
You can set filters to lf: `filter-name:args`
|
41
|
+
|
42
|
+
```
|
43
|
+
$ lf label:tag1,foo example.ltsv
|
44
|
+
tag1:test foo:bar
|
45
|
+
$ lf label:tag1 example.ltsv
|
46
|
+
tag1:test
|
47
|
+
```
|
48
|
+
|
49
|
+
#### Label filter
|
50
|
+
|
51
|
+
`label:select labels(comma separated)`
|
52
|
+
|
53
|
+
```
|
54
|
+
$ lf label:tag1,foo example.ltsv
|
55
|
+
tag1:test foo:bar
|
56
|
+
tag1:test foo:baz
|
57
|
+
$ lf label:tag1 example.ltsv
|
58
|
+
tag1:test
|
59
|
+
tag1:test
|
60
|
+
```
|
61
|
+
|
62
|
+
#### Ignore filter
|
63
|
+
|
64
|
+
`ignore:ignore labels(comma separated)`
|
65
|
+
|
66
|
+
```
|
67
|
+
$ lf ignore:tag1 example.ltsv
|
68
|
+
foo:bar test:key long:long long long message long long long label:short
|
69
|
+
foo:baz test:key long:long long long message long long long label:short
|
70
|
+
```
|
71
|
+
|
72
|
+
#### Equal filter
|
73
|
+
|
74
|
+
`equal:label:value`
|
75
|
+
|
76
|
+
```
|
77
|
+
$ lf eq:foo:bar example.ltsv
|
78
|
+
tag1:test foo:bar test:key long:long long long message long long long label:short
|
79
|
+
```
|
80
|
+
|
81
|
+
#### Regexp filter
|
82
|
+
|
83
|
+
`regexp:label:regexp`
|
84
|
+
|
85
|
+
```
|
86
|
+
$ lf reg:foo:r$ example.ltsv
|
87
|
+
tag1:test foo:bar test:key long:long long long message long long long label:short
|
88
|
+
```
|
89
|
+
|
90
|
+
#### Customu filter
|
91
|
+
|
92
|
+
Make your custom filters:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
# custom-filter.rb
|
96
|
+
class StatusCode < Lf::Filter
|
97
|
+
filter_alias :status
|
98
|
+
|
99
|
+
def initialize(status_code)
|
100
|
+
@status_code = status_code.to_i
|
101
|
+
end
|
102
|
+
|
103
|
+
def apply(row)
|
104
|
+
row[:status].to_i == @status_code ? row : nil
|
105
|
+
end
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
```
|
110
|
+
$ lf --require custom-filter.rb status:200 nginx.log
|
111
|
+
```
|
112
|
+
|
113
|
+
|
114
|
+
## Contributing
|
115
|
+
|
116
|
+
1. Fork it ( https://github.com/rosylilly/lf/fork )
|
117
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
118
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
119
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
120
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'bundler/gem_tasks'
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
require 'rubocop/rake_task'
|
5
|
+
require 'cucumber/rake/task'
|
6
|
+
require 'fileutils'
|
7
|
+
|
8
|
+
RSpec::Core::RakeTask.new(:spec)
|
9
|
+
RuboCop::RakeTask.new
|
10
|
+
Cucumber::Rake::Task.new
|
11
|
+
|
12
|
+
namespace :coverage do
|
13
|
+
desc 'Cleanup simplecov coverage directory'
|
14
|
+
task :clean do
|
15
|
+
FileUtils.rmtree('coverage')
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
task default: [:rubocop, 'coverage:clean', :spec, :cucumber]
|
data/bin/lf
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
if ENV['COVERAGE']
|
4
|
+
require 'simplecov'
|
5
|
+
SimpleCov.command_name "binary #{Process.pid}"
|
6
|
+
SimpleCov.root(File.join(File.expand_path(File.dirname(__FILE__)), '..'))
|
7
|
+
SimpleCov.start do
|
8
|
+
add_filter 'features/'
|
9
|
+
add_filter 'spec/'
|
10
|
+
add_filter 'tmp/'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
require 'lf'
|
15
|
+
Lf::CLI.new(ARGV.dup).execute!
|
data/example.ltsv
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
Feature: Run lf command
|
2
|
+
Scenario: Running lf command
|
3
|
+
When I run `lf` interactively
|
4
|
+
And I close the stdin stream
|
5
|
+
Then the exit status should be 0
|
6
|
+
|
7
|
+
Scenario: Show version
|
8
|
+
When I run `lf -v`
|
9
|
+
Then the exit status should not be 0
|
10
|
+
Then the output should contain "lf"
|
11
|
+
|
12
|
+
Scenario: Show help
|
13
|
+
When I run `lf -h`
|
14
|
+
Then the exit status should not be 0
|
15
|
+
Then the output should contain "Usage: lf"
|
16
|
+
|
17
|
+
Scenario: Show error on the unknown option
|
18
|
+
When I run `lf --unknown`
|
19
|
+
Then the exit status should not be 0
|
20
|
+
|
21
|
+
Scenario: Parsing ltsv
|
22
|
+
When I run `lf` interactively
|
23
|
+
And I type "tag1:hoge tag2:fuga"
|
24
|
+
And I type "tag:nginx status:200"
|
25
|
+
And I close the stdin stream
|
26
|
+
Then the output should contain "tag1:hoge"
|
27
|
+
Then the output should contain "tag:nginx"
|
28
|
+
|
29
|
+
Scenario: Parsing ltsv file
|
30
|
+
Given a file named "test.ltsv" with:
|
31
|
+
"""
|
32
|
+
tag:ayataka message:選ばれたのは綾鷹でした
|
33
|
+
"""
|
34
|
+
When I run `lf test.ltsv`
|
35
|
+
Then the output should contain "tag:ayataka"
|
@@ -0,0 +1,57 @@
|
|
1
|
+
Feature: Filters
|
2
|
+
Scenario: Label filter
|
3
|
+
When I run `lf label:tag1` interactively
|
4
|
+
And I type "tag1:hoge tag2:fuga"
|
5
|
+
And I close the stdin stream
|
6
|
+
Then the output should contain "tag1"
|
7
|
+
Then the output should not contain "tag2"
|
8
|
+
|
9
|
+
Scenario: Ignore filter
|
10
|
+
When I run `lf ignore:tag2` interactively
|
11
|
+
And I type "tag1:hoge tag2:fuga"
|
12
|
+
And I close the stdin stream
|
13
|
+
Then the output should contain "tag1"
|
14
|
+
Then the output should not contain "tag2"
|
15
|
+
|
16
|
+
Scenario: Chain filter
|
17
|
+
When I run `lf label:tag1,tag3 ignore:tag3` interactively
|
18
|
+
And I type "tag1:hoge tag2:fuga tag3:piyo"
|
19
|
+
And I close the stdin stream
|
20
|
+
Then the output should contain "tag1"
|
21
|
+
Then the output should not contain "tag2"
|
22
|
+
Then the output should not contain "tag3"
|
23
|
+
|
24
|
+
Scenario: Quoted argv
|
25
|
+
When I run `lf "label:tag1,tag3"` interactively
|
26
|
+
And I type "tag1:hoge tag2:fuga"
|
27
|
+
And I close the stdin stream
|
28
|
+
Then the output should contain "tag1"
|
29
|
+
Then the output should not contain "tag2"
|
30
|
+
|
31
|
+
Scenario: Equal filter(match)
|
32
|
+
When I run `lf eq:tag1:hoge` interactively
|
33
|
+
And I type "tag1:hoge tag2:fuga"
|
34
|
+
And I close the stdin stream
|
35
|
+
Then the output should contain "tag1"
|
36
|
+
Then the output should contain "tag2"
|
37
|
+
|
38
|
+
Scenario: Equal filter(miss-match)
|
39
|
+
When I run `lf eq:tag1:fuga` interactively
|
40
|
+
And I type "tag1:hoge tag2:fuga"
|
41
|
+
And I close the stdin stream
|
42
|
+
Then the output should not contain "tag1"
|
43
|
+
Then the output should not contain "tag2"
|
44
|
+
|
45
|
+
Scenario: Regexp filter(match)
|
46
|
+
When I run `lf reg:tag1:..ge` interactively
|
47
|
+
And I type "tag1:hoge tag2:fuga"
|
48
|
+
And I close the stdin stream
|
49
|
+
Then the output should contain "tag1"
|
50
|
+
Then the output should contain "tag2"
|
51
|
+
|
52
|
+
Scenario: Regexp filter(miss-match)
|
53
|
+
When I run `lf reg:tag1:^f` interactively
|
54
|
+
And I type "tag1:hoge tag2:fuga"
|
55
|
+
And I close the stdin stream
|
56
|
+
Then the output should not contain "tag1"
|
57
|
+
Then the output should not contain "tag2"
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'simplecov'
|
2
|
+
SimpleCov.start do
|
3
|
+
add_filter 'features/'
|
4
|
+
add_filter 'spec/'
|
5
|
+
add_filter 'tmp/'
|
6
|
+
end
|
7
|
+
|
8
|
+
require 'aruba'
|
9
|
+
require 'aruba/cucumber'
|
10
|
+
require 'aruba/in_process'
|
11
|
+
require 'rspec/expectations'
|
12
|
+
require 'lf'
|
13
|
+
|
14
|
+
Before do
|
15
|
+
set_env('COVERAGE', 'true')
|
16
|
+
end
|
data/lf.gemspec
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'lf/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'lf'
|
8
|
+
spec.version = Lf::VERSION
|
9
|
+
spec.authors = ['Sho Kusano']
|
10
|
+
spec.email = ['rosylilly@aduca.org']
|
11
|
+
spec.summary = 'lf is a lightweight command-line LTSV processor'
|
12
|
+
spec.description = 'lf is a lightweight command-line LTSV processor'
|
13
|
+
spec.homepage = 'https://github.com/rosylilly/lf'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(/^bin\//) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(/^(test|spec|features)\//)
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'rspec', '~> 3.1.0'
|
24
|
+
spec.add_development_dependency 'aruba', '~> 0.6.1'
|
25
|
+
spec.add_development_dependency 'rubocop', '~> 0.26.1'
|
26
|
+
spec.add_development_dependency 'simplecov', '~> 0.9.1'
|
27
|
+
spec.add_development_dependency 'cucumber', '~> 1.3.17'
|
28
|
+
|
29
|
+
spec.add_dependency 'ltsv', '~> 0.1.0'
|
30
|
+
spec.add_dependency 'terminal-table', '~> 1.4.5'
|
31
|
+
spec.add_dependency 'term-ansicolor', '~> 1.3.0'
|
32
|
+
end
|
data/lib/lf/cli.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'terminal-table'
|
2
|
+
require 'lf'
|
3
|
+
require 'lf/option'
|
4
|
+
require 'lf/stream'
|
5
|
+
|
6
|
+
class Lf::CLI
|
7
|
+
def initialize(argv, stdin = STDIN, stdout = STDOUT, stderr = STDERR, kernel = Kernel)
|
8
|
+
@argv, @stdin, @stdout, @stderr, @kernel = argv, stdin, stdout, stderr, kernel
|
9
|
+
|
10
|
+
@option = Lf::Option.parse(@argv, @stdin, @stdout)
|
11
|
+
@stream = Lf::Stream.new(@option.input, @option.output, @option)
|
12
|
+
|
13
|
+
rescue => e
|
14
|
+
@stderr.puts e.message
|
15
|
+
@kernel.exit(1)
|
16
|
+
end
|
17
|
+
|
18
|
+
def execute!
|
19
|
+
@stream.process
|
20
|
+
|
21
|
+
@kernel.exit(0)
|
22
|
+
end
|
23
|
+
end
|
data/lib/lf/filter/eq.rb
ADDED
data/lib/lf/filter.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'lf'
|
2
|
+
|
3
|
+
class Lf::Filter
|
4
|
+
FILTERS = {}
|
5
|
+
|
6
|
+
def self.inherited(klass)
|
7
|
+
Lf::Filter[klass.name.sub(/.*::/, '').downcase] = klass
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.filter_alias(name)
|
11
|
+
Lf::Filter[name] = self
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.[](name)
|
15
|
+
FILTERS[name.to_s.to_sym]
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.[]=(name, klass)
|
19
|
+
FILTERS[name.to_s.to_sym] = klass
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.parse(argv)
|
23
|
+
filters = []
|
24
|
+
|
25
|
+
argv.each do |arg|
|
26
|
+
arg = arg.sub(/^([^:]*):/, '')
|
27
|
+
if ::Regexp.last_match && ::Regexp.last_match[1]
|
28
|
+
name = ::Regexp.last_match[1].to_s.to_sym
|
29
|
+
filters << self[name].new(arg) if self[name]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
filters
|
34
|
+
end
|
35
|
+
|
36
|
+
def initialize(arg)
|
37
|
+
@arg = arg
|
38
|
+
end
|
39
|
+
|
40
|
+
def apply(row)
|
41
|
+
row
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
Dir.glob(::File.expand_path('../filter/*.rb', __FILE__)).each do |f|
|
46
|
+
require_relative f
|
47
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
class Lf::Formatter::Table < Lf::Formatter
|
2
|
+
Lf::Formatter[:table] = self
|
3
|
+
|
4
|
+
def self.format(row)
|
5
|
+
field_size = row.keys.map(&:to_s).sort_by(&:length).last.to_s.size + 1
|
6
|
+
|
7
|
+
fields = []
|
8
|
+
row.each_pair do |label, val|
|
9
|
+
fields << "#{green(label.to_s.ljust(field_size))}: #{cyan(val.to_s)}"
|
10
|
+
end
|
11
|
+
|
12
|
+
"----\n" + fields.join("\n")
|
13
|
+
end
|
14
|
+
end
|
data/lib/lf/formatter.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'term/ansicolor'
|
2
|
+
require 'lf'
|
3
|
+
|
4
|
+
class Lf::Formatter
|
5
|
+
extend Term::ANSIColor
|
6
|
+
|
7
|
+
FORMATTERS = {}
|
8
|
+
|
9
|
+
def self.[](name)
|
10
|
+
FORMATTERS[name]
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.[]=(name, klass)
|
14
|
+
FORMATTERS[name.to_s.to_sym] = klass
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Dir.glob(::File.expand_path('../formatter/*.rb', __FILE__)).each do |f|
|
19
|
+
require_relative f
|
20
|
+
end
|
data/lib/lf/option.rb
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'lf'
|
3
|
+
require 'lf/formatter'
|
4
|
+
require 'lf/filter'
|
5
|
+
|
6
|
+
class Lf::Option < ::OptionParser
|
7
|
+
SUPPORTED_FORMATS = Lf::Formatter::FORMATTERS.keys
|
8
|
+
|
9
|
+
def self.parse(*args)
|
10
|
+
new(*args).tap { |option| option.parse!(option.argv) }
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(argv, stdin, stdout)
|
14
|
+
super()
|
15
|
+
@argv, @input, @output = argv.dup, stdin, stdout
|
16
|
+
@options = {
|
17
|
+
format: 'ltsv'
|
18
|
+
}
|
19
|
+
@filters = []
|
20
|
+
|
21
|
+
configure_options
|
22
|
+
end
|
23
|
+
|
24
|
+
attr_reader :argv, :input, :output, :filters
|
25
|
+
|
26
|
+
def version
|
27
|
+
Lf::VERSION
|
28
|
+
end
|
29
|
+
|
30
|
+
def banner
|
31
|
+
'Usage: lf [options] <filters ...> [file]'
|
32
|
+
end
|
33
|
+
|
34
|
+
def show_help
|
35
|
+
output.puts self
|
36
|
+
exit 1
|
37
|
+
end
|
38
|
+
|
39
|
+
def parse!(*)
|
40
|
+
super
|
41
|
+
|
42
|
+
@input = open(@argv.pop, 'r') if File.file?(@argv.last.to_s)
|
43
|
+
@filters = Lf::Filter.parse(@argv)
|
44
|
+
end
|
45
|
+
|
46
|
+
def [](key)
|
47
|
+
@options[key]
|
48
|
+
end
|
49
|
+
|
50
|
+
private
|
51
|
+
|
52
|
+
# rubocop:disable MethodLength
|
53
|
+
def configure_options
|
54
|
+
on('-r FILE', '--require FILE', 'Require a file') do |file|
|
55
|
+
require file
|
56
|
+
end
|
57
|
+
|
58
|
+
on('-c', '--[no-]color', @input.isatty.class, 'Colored output') do |val|
|
59
|
+
@options[:color] = val
|
60
|
+
end
|
61
|
+
|
62
|
+
on('-f FORMAT', '--format FORMAT', "Choose a format(#{SUPPORTED_FORMATS.join(', ')})") do |val|
|
63
|
+
val = val.to_s.to_sym
|
64
|
+
@options[:format] = val if SUPPORTED_FORMATS.include?(val)
|
65
|
+
end
|
66
|
+
|
67
|
+
on('-b', '--[no-]buffered', TrueClass, 'Flush the output after each LTSV row') do |val|
|
68
|
+
@options[:buffered] = val
|
69
|
+
end
|
70
|
+
|
71
|
+
on_tail('-h', '--help', 'Show this message') do
|
72
|
+
show_help
|
73
|
+
end
|
74
|
+
|
75
|
+
on_tail('-v', '--version', 'Show version') do
|
76
|
+
output.puts ver
|
77
|
+
exit 2
|
78
|
+
end
|
79
|
+
end
|
80
|
+
# rubocop:enable MethodLength
|
81
|
+
end
|
data/lib/lf/row.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'ltsv'
|
2
|
+
require 'delegate'
|
3
|
+
require 'lf'
|
4
|
+
require 'lf/formatter'
|
5
|
+
|
6
|
+
class Lf::Row < SimpleDelegator
|
7
|
+
attr_reader :raw
|
8
|
+
|
9
|
+
def initialize(line)
|
10
|
+
if line.is_a?(Hash)
|
11
|
+
@raw = line
|
12
|
+
else
|
13
|
+
@raw = LTSV.parse(line.to_s.chomp)[0]
|
14
|
+
end
|
15
|
+
|
16
|
+
super(@raw)
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_s(format = :ltsv)
|
20
|
+
Lf::Formatter[format.to_s.to_sym].format(self)
|
21
|
+
end
|
22
|
+
end
|
data/lib/lf/stream.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'lf'
|
2
|
+
require 'lf/row'
|
3
|
+
|
4
|
+
class Lf::Stream
|
5
|
+
def initialize(input, output, option)
|
6
|
+
@input, @output, @option = input, output, option
|
7
|
+
end
|
8
|
+
|
9
|
+
def process
|
10
|
+
loop do
|
11
|
+
line = @input.gets
|
12
|
+
break unless line
|
13
|
+
row = Lf::Row.new(line)
|
14
|
+
row = @option.filters.reduce(row) { |a, e| a && e.apply(a) }
|
15
|
+
@output.puts(row.to_s(@option[:format])) if row
|
16
|
+
@output.flush unless @option[:buffered]
|
17
|
+
end
|
18
|
+
rescue Interrupt
|
19
|
+
@output.flush
|
20
|
+
end
|
21
|
+
end
|
data/lib/lf/version.rb
ADDED
data/lib/lf.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
describe Lf::Row do
|
2
|
+
before do
|
3
|
+
Term::ANSIColor.coloring = false
|
4
|
+
end
|
5
|
+
|
6
|
+
subject(:row) { described_class.new(ltsv_line) }
|
7
|
+
|
8
|
+
describe '#initialize' do
|
9
|
+
let(:ltsv_line) { "tag1:foo\ttag2:bar\n" }
|
10
|
+
|
11
|
+
it 'should be ok' do
|
12
|
+
expect(row).to be_a described_class
|
13
|
+
expect(row).to have_key(:tag1)
|
14
|
+
expect(row).to have_key(:tag2)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#to_s' do
|
19
|
+
let(:ltsv_line) { 'tag1:foo' }
|
20
|
+
subject(:to_s) { row.to_s }
|
21
|
+
|
22
|
+
it { expect(to_s).to eq('tag1:foo') }
|
23
|
+
end
|
24
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
4
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
5
|
+
#
|
6
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
7
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
8
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
9
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
10
|
+
# a separate helper file that requires the additional dependencies and performs
|
11
|
+
# the additional setup, and require it from the spec files that actually need it.
|
12
|
+
#
|
13
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
14
|
+
# users commonly want.
|
15
|
+
#
|
16
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
17
|
+
require 'simplecov'
|
18
|
+
SimpleCov.start do
|
19
|
+
add_filter 'spec/'
|
20
|
+
end
|
21
|
+
|
22
|
+
Dir.glob(::File.expand_path('../support/*.rb', __FILE__)).each do |f|
|
23
|
+
require_relative f
|
24
|
+
end
|
25
|
+
|
26
|
+
require 'lf'
|
27
|
+
|
28
|
+
RSpec.configure do |config|
|
29
|
+
# rspec-expectations config goes here. You can use an alternate
|
30
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
31
|
+
# assertions if you prefer.
|
32
|
+
config.expect_with :rspec do |expectations|
|
33
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
34
|
+
# and `failure_message` of custom matchers include text for helper methods
|
35
|
+
# defined using `chain`, e.g.:
|
36
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
37
|
+
# # => "be bigger than 2 and smaller than 4"
|
38
|
+
# ...rather than:
|
39
|
+
# # => "be bigger than 2"
|
40
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
41
|
+
end
|
42
|
+
|
43
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
44
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
45
|
+
config.mock_with :rspec do |mocks|
|
46
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
47
|
+
# a real object. This is generally recommended, and will default to
|
48
|
+
# `true` in RSpec 4.
|
49
|
+
mocks.verify_partial_doubles = true
|
50
|
+
end
|
51
|
+
|
52
|
+
# The settings below are suggested to provide a good initial experience
|
53
|
+
# with RSpec, but feel free to customize to your heart's content.
|
54
|
+
# # These two settings work together to allow you to limit a spec run
|
55
|
+
# # to individual examples or groups you care about by tagging them with
|
56
|
+
# # `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
57
|
+
# # get run.
|
58
|
+
# config.filter_run :focus
|
59
|
+
# config.run_all_when_everything_filtered = true
|
60
|
+
#
|
61
|
+
# # Limits the available syntax to the non-monkey patched syntax that is recommended.
|
62
|
+
# # For more details, see:
|
63
|
+
# # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
64
|
+
# # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
65
|
+
# # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
66
|
+
# config.disable_monkey_patching!
|
67
|
+
#
|
68
|
+
# # This setting enables warnings. It's recommended, but in some cases may
|
69
|
+
# # be too noisy due to issues in dependencies.
|
70
|
+
# config.warnings = true
|
71
|
+
#
|
72
|
+
# # Many RSpec users commonly either run the entire suite or an individual
|
73
|
+
# # file, and it's useful to allow more verbose output when running an
|
74
|
+
# # individual spec file.
|
75
|
+
# if config.files_to_run.one?
|
76
|
+
# # Use the documentation formatter for detailed output,
|
77
|
+
# # unless a formatter has already been configured
|
78
|
+
# # (e.g. via a command-line flag).
|
79
|
+
# config.default_formatter = 'doc'
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# # Print the 10 slowest examples and example groups at the
|
83
|
+
# # end of the spec run, to help surface which specs are running
|
84
|
+
# # particularly slow.
|
85
|
+
# config.profile_examples = 10
|
86
|
+
#
|
87
|
+
# # Run specs in random order to surface order dependencies. If you find an
|
88
|
+
# # order dependency and want to debug it, you can fix the order by providing
|
89
|
+
# # the seed, which is printed after each run.
|
90
|
+
# # --seed 1234
|
91
|
+
# config.order = :random
|
92
|
+
#
|
93
|
+
# # Seed global randomization in this process using the `--seed` CLI option.
|
94
|
+
# # Setting this allows you to use `--seed` to deterministically reproduce
|
95
|
+
# # test failures related to randomization by passing the same `--seed` value
|
96
|
+
# # as the one that triggered the failure.
|
97
|
+
# Kernel.srand config.seed
|
98
|
+
end
|
metadata
ADDED
@@ -0,0 +1,219 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: lf
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sho Kusano
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-25 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.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
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: 3.1.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 3.1.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: aruba
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.6.1
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 0.6.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rubocop
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.26.1
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.26.1
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.9.1
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: 0.9.1
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: cucumber
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 1.3.17
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 1.3.17
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: ltsv
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.1.0
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.1.0
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: terminal-table
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 1.4.5
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 1.4.5
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: term-ansicolor
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 1.3.0
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 1.3.0
|
153
|
+
description: lf is a lightweight command-line LTSV processor
|
154
|
+
email:
|
155
|
+
- rosylilly@aduca.org
|
156
|
+
executables:
|
157
|
+
- lf
|
158
|
+
extensions: []
|
159
|
+
extra_rdoc_files: []
|
160
|
+
files:
|
161
|
+
- ".gitignore"
|
162
|
+
- ".rspec"
|
163
|
+
- ".rubocop.yml"
|
164
|
+
- Gemfile
|
165
|
+
- LICENSE.txt
|
166
|
+
- README.md
|
167
|
+
- Rakefile
|
168
|
+
- bin/lf
|
169
|
+
- example.ltsv
|
170
|
+
- features/cli.feature
|
171
|
+
- features/filter.feature
|
172
|
+
- features/support/env.rb
|
173
|
+
- lf.gemspec
|
174
|
+
- lib/lf.rb
|
175
|
+
- lib/lf/cli.rb
|
176
|
+
- lib/lf/filter.rb
|
177
|
+
- lib/lf/filter/eq.rb
|
178
|
+
- lib/lf/filter/ignore.rb
|
179
|
+
- lib/lf/filter/label.rb
|
180
|
+
- lib/lf/filter/regexp.rb
|
181
|
+
- lib/lf/formatter.rb
|
182
|
+
- lib/lf/formatter/ltsv.rb
|
183
|
+
- lib/lf/formatter/table.rb
|
184
|
+
- lib/lf/option.rb
|
185
|
+
- lib/lf/row.rb
|
186
|
+
- lib/lf/stream.rb
|
187
|
+
- lib/lf/version.rb
|
188
|
+
- spec/lib/lf/row_spec.rb
|
189
|
+
- spec/spec_helper.rb
|
190
|
+
homepage: https://github.com/rosylilly/lf
|
191
|
+
licenses:
|
192
|
+
- MIT
|
193
|
+
metadata: {}
|
194
|
+
post_install_message:
|
195
|
+
rdoc_options: []
|
196
|
+
require_paths:
|
197
|
+
- lib
|
198
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
199
|
+
requirements:
|
200
|
+
- - ">="
|
201
|
+
- !ruby/object:Gem::Version
|
202
|
+
version: '0'
|
203
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
204
|
+
requirements:
|
205
|
+
- - ">="
|
206
|
+
- !ruby/object:Gem::Version
|
207
|
+
version: '0'
|
208
|
+
requirements: []
|
209
|
+
rubyforge_project:
|
210
|
+
rubygems_version: 2.2.2
|
211
|
+
signing_key:
|
212
|
+
specification_version: 4
|
213
|
+
summary: lf is a lightweight command-line LTSV processor
|
214
|
+
test_files:
|
215
|
+
- features/cli.feature
|
216
|
+
- features/filter.feature
|
217
|
+
- features/support/env.rb
|
218
|
+
- spec/lib/lf/row_spec.rb
|
219
|
+
- spec/spec_helper.rb
|