Skip to content

Commit

Permalink
Allow changing current working directory of fd
Browse files Browse the repository at this point in the history
  • Loading branch information
hajdamak committed Dec 10, 2019
1 parent 19e5d6e commit 1e81907
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,14 @@ pub fn build_app() -> App<'static, 'static> {
.hidden_short_help(true)
.overrides_with("show-errors"),
)
.arg(
arg("base-directory")
.long("base-directory")
.takes_value(true)
.value_name("path")
.number_of_values(1)
.hidden_short_help(true),
)
.arg(arg("pattern"))
.arg(
arg("path-separator")
Expand Down Expand Up @@ -443,5 +451,8 @@ fn usage() -> HashMap<&'static str, Help> {
, "(hidden)"
, "Provide paths to search as an alternative to the positional <path> argument. \
Changes the usage to `fd [FLAGS/OPTIONS] --search-path <path> --search-path <path2> [<pattern>]`");
doc!(h, "base-directory"
, "(hidden)"
, "Change current working directory of the fd process to provided path.");
h
}
17 changes: 17 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,23 @@ fn main() {
let checked_args = transform_args_with_exec(env::args_os());
let matches = app::build_app().get_matches_from(checked_args);

// Set the current working directory of the process
if let Some(base_directory) = matches.value_of("base-directory") {
let basedir = Path::new(base_directory);
if !fshelper::is_dir(basedir) {
print_error_and_exit!(
"'{}' passed as '--base-directory' value is not a directory.",
basedir.to_string_lossy()
);
}
if let Err(e) = env::set_current_dir(basedir) {
print_error_and_exit!(
"Cannot set '{}' as current working directory: {}",
basedir.to_string_lossy(), e
);
}
}

// Get the search pattern
let pattern = matches.value_of("pattern").unwrap_or("");

Expand Down
50 changes: 50 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1350,3 +1350,53 @@ fn test_custom_path_separator() {
one=two=three=directory_foo",
);
}

#[test]
fn test_base_directory() {
let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);

te.assert_output(
&["--base-directory", "one"],
"b.foo
two
two/c.foo
two/C.Foo2
two/three
two/three/d.foo
two/three/directory_foo",
);

te.assert_output(
&["--base-directory", "one/two", "foo"],
"c.foo
C.Foo2
three/d.foo
three/directory_foo",
);

// Explicit root path
te.assert_output(
&["--base-directory", "one", "foo", "two"],
"two/c.foo
two/C.Foo2
two/three/d.foo
two/three/directory_foo",
);

// Ignore base directory when aboslute path is used
let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);
let abs_base_dir = &format!("{abs_path}/one/two", abs_path = &abs_path);
te.assert_output(
&["--base-directory", &abs_base_dir, "foo", &abs_path],
&format!(
"{abs_path}/a.foo
{abs_path}/one/b.foo
{abs_path}/one/two/c.foo
{abs_path}/one/two/C.Foo2
{abs_path}/one/two/three/d.foo
{abs_path}/one/two/three/directory_foo",
abs_path = &abs_path
),
);

}

0 comments on commit 1e81907

Please sign in to comment.