Allow changing current working directory of fd
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rafał Hajdacki · committed by David Peter · parent 32fdbb1 · Browse files · 3 files·+80 added−0 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+80 added−0 removed
- tests:+50 added−0 removed
- other:+30 added−0 removed
Provenance
- Commit
fb205f5- Tree
384da5c- Author
Rafał Hajdacki <rafal@hajdacki.com>- Committer
David Peter <sharkdp@users.noreply.github.com>- Parent
32fdbb1
3 files changed+80 added−0 removed
modified:src/app.rs+13 added
255 unmodified lines256 .long("show-errors")257 .hidden_short_help(true)258 .overrides_with("show-errors"),259 )260+ .arg(261+ arg("base-directory")262+ .long("base-directory")263+ .takes_value(true)264+ .value_name("path")265+ .number_of_values(1)266+ .hidden_short_help(true),267+ )268 .arg(arg("pattern"))269 .arg(270 arg("path-separator")271 .takes_value(true)178 unmodified lines450 doc!(h, "search-path"451 , "(hidden)"452 , "Provide paths to search as an alternative to the positional <path> argument. \453 Changes the usage to `fd [FLAGS/OPTIONS] --search-path <path> --search-path <path2> [<pattern>]`");454+ doc!(h, "base-directory"455+ , "(hidden)"456+ , "Change current working directory of the fd process to provided path. \457+ Non-absolute paths passsed to positional <path> argument or '--search-path' will be \458+ resolved relative to this directory instead of directory where fd was executed.");459 h460 }modified:src/main.rs+17 added
42 unmodified lines43 fn main() {44 let checked_args = transform_args_with_exec(env::args_os());45 let matches = app::build_app().get_matches_from(checked_args);46 47+ // Set the current working directory of the process48+ if let Some(base_directory) = matches.value_of("base-directory") {49+ let basedir = Path::new(base_directory);50+ if !fshelper::is_dir(basedir) {51+ print_error_and_exit!(52+ "The '--base-directory' path ('{}') is not a directory.",53+ basedir.to_string_lossy()54+ );55+ }56+ if let Err(e) = env::set_current_dir(basedir) {57+ print_error_and_exit!(58+ "Could not set '{}' as the current working directory: {}", 59+ basedir.to_string_lossy(), e60+ );61+ }62+ }63+64 // Get the search pattern65 let pattern = matches.value_of("pattern").unwrap_or("");66 67 // Get the current working directory233 unmodified linesmodified:tests/tests.rs+50 added
1348 unmodified lines1349 one=two=three=d.foo1350 one=two=three=directory_foo",1351 );1352 }1353+1354+#[test]1355+fn test_base_directory() {1356+ let te = TestEnv::new(DEFAULT_DIRS, DEFAULT_FILES);1357+1358+ te.assert_output(1359+ &["--base-directory", "one"],1360+ "b.foo1361+ two1362+ two/c.foo1363+ two/C.Foo21364+ two/three1365+ two/three/d.foo1366+ two/three/directory_foo",1367+ );1368+ 1369+ te.assert_output(1370+ &["--base-directory", "one/two", "foo"],1371+ "c.foo1372+ C.Foo21373+ three/d.foo1374+ three/directory_foo",1375+ );1376+1377+ // Explicit root path1378+ te.assert_output(1379+ &["--base-directory", "one", "foo", "two"],1380+ "two/c.foo1381+ two/C.Foo21382+ two/three/d.foo1383+ two/three/directory_foo",1384+ );1385+1386+ // Ignore base directory when absolute path is used1387+ let (te, abs_path) = get_test_env_with_abs_path(DEFAULT_DIRS, DEFAULT_FILES);1388+ let abs_base_dir = &format!("{abs_path}/one/two", abs_path = &abs_path);1389+ te.assert_output(1390+ &["--base-directory", &abs_base_dir, "foo", &abs_path],1391+ &format!(1392+ "{abs_path}/a.foo1393+ {abs_path}/one/b.foo1394+ {abs_path}/one/two/c.foo1395+ {abs_path}/one/two/C.Foo21396+ {abs_path}/one/two/three/d.foo1397+ {abs_path}/one/two/three/directory_foo",1398+ abs_path = &abs_path1399+ ),1400+ );1401+1402+}