easy_find 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/LICENSE +22 -0
- data/README.md +53 -0
- data/Rakefile +30 -0
- data/bin/easy_find +16 -0
- data/lib/easy_find/default_command_maker.rb +141 -0
- data/lib/easy_find/version.rb +4 -0
- data/lib/easy_find.rb +42 -0
- metadata +51 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 34706a5eae21c71599a96aeaa255b0a4786ef62f
|
4
|
+
data.tar.gz: 296fdd954886859914373ea8239e683f9591e5ef
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 66d1def379718a2a36c225b73d9c6c06cd7625a9092bab4ebb14c702d177e53e976b285bb9a75dfbf5f7a4265d1881728922ac1fba90ecbd7756f5dfcdfc674e
|
7
|
+
data.tar.gz: 408b52bcf13b16e6f5f42f08265a4d952556f84cc90e417d1478c3715f0c0dcb89bf64d3349a56b2da60fc7875c3a1bb121863ac94d3675b009bd31ecd030592
|
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Yi Wei
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
EasyFind
|
2
|
+
=============
|
3
|
+
|
4
|
+
EasyFind is a thin wrapper of \*nix find command.
|
5
|
+
It enables users to specify find command line. The syntax of
|
6
|
+
EasyFind is quite simple, and you may easily replace current syntax
|
7
|
+
with your perferred syntax.
|
8
|
+
|
9
|
+
Specify find command in STDIN string
|
10
|
+
_________________
|
11
|
+
|
12
|
+
Run *easy_find*, and you may specify intended find command in the STDIN string as follows:
|
13
|
+
```ruby
|
14
|
+
EasyFind::Finder.find do
|
15
|
+
in_folder { [".", "~", "/usr/bin"] }
|
16
|
+
where do
|
17
|
+
type "f"
|
18
|
+
size :greater_than, 1000
|
19
|
+
grouping do
|
20
|
+
atime :greater_than, 7
|
21
|
+
or_else
|
22
|
+
mtime :less_than, 10
|
23
|
+
end
|
24
|
+
end
|
25
|
+
with_actions { print }
|
26
|
+
end
|
27
|
+
|
28
|
+
```
|
29
|
+
The script will print generated find command line in the last line.
|
30
|
+
|
31
|
+
Specify find command in a .rb file
|
32
|
+
_____________________
|
33
|
+
|
34
|
+
You may save the above contents in a .rb file, say demo.rb.
|
35
|
+
Then run "*easy_find* demo.rb", and copy&paste generated printed find command line.
|
36
|
+
|
37
|
+
API calls
|
38
|
+
_____________________
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
require 'easy_find'
|
42
|
+
|
43
|
+
find_command = EasyFind::Finder.find do
|
44
|
+
in_folder { "/usr/bin" }
|
45
|
+
where do
|
46
|
+
...
|
47
|
+
end
|
48
|
+
with_actions do
|
49
|
+
...
|
50
|
+
end
|
51
|
+
end
|
52
|
+
```
|
53
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
require "bundler"
|
2
|
+
Bundler.setup
|
3
|
+
|
4
|
+
require "rake"
|
5
|
+
require "rspec/core/rake_task"
|
6
|
+
|
7
|
+
$LOAD_PATH.unshift File.expand_path("../lib", __FILE__)
|
8
|
+
require "easy_find/version"
|
9
|
+
|
10
|
+
task :gem => :build
|
11
|
+
task :build do
|
12
|
+
system "gem build easy_find.gemspec"
|
13
|
+
end
|
14
|
+
|
15
|
+
task :install => :build do
|
16
|
+
system "sudo gem install easy_find-#{EasyFind::VERSION}.gem"
|
17
|
+
end
|
18
|
+
|
19
|
+
task :release => :build do
|
20
|
+
system "git tag -a v#{EasyFind::VERSION} -m 'Tagging #{EasyFind::VERSION}'"
|
21
|
+
system "git push --tags"
|
22
|
+
system "gem push EasyFind-#{EasyFind::VERSION}.gem"
|
23
|
+
system "rm EasyFind-#{EasyFind::VERSION}.gem"
|
24
|
+
end
|
25
|
+
|
26
|
+
RSpec::Core::RakeTask.new("spec") do |spec|
|
27
|
+
spec.pattern = "spec/**/*_spec.rb"
|
28
|
+
end
|
29
|
+
|
30
|
+
task :default => :spec
|
data/bin/easy_find
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'easy_find'
|
3
|
+
|
4
|
+
if ARGV.length == 0
|
5
|
+
str = ''
|
6
|
+
while $stdin.gets
|
7
|
+
str << $_
|
8
|
+
end
|
9
|
+
puts eval str
|
10
|
+
elsif ARGV.length == 1
|
11
|
+
contents = open(ARGV[0]).read
|
12
|
+
puts eval contents
|
13
|
+
else
|
14
|
+
puts "Wrong number of arguments"
|
15
|
+
end
|
16
|
+
|
@@ -0,0 +1,141 @@
|
|
1
|
+
module EasyFind
|
2
|
+
class DefaultCommandMaker
|
3
|
+
SEPARATOR = ' '
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@folder_clause = ''
|
7
|
+
@where_clause = ''
|
8
|
+
@action_clause = ''
|
9
|
+
end
|
10
|
+
|
11
|
+
def make_folder_name(&block)
|
12
|
+
folder = block.call
|
13
|
+
if folder.is_a?(Array)
|
14
|
+
@folder_clause = SEPARATOR + folder.join(" ")
|
15
|
+
else
|
16
|
+
@folder_clause = folder.nil? ? "" : (SEPARATOR + folder)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def make_where_str(&block)
|
21
|
+
instance_eval &block
|
22
|
+
@where_clause
|
23
|
+
end
|
24
|
+
|
25
|
+
def make_actions_str(&block)
|
26
|
+
instance_eval &block
|
27
|
+
@action_clause
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def name(n)
|
33
|
+
build_quoted_where_segment("-name", n)
|
34
|
+
end
|
35
|
+
|
36
|
+
def size(*xs)
|
37
|
+
build_modified_action_str("-size", *xs)
|
38
|
+
end
|
39
|
+
|
40
|
+
def atime(*xs)
|
41
|
+
build_modified_action_str("-atime", *xs)
|
42
|
+
end
|
43
|
+
|
44
|
+
def mtime(*xs)
|
45
|
+
build_modified_action_str("-mtime", *xs)
|
46
|
+
end
|
47
|
+
|
48
|
+
def build_modified_action_str(action_str, *xs)
|
49
|
+
modified_param = modify(xs)
|
50
|
+
build_unquoted_where_segment(action_str, modified_param)
|
51
|
+
end
|
52
|
+
|
53
|
+
def modify(xs)
|
54
|
+
if xs.length == 1
|
55
|
+
xs[0]
|
56
|
+
elsif xs.length == 2
|
57
|
+
prefix_with_sign_symbol(xs)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
def prefix_with_sign_symbol(xs)
|
62
|
+
if xs[0] == :greater_than
|
63
|
+
"+#{xs[1]}"
|
64
|
+
elsif xs[0] == :less_than
|
65
|
+
"-#{xs[1]}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def type(n)
|
70
|
+
build_quoted_where_segment("-type", n)
|
71
|
+
end
|
72
|
+
|
73
|
+
def fstype(n)
|
74
|
+
build_quoted_where_segment("-fstype", n)
|
75
|
+
end
|
76
|
+
|
77
|
+
def user(n)
|
78
|
+
build_quoted_where_segment("-user", n)
|
79
|
+
end
|
80
|
+
|
81
|
+
def group(n)
|
82
|
+
build_quoted_where_segment("-group", n)
|
83
|
+
end
|
84
|
+
|
85
|
+
def perm(n)
|
86
|
+
build_unquoted_where_segment("-perm", n)
|
87
|
+
end
|
88
|
+
|
89
|
+
def build_quoted_where_segment(criteria, value)
|
90
|
+
quoted = '"' + value.to_s + '"'
|
91
|
+
build_where_segment(criteria, quoted)
|
92
|
+
end
|
93
|
+
|
94
|
+
def build_unquoted_where_segment(criteria, value)
|
95
|
+
build_where_segment(criteria, value)
|
96
|
+
end
|
97
|
+
|
98
|
+
def build_where_segment(criteria, value)
|
99
|
+
@where_clause += SEPARATOR + criteria + SEPARATOR + value.to_s
|
100
|
+
end
|
101
|
+
|
102
|
+
def grouping(&block)
|
103
|
+
in_group_statement = instance_eval &block
|
104
|
+
str = in_group_statement.nil? ? '' : in_group_statement
|
105
|
+
@where_clause = SEPARATOR + "\\(" + @where_clause + SEPARATOR + "\\)"
|
106
|
+
end
|
107
|
+
|
108
|
+
def or_else
|
109
|
+
@where_clause += SEPARATOR + "-o"
|
110
|
+
end
|
111
|
+
|
112
|
+
def print
|
113
|
+
build_action_str "-print"
|
114
|
+
end
|
115
|
+
|
116
|
+
def exec(cmd)
|
117
|
+
build_action_str "-exec #{cmd} \\;"
|
118
|
+
end
|
119
|
+
|
120
|
+
def ok(cmd)
|
121
|
+
build_action_str "-ok #{cmd} \\;"
|
122
|
+
end
|
123
|
+
|
124
|
+
def mount
|
125
|
+
build_action_str "-mount"
|
126
|
+
end
|
127
|
+
|
128
|
+
def xdev
|
129
|
+
build_action_str "-xdev"
|
130
|
+
end
|
131
|
+
|
132
|
+
def prune
|
133
|
+
build_action_str "-prune"
|
134
|
+
end
|
135
|
+
|
136
|
+
def build_action_str(cmd)
|
137
|
+
@action_clause += SEPARATOR + cmd.to_s
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
data/lib/easy_find.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'easy_find/default_command_maker'
|
2
|
+
|
3
|
+
module EasyFind
|
4
|
+
|
5
|
+
class Finder
|
6
|
+
BASE_FIND = "find"
|
7
|
+
SEPARATOR = ' '
|
8
|
+
|
9
|
+
def self.find(&block)
|
10
|
+
self.new.find(&block)
|
11
|
+
end
|
12
|
+
|
13
|
+
def initialize(command_maker = DefaultCommandMaker)
|
14
|
+
@folder_clause = ''
|
15
|
+
@where_clause = ''
|
16
|
+
@action_clause = ''
|
17
|
+
@command_maker = DefaultCommandMaker.new
|
18
|
+
end
|
19
|
+
|
20
|
+
def find(&block)
|
21
|
+
command = instance_eval &block
|
22
|
+
if command.nil?
|
23
|
+
BASE_FIND
|
24
|
+
else
|
25
|
+
BASE_FIND + @folder_clause + @where_clause + @action_clause
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def in_folder(&block)
|
30
|
+
@folder_clause += @command_maker.instance_eval { make_folder_name(&block) }
|
31
|
+
end
|
32
|
+
|
33
|
+
def where(&block)
|
34
|
+
@where_clause += @command_maker.instance_eval { make_where_str(&block) }
|
35
|
+
end
|
36
|
+
|
37
|
+
def with_actions(&block)
|
38
|
+
@action_clause += @command_maker.instance_eval { make_actions_str(&block) }
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
metadata
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_find
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yi Wei
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-06-17 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A thin wrapper of *nix find command
|
14
|
+
email:
|
15
|
+
- yiwei.in.cyber@gmail.com
|
16
|
+
executables:
|
17
|
+
- easy_find
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- lib/easy_find/default_command_maker.rb
|
22
|
+
- lib/easy_find/version.rb
|
23
|
+
- lib/easy_find.rb
|
24
|
+
- README.md
|
25
|
+
- LICENSE
|
26
|
+
- Rakefile
|
27
|
+
- bin/easy_find
|
28
|
+
homepage:
|
29
|
+
licenses: []
|
30
|
+
metadata: {}
|
31
|
+
post_install_message:
|
32
|
+
rdoc_options: []
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
|
+
requirements:
|
42
|
+
- - '>='
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: '0'
|
45
|
+
requirements: []
|
46
|
+
rubyforge_project:
|
47
|
+
rubygems_version: 2.0.6
|
48
|
+
signing_key:
|
49
|
+
specification_version: 4
|
50
|
+
summary: A thin wrapper of *nix find command
|
51
|
+
test_files: []
|