diff --git a/lib/Radpath/directory.ex b/lib/Radpath/directory.ex index 287ec4e..b4c234e 100644 --- a/lib/Radpath/directory.ex +++ b/lib/Radpath/directory.ex @@ -22,24 +22,37 @@ defmodule Radpath.Dirs do """ - def dirs(path) when (is_bitstring(path) or is_list(path)) do + def dirs(path, regex_dir \\ "") when (is_bitstring(path) or is_list(path)) do file_path = case String.valid? path do true -> [path] false -> path end - do_dirs(file_path, []) + do_dirs(file_path, [], regex_dir) end - defp do_dirs([], result) do + defp do_dirs([], result, regex_dir) do result end - defp do_dirs(paths ,result) do + defp do_dirs(paths ,result, regex_dir) do [h | t] = paths - do_dirs(t, result ++ dirs_list(h)) + do_dirs(t, result ++ dirs_list(h, regex_dir), regex_dir) end - defp dirs_list(path) when is_bitstring(path) do - Finder.new() |> Finder.only_directories() |> Finder.find(Path.expand(path)) |> Enum.to_list + defp dirs_list(path, regex_dir) when is_bitstring(path) do + if regex_dir |> String.length == 0 do + Finder.new() + |> Finder.only_directories() + |> Finder.find(Path.expand(path)) + |> Enum.to_list + |> Enum.sort + else + Finder.new() + |> Finder.with_directory_regex(Regex.compile!(regex_dir)) + |> Finder.only_directories() + |> Finder.find(Path.expand(path)) + |> Enum.to_list + |> Enum.sort + end end end end -end +end diff --git a/test/radpath_test.exs b/test/radpath_test.exs index 05e04c5..16dae60 100644 --- a/test/radpath_test.exs +++ b/test/radpath_test.exs @@ -109,6 +109,11 @@ defmodule RadpathTests.RadpathFacts do dirs = Radpath.dirs(fixture_path) |> Enum.map(&Path.basename(&1)) Enum.each(["testdir3", "testdir2", "testdir1"], fn(x) -> dirs |> contains x end) end + + fact "Test Filtering: Regex Directories" do + dirs = Radpath.dirs("test", "fixtures") |> Enum.map(&Path.basename(&1)) + Enum.each(["fixtures"], fn(x) -> dirs |> contains x end) + end fact "Test Filtering: files" do Radpath.files(fixture_path, "log") |> Enum.map(&Path.basename(&1)) |> ["file3.log"]