Skip to content

Commit d28f439

Browse files
author
Greyforge Admin
committed
Filter logs paths to log files
1 parent 7954d02 commit d28f439

2 files changed

Lines changed: 55 additions & 6 deletions

File tree

src/cortex-cli/src/logs_cmd.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use anyhow::Result;
1010
use clap::Parser;
1111
use serde::Serialize;
12-
use std::path::PathBuf;
12+
use std::path::{Path, PathBuf};
1313

1414
/// Logs CLI command.
1515
#[derive(Debug, Parser)]
@@ -81,6 +81,12 @@ fn format_size(bytes: u64) -> String {
8181
}
8282
}
8383

84+
fn is_log_file(path: &Path) -> bool {
85+
path.file_name()
86+
.and_then(|n| n.to_str())
87+
.is_some_and(|name| name.ends_with(".log") || name.ends_with(".txt"))
88+
}
89+
8490
impl LogsCli {
8591
/// Run the logs command.
8692
pub async fn run(self) -> Result<()> {
@@ -113,11 +119,8 @@ impl LogsCli {
113119
if let Ok(entries) = std::fs::read_dir(&logs_dir) {
114120
for entry in entries.flatten() {
115121
let path = entry.path();
116-
if path.is_file() {
117-
let name = path.file_name().and_then(|n| n.to_str()).unwrap_or("");
118-
if name.ends_with(".log") || name.ends_with(".txt") {
119-
log_files.push(path);
120-
}
122+
if path.is_file() && is_log_file(&path) {
123+
log_files.push(path);
121124
}
122125
}
123126
}
@@ -266,6 +269,7 @@ impl LogsCli {
266269
for entry in entries.flatten() {
267270
let path = entry.path();
268271
if path.is_file()
272+
&& is_log_file(&path)
269273
&& let Ok(meta) = entry.metadata()
270274
{
271275
let modified = meta

src/cortex-cli/tests/logs_paths.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use std::fs;
2+
use std::process::Command;
3+
4+
use tempfile::tempdir;
5+
6+
fn combined_output(output: &std::process::Output) -> String {
7+
format!(
8+
"{}{}",
9+
String::from_utf8_lossy(&output.stdout),
10+
String::from_utf8_lossy(&output.stderr)
11+
)
12+
}
13+
14+
#[test]
15+
fn logs_paths_lists_only_log_files() {
16+
let home = tempdir().unwrap();
17+
let cache = tempdir().unwrap();
18+
let logs_dir = cache.path().join("cortex").join("logs");
19+
fs::create_dir_all(&logs_dir).unwrap();
20+
21+
fs::write(logs_dir.join("cortex.log"), "log\n").unwrap();
22+
fs::write(logs_dir.join("debug.txt"), "debug\n").unwrap();
23+
fs::write(logs_dir.join("state.json"), "{}\n").unwrap();
24+
fs::write(logs_dir.join("session.lock"), "locked\n").unwrap();
25+
26+
let output = Command::new(env!("CARGO_BIN_EXE_Cortex"))
27+
.args(["logs", "--paths"])
28+
.env("HOME", home.path())
29+
.env("XDG_CACHE_HOME", cache.path())
30+
.env_remove("CORTEX_HOME")
31+
.output()
32+
.unwrap();
33+
34+
assert!(
35+
output.status.success(),
36+
"logs --paths failed:\n{}",
37+
combined_output(&output)
38+
);
39+
40+
let stdout = String::from_utf8_lossy(&output.stdout);
41+
assert!(stdout.contains("cortex.log"), "output:\n{stdout}");
42+
assert!(stdout.contains("debug.txt"), "output:\n{stdout}");
43+
assert!(!stdout.contains("state.json"), "output:\n{stdout}");
44+
assert!(!stdout.contains("session.lock"), "output:\n{stdout}");
45+
}

0 commit comments

Comments
 (0)