Module: Kanrisuru::Core::Find

Extended by:
OsPackage::Define
Defined in:
lib/kanrisuru/core/find.rb,
lib/kanrisuru/core/find/types.rb,
lib/kanrisuru/core/find/constants.rb,
lib/kanrisuru/core/find/parsers/find.rb,
lib/kanrisuru/core/find/commands/find.rb

Defined Under Namespace

Modules: Parser Classes: FilePath

Constant Summary collapse

REGEX_TYPES =
%w[emacs posix-awk posix-basic posix-egrep posix-extended].freeze

Instance Method Summary collapse

Methods included from OsPackage::Define

extended, os_define

Instance Method Details

#find(opts = {}) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/kanrisuru/core/find/commands/find.rb', line 6

def find(opts = {})
  paths      = opts[:paths]
  type       = opts[:type]
  regex      = opts[:regex]
  size       = opts[:size]

  command = Kanrisuru::Command.new('find')

  case opts[:follow]
  when 'never'
    command.append_flag('-P')
  when 'always'
    command.append_flag('-L')
  when 'command'
    command.append_flag('-H')
  end

  if Kanrisuru::Util.present?(paths)
    raise ArgumentError, 'Invalid paths type' unless [String, Array].include?(paths.class)

    command.append_array(paths)
  end

  command.append_flag('-executable', opts[:executable])
  command.append_flag('-empty', opts[:empty])
  command.append_flag('-readable', opts[:readable])
  command.append_flag('-writable', opts[:writable])
  command.append_flag('-nogroup', opts[:nogroup])
  command.append_flag('-nouser', opts[:nouser])
  command.append_flag('-mount', opts[:mount])

  command.append_arg('-path', opts[:path])
  command.append_arg('-name', opts[:name])
  command.append_arg('-iname', opts[:iname])
  command.append_arg('-gid', opts[:gid])
  command.append_arg('-uid', opts[:uid])
  command.append_arg('-user', opts[:user])
  command.append_arg('-group', opts[:group])
  command.append_arg('-inum', opts[:inode])
  command.append_arg('-links', opts[:links])
  command.append_arg('-maxdepth', opts[:maxdepth])
  command.append_arg('-mindepth', opts[:mindepth])

  command.append_arg('-atime', opts[:atime])
  command.append_arg('-ctime', opts[:ctime])
  command.append_arg('-mtime', opts[:mtime])
  command.append_arg('-amin', opts[:amin])
  command.append_arg('-cmin', opts[:cmin])
  command.append_arg('-mmin', opts[:mmin])

  if Kanrisuru::Util.present?(opts[:regex_type])
    raise ArgumentError, 'invalid regex type' unless REGEX_TYPES.include?(opts[:regex_type])

    command.append_arg('-regextype', opts[:regex_type])
  end

  command.append_arg('-regex', "'#{opts[:regex]}'") if Kanrisuru::Util.present?(opts[:regex])
  command.append_arg('-iregex', "'#{opts[:iregex]}'") if Kanrisuru::Util.present?(opts[:iregex])

  if size.instance_of?(String)
    regex = Regexp.new(/^([-+])?(\d+)([bcwkMG])*$/)
    raise ArgumentError, "invalid size string: '#{@size}'" unless regex.match?(size)

    command.append_arg('-size', size)
  elsif size.instance_of?(Integer)
    command.append_arg('-size', size)
  end

  case type
  when 'directory'
    command.append_arg('-type', 'd')
  when 'file'
    command.append_arg('-type', 'f')
  when 'symlinks'
    command.append_arg('-type', 'l')
  when 'block'
    command.append_arg('-type', 'b')
  when 'character'
    command.append_arg('-type', 'c')
  when 'pipe'
    command.append_arg('-type', 'p')
  when 'socket'
    command.append_arg('-type', 's')
  end

  execute_shell(command)

  Kanrisuru::Result.new(command) do |cmd|
    Parser::Find.parse(cmd)
  end
end