Skip to content

Commit b507040

Browse files
committed
feat: add validation for rename of current directory
1 parent e9f2b44 commit b507040

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

src/workflows/rename_project/interactive.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use inquire::{validator::Validation, CustomUserError, Text};
44
use regex::Regex;
55

66
use super::Params;
7+
use indoc::indoc;
78

89
pub fn get_params_from_user() -> Result<Params, String> {
910
let project_root = get_project_root_from_user()?;
@@ -18,6 +19,7 @@ fn get_project_root_from_user() -> Result<PathBuf, String> {
1819
Text::new("Project root directory path:")
1920
.with_validator(validate_project_root_is_not_special)
2021
.with_validator(validate_project_root_is_dir)
22+
.with_validator(validate_project_root_is_not_current_dir)
2123
.with_validator(validate_project_root_contains_project_descriptor)
2224
.prompt()
2325
.map(|project_root| PathBuf::from(project_root))
@@ -44,6 +46,26 @@ fn validate_project_root_is_dir(project_root: &str) -> Result<Validation, Custom
4446
}
4547
}
4648

49+
fn validate_project_root_is_not_current_dir(
50+
project_root: &str,
51+
) -> Result<Validation, CustomUserError> {
52+
let project_root = PathBuf::from(project_root);
53+
let current_dir = std::env::current_dir().map_err(|err| err.to_string())?;
54+
let abs_current_dir = fs::canonicalize(current_dir).map_err(|err| err.to_string())?;
55+
let abs_project_root = fs::canonicalize(project_root).map_err(|err| err.to_string())?;
56+
57+
if abs_project_root == abs_current_dir {
58+
let error_message = indoc! {"
59+
Provided directory is the current directory which is protected
60+
Move to the parent directory and try again\
61+
"};
62+
63+
return Ok(Validation::Invalid(error_message.into()));
64+
}
65+
66+
Ok(Validation::Valid)
67+
}
68+
4769
fn validate_project_root_contains_project_descriptor(
4870
project_root: &str,
4971
) -> Result<Validation, CustomUserError> {

src/workflows/rename_project/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ pub fn rename_project(params: Params) -> Result<(), String> {
5959
fn validate_params(params: &Params) -> Result<(), String> {
6060
validate_project_root_is_not_special(&params.project_root)?;
6161
validate_project_root_is_dir(&params.project_root)?;
62+
validate_project_root_is_not_current_dir(&params.project_root)?;
6263
validate_project_root_contains_project_descriptor(&params.project_root)?;
6364
let project_name = detect_project_name(&params.project_root)?;
6465
validate_new_name_is_not_empty(&params.new_name)?;
@@ -83,6 +84,18 @@ fn validate_project_root_is_dir(project_root: &Path) -> Result<(), String> {
8384
}
8485
}
8586

87+
fn validate_project_root_is_not_current_dir(project_root: &Path) -> Result<(), String> {
88+
let current_dir = std::env::current_dir().map_err(|err| err.to_string())?;
89+
let abs_current_dir = fs::canonicalize(current_dir).map_err(|err| err.to_string())?;
90+
let abs_project_root = fs::canonicalize(project_root).map_err(|err| err.to_string())?;
91+
92+
if abs_project_root == abs_current_dir {
93+
return Err("project root cannot be current directory".into());
94+
}
95+
96+
Ok(())
97+
}
98+
8699
fn validate_project_root_contains_project_descriptor(project_root: &Path) -> Result<(), String> {
87100
match fs::read_dir(&project_root)
88101
.map_err(|err| err.to_string())?

0 commit comments

Comments
 (0)