feat: preview to-be-removed session (#2306)
This commit is contained in:
@@ -3,20 +3,27 @@ use goose::session::info::{get_session_info, SessionInfo, SortOrder};
|
|||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
pub fn remove_session(session: &SessionInfo) -> Result<()> {
|
pub fn remove_sessions(sessions: Vec<SessionInfo>) -> Result<()> {
|
||||||
let should_delete = cliclack::confirm(format!(
|
println!("The following sessions will be removed:");
|
||||||
"Are you sure you want to delete session `{}`? (yes/no):?",
|
for session in &sessions {
|
||||||
session.id
|
println!("- {}", session.id);
|
||||||
))
|
|
||||||
.initial_value(true)
|
|
||||||
.interact()?;
|
|
||||||
if should_delete {
|
|
||||||
fs::remove_file(session.path.clone())
|
|
||||||
.with_context(|| format!("Failed to remove session file '{}'", session.path))?;
|
|
||||||
println!("Session `{}` removed.", session.id);
|
|
||||||
} else {
|
|
||||||
println!("Skipping deletion of '{}'.", session.id);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let should_delete =
|
||||||
|
cliclack::confirm("Are you sure you want to delete all these sessions? (yes/no):")
|
||||||
|
.initial_value(true)
|
||||||
|
.interact()?;
|
||||||
|
|
||||||
|
if should_delete {
|
||||||
|
for session in sessions {
|
||||||
|
fs::remove_file(session.path.clone())
|
||||||
|
.with_context(|| format!("Failed to remove session file '{}'", session.path))?;
|
||||||
|
println!("Session `{}` removed.", session.id);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
println!("Skipping deletion of the sessions.");
|
||||||
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,37 +31,38 @@ pub fn handle_session_remove(id: String, regex_string: String) -> Result<()> {
|
|||||||
let sessions = match get_session_info(SortOrder::Descending) {
|
let sessions = match get_session_info(SortOrder::Descending) {
|
||||||
Ok(sessions) => sessions,
|
Ok(sessions) => sessions,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
tracing::error!("Failed to remove sessions: {:?}", e);
|
tracing::error!("Failed to retrieve sessions: {:?}", e);
|
||||||
return Err(anyhow::anyhow!("Failed to remove sessions"));
|
return Err(anyhow::anyhow!("Failed to retrieve sessions"));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let matched_sessions: Vec<SessionInfo>;
|
||||||
if !id.is_empty() {
|
if !id.is_empty() {
|
||||||
if let Some(session_to_remove) = sessions.iter().find(|s| s.id == id) {
|
if let Some(session) = sessions.iter().find(|s| s.id == id) {
|
||||||
remove_session(session_to_remove)?;
|
matched_sessions = vec![session.clone()];
|
||||||
} else {
|
} else {
|
||||||
return Err(anyhow::anyhow!("Session '{}' not found.", id));
|
return Err(anyhow::anyhow!("Session '{}' not found.", id));
|
||||||
}
|
}
|
||||||
} else if !regex_string.is_empty() {
|
} else if !regex_string.is_empty() {
|
||||||
let session_regex: Regex = Regex::new(regex_string.as_str())?;
|
let session_regex = Regex::new(®ex_string)
|
||||||
let mut removed_count = 0;
|
.with_context(|| format!("Invalid regex pattern '{}'", regex_string))?;
|
||||||
for session_info in sessions {
|
matched_sessions = sessions
|
||||||
if session_regex.is_match(session_info.id.as_str()) {
|
.into_iter()
|
||||||
remove_session(&session_info)?;
|
.filter(|session| session_regex.is_match(&session.id))
|
||||||
removed_count += 1;
|
.collect();
|
||||||
}
|
|
||||||
}
|
if matched_sessions.is_empty() {
|
||||||
if removed_count == 0 {
|
|
||||||
println!(
|
println!(
|
||||||
"Regex string '{}' does not match any sessions",
|
"Regex string '{}' does not match any sessions",
|
||||||
regex_string
|
regex_string
|
||||||
);
|
);
|
||||||
|
return Ok(());
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return Err(anyhow::anyhow!(
|
return Err(anyhow::anyhow!("Neither --regex nor --id flags provided."));
|
||||||
"Neither --regex nor --session-name flags provided."
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
Ok(())
|
|
||||||
|
remove_sessions(matched_sessions)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn handle_session_list(verbose: bool, format: String, ascending: bool) -> Result<()> {
|
pub fn handle_session_list(verbose: bool, format: String, ascending: bool) -> Result<()> {
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use std::cmp::Ordering;
|
|||||||
|
|
||||||
use crate::session::{self, SessionMetadata};
|
use crate::session::{self, SessionMetadata};
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Clone, Serialize)]
|
||||||
pub struct SessionInfo {
|
pub struct SessionInfo {
|
||||||
pub id: String,
|
pub id: String,
|
||||||
pub path: String,
|
pub path: String,
|
||||||
|
|||||||
Reference in New Issue
Block a user