fix: handle --clear flag in console command (#1015)

The console and errors commands parsed --clear from CLI args but the
action handlers silently ignored the flag. The handlers did not accept
the cmd parameter so they had no way to read the clear field.

Changes:
- Add clear_console() method to EventTracker in network.rs
- Update handle_console to accept cmd, read the clear field, and clear
  the buffer when --clear is passed (returns {cleared: true})
- Update call site in execute_command to pass cmd

Co-authored-by: xuyongliang <yongliang.xyl@alibaba-inc.com>
This commit is contained in:
xuyongliang
2026-03-25 08:27:28 -07:00
committed by GitHub
co-authored by xuyongliang
parent 7d2cd726ec
commit 8c6fc35450
3 changed files with 16 additions and 4 deletions
+10 -3
View File
@@ -1076,7 +1076,7 @@ pub async fn execute_command(cmd: &Value, state: &mut DaemonState) -> Value {
"setcontent" => handle_setcontent(cmd, state).await,
"headers" => handle_headers(cmd, state).await,
"offline" => handle_offline(cmd, state).await,
"console" => handle_console(state).await,
"console" => handle_console(cmd, state).await,
"errors" => handle_errors(state).await,
"state_save" => handle_state_save(cmd, state).await,
"state_load" => handle_state_load(cmd, state).await,
@@ -2887,8 +2887,15 @@ async fn handle_offline(cmd: &Value, state: &DaemonState) -> Result<Value, Strin
Ok(json!({ "offline": offline }))
}
async fn handle_console(state: &DaemonState) -> Result<Value, String> {
Ok(state.event_tracker.get_console_json())
async fn handle_console(cmd: &Value, state: &mut DaemonState) -> Result<Value, String> {
let clear = cmd.get("clear").and_then(|v| v.as_bool()).unwrap_or(false);
if clear {
state.event_tracker.clear_console();
Ok(json!({ "cleared": true }))
} else {
let result = state.event_tracker.get_console_json();
Ok(result)
}
}
async fn handle_errors(state: &DaemonState) -> Result<Value, String> {
+4
View File
@@ -322,6 +322,10 @@ impl EventTracker {
});
}
pub fn clear_console(&mut self) {
self.console_entries.clear();
}
pub fn get_console_json(&self) -> Value {
let messages: Vec<Value> = self
.console_entries
+2 -1
View File
@@ -430,11 +430,12 @@ pub fn print_response_with_opts(resp: &Response, action: Option<&str>, opts: &Ou
}
return;
}
// Cleared (cookies or request log)
// Cleared (cookies, console, or request log)
if let Some(cleared) = data.get("cleared").and_then(|v| v.as_bool()) {
if cleared {
let label = match action {
Some("cookies_clear") => "Cookies cleared",
Some("console") => "Console log cleared",
_ => "Request log cleared",
};
println!("{} {}", color::success_indicator(), label);