feat(cli): add path & limit to session list command (#4878)
This commit is contained in:
@@ -98,9 +98,6 @@ fn parse_key_val(s: &str) -> Result<(String, String), String> {
|
||||
enum SessionCommand {
|
||||
#[command(about = "List all available sessions")]
|
||||
List {
|
||||
#[arg(short, long, help = "List all available sessions")]
|
||||
verbose: bool,
|
||||
|
||||
#[arg(
|
||||
short,
|
||||
long,
|
||||
@@ -115,6 +112,16 @@ enum SessionCommand {
|
||||
long_help = "Sort sessions by date in ascending order (oldest first). Default is descending order (newest first)."
|
||||
)]
|
||||
ascending: bool,
|
||||
|
||||
#[arg(
|
||||
short = 'p',
|
||||
long = "working_dir",
|
||||
help = "Filter sessions by working directory"
|
||||
)]
|
||||
working_dir: Option<PathBuf>,
|
||||
|
||||
#[arg(short = 'l', long = "limit", help = "Limit the number of results")]
|
||||
limit: Option<usize>,
|
||||
},
|
||||
#[command(about = "Remove sessions. Runs interactively if no ID or regex is provided.")]
|
||||
Remove {
|
||||
@@ -182,11 +189,9 @@ enum SchedulerCommand {
|
||||
/// ID of the schedule
|
||||
#[arg(long, help = "ID of the schedule")] // Explicitly make it --id
|
||||
id: String,
|
||||
/// Maximum number of sessions to return
|
||||
#[arg(long, help = "Maximum number of sessions to return")]
|
||||
limit: Option<u32>,
|
||||
limit: Option<usize>,
|
||||
},
|
||||
/// Run a scheduled job immediately
|
||||
#[command(about = "Run a scheduled job immediately")]
|
||||
RunNow {
|
||||
/// ID of the schedule to run
|
||||
@@ -795,11 +800,12 @@ pub async fn cli() -> Result<()> {
|
||||
}) => {
|
||||
return match command {
|
||||
Some(SessionCommand::List {
|
||||
verbose,
|
||||
format,
|
||||
ascending,
|
||||
working_dir,
|
||||
limit,
|
||||
}) => {
|
||||
handle_session_list(verbose, format, ascending).await?;
|
||||
handle_session_list(format, ascending, working_dir, limit).await?;
|
||||
Ok(())
|
||||
}
|
||||
Some(SessionCommand::Remove { id, regex }) => {
|
||||
|
||||
@@ -203,14 +203,14 @@ pub async fn handle_schedule_remove(id: String) -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle_schedule_sessions(id: String, limit: Option<u32>) -> Result<()> {
|
||||
pub async fn handle_schedule_sessions(id: String, limit: Option<usize>) -> Result<()> {
|
||||
let scheduler_storage_path =
|
||||
get_default_scheduler_storage_path().context("Failed to get scheduler storage path")?;
|
||||
let scheduler = SchedulerFactory::create(scheduler_storage_path)
|
||||
.await
|
||||
.context("Failed to initialize scheduler")?;
|
||||
|
||||
match scheduler.sessions(&id, limit.unwrap_or(50) as usize).await {
|
||||
match scheduler.sessions(&id, limit.unwrap_or(50)).await {
|
||||
Ok(sessions) => {
|
||||
if sessions.is_empty() {
|
||||
println!("No sessions found for schedule ID '{}'.", id);
|
||||
|
||||
@@ -114,14 +114,34 @@ pub async fn handle_session_remove(id: Option<String>, regex_string: Option<Stri
|
||||
remove_sessions(matched_sessions).await
|
||||
}
|
||||
|
||||
pub async fn handle_session_list(verbose: bool, format: String, ascending: bool) -> Result<()> {
|
||||
pub async fn handle_session_list(
|
||||
format: String,
|
||||
ascending: bool,
|
||||
working_dir: Option<PathBuf>,
|
||||
limit: Option<usize>,
|
||||
) -> Result<()> {
|
||||
let mut sessions = SessionManager::list_sessions().await?;
|
||||
|
||||
if let Some(ref pat) = working_dir {
|
||||
let pat_lower = pat.to_string_lossy().to_lowercase();
|
||||
sessions.retain(|s| {
|
||||
s.working_dir
|
||||
.to_string_lossy()
|
||||
.to_lowercase()
|
||||
.contains(&pat_lower)
|
||||
});
|
||||
}
|
||||
|
||||
if ascending {
|
||||
sessions.sort_by(|a, b| a.updated_at.cmp(&b.updated_at));
|
||||
} else {
|
||||
sessions.sort_by(|a, b| b.updated_at.cmp(&a.updated_at));
|
||||
}
|
||||
|
||||
if let Some(n) = limit {
|
||||
sessions.truncate(n);
|
||||
}
|
||||
|
||||
match format.as_str() {
|
||||
"json" => {
|
||||
println!("{}", serde_json::to_string(&sessions)?);
|
||||
@@ -138,11 +158,7 @@ pub async fn handle_session_list(verbose: bool, format: String, ascending: bool)
|
||||
"{} - {} - {}",
|
||||
session.id, session.description, session.updated_at
|
||||
);
|
||||
if verbose {
|
||||
println!(" {}", output);
|
||||
} else {
|
||||
println!("{}", output);
|
||||
}
|
||||
println!("{}", output);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user