Skip to content

Commit 3ce09de

Browse files
committed
fix(move): error when target directory exists
Running `rari content move Foo/Bar Something/Else` should fail if `$CONTENT_ROOT/files/en-us/something/else` alreday exists.
1 parent 7de48c0 commit 3ce09de

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

crates/rari-tools/src/error.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::borrow::Cow;
2+
use std::path::PathBuf;
23

34
use rari_doc::error::{DocError, UrlError};
45
use rari_types::error::EnvError;
@@ -55,6 +56,8 @@ pub enum ToolError {
5556
InvalidFrontmatter(#[from] serde_yaml_ng::Error),
5657
#[error("Page has subpages: {0}")]
5758
HasSubpagesError(Cow<'static, str>),
59+
#[error("Target directory ({0}) for slug ({1}) already exists")]
60+
TargetDirExists(PathBuf, String),
5861

5962
#[error("Unknown error")]
6063
Unknown(&'static str),

crates/rari-tools/src/move.rs

+21-10
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ fn do_move(
104104
return Ok(vec![]);
105105
}
106106

107+
let old_folder_path = slug_to_repo_folder_path(real_old_slug, locale)?;
108+
let new_folder_path = slug_to_repo_folder_path(new_slug, locale)?;
109+
110+
if root_for_locale(locale)?
111+
.join(&new_folder_path)
112+
.try_exists()?
113+
{
114+
return Err(ToolError::TargetDirExists(
115+
new_folder_path,
116+
new_slug.to_string(),
117+
));
118+
}
119+
107120
let pairs = [doc.clone()]
108121
.iter()
109122
.chain(&subpages)
@@ -146,16 +159,6 @@ fn do_move(
146159
// to a new location. This will move all children as well and
147160
// makes sure that we get a proper "file moved" in the git history.
148161

149-
let mut old_folder_path = PathBuf::from(locale.as_folder_str());
150-
let url = build_url(real_old_slug, locale, PageCategory::Doc)?;
151-
let UrlMeta { folder_path, .. } = url_meta_from(&url)?;
152-
old_folder_path.push(folder_path);
153-
154-
let mut new_folder_path = PathBuf::from(locale.as_folder_str());
155-
let url = build_url(new_slug, locale, PageCategory::Doc)?;
156-
let UrlMeta { folder_path, .. } = url_meta_from(&url)?;
157-
new_folder_path.push(folder_path);
158-
159162
// Make sure the target parent directory exists.
160163
if let Some(target_parent_path) = new_folder_path.parent() {
161164
let absolute_target_parent_path = root_for_locale(locale)?.join(target_parent_path);
@@ -219,6 +222,14 @@ fn do_move(
219222
Ok(pairs)
220223
}
221224

225+
fn slug_to_repo_folder_path(slug: &str, locale: Locale) -> Result<PathBuf, ToolError> {
226+
let mut new_folder_path = PathBuf::from(locale.as_folder_str());
227+
let url = build_url(slug, locale, PageCategory::Doc)?;
228+
let UrlMeta { folder_path, .. } = url_meta_from(&url)?;
229+
new_folder_path.push(folder_path);
230+
Ok(new_folder_path)
231+
}
232+
222233
fn validate_args(old_slug: &str, new_slug: &str) -> Result<(), ToolError> {
223234
if old_slug.is_empty() {
224235
return Err(ToolError::InvalidSlug(Cow::Borrowed(

0 commit comments

Comments
 (0)