Issue: handle empty description from user
This fixes issue a26da230276d317e85f9fcca41c19d2e.
This commit is contained in:
parent
bfdf6178f4
commit
211bf92dde
1 changed files with 31 additions and 2 deletions
33
src/issue.rs
33
src/issue.rs
|
|
@ -47,6 +47,8 @@ pub enum IssueError {
|
|||
GitError(#[from] crate::git::GitError),
|
||||
#[error("Failed to run editor")]
|
||||
EditorError,
|
||||
#[error("supplied description is empty")]
|
||||
EmptyDescription,
|
||||
}
|
||||
|
||||
impl FromStr for State {
|
||||
|
|
@ -195,6 +197,9 @@ impl Issue {
|
|||
}
|
||||
|
||||
pub fn set_description(&mut self, description: &str) -> Result<(), IssueError> {
|
||||
if description.len() == 0 {
|
||||
return Err(IssueError::EmptyDescription);
|
||||
}
|
||||
self.description = String::from(description);
|
||||
let mut description_filename = std::path::PathBuf::from(&self.dir);
|
||||
description_filename.push("description");
|
||||
|
|
@ -214,6 +219,7 @@ impl Issue {
|
|||
pub fn edit_description(&mut self) -> Result<(), IssueError> {
|
||||
let mut description_filename = std::path::PathBuf::from(&self.dir);
|
||||
description_filename.push("description");
|
||||
let exists = description_filename.exists();
|
||||
let result = std::process::Command::new("vi")
|
||||
.arg(&description_filename.as_mut_os_str())
|
||||
.spawn()?
|
||||
|
|
@ -223,8 +229,31 @@ impl Issue {
|
|||
println!("stderr: {}", std::str::from_utf8(&result.stderr).unwrap());
|
||||
return Err(IssueError::EditorError);
|
||||
}
|
||||
crate::git::git_commit_file(&description_filename)?;
|
||||
self.read_description()?;
|
||||
if description_filename.exists() && description_filename.metadata()?.len() > 0 {
|
||||
crate::git::add_file(&description_filename)?;
|
||||
} else {
|
||||
// User saved an empty file, which means they changed their
|
||||
// mind and no longer want to edit the description.
|
||||
if exists {
|
||||
crate::git::restore_file(&description_filename)?;
|
||||
}
|
||||
return Err(IssueError::EmptyDescription);
|
||||
}
|
||||
if crate::git::worktree_is_dirty(&self.dir.to_string_lossy())? {
|
||||
crate::git::commit(
|
||||
&description_filename.parent().unwrap(),
|
||||
&format!(
|
||||
"new description for issue {}",
|
||||
description_filename
|
||||
.parent()
|
||||
.unwrap()
|
||||
.file_name()
|
||||
.unwrap()
|
||||
.to_string_lossy()
|
||||
),
|
||||
)?;
|
||||
self.read_description()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue