Skip to content

Commit 962e244

Browse files
committed
handled file creation without duplicates
Signed-off-by: Aminu 'Seun Joshua <[email protected]>
1 parent 1edd2d5 commit 962e244

File tree

1 file changed

+17
-35
lines changed

1 file changed

+17
-35
lines changed

crates/trigger/src/cli/stdio.rs

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ impl StdioLoggingExecutorHooks {
6565
log_dir: Option<&Path>,
6666
) -> Result<ComponentStdioWriter> {
6767
let sanitized_component_id = sanitize_filename::sanitize(component_id);
68-
let log_path = sanitized_log_path(log_dir, &sanitized_component_id, log_suffix);
68+
let log_path = log_dir
69+
.map(|log_dir| log_dir.join(format!("{sanitized_component_id}_{log_suffix}.txt",)));
6970
let log_path = log_path.as_deref();
7071

7172
let follow = self.follow_components.should_follow(component_id);
@@ -95,31 +96,20 @@ impl StdioLoggingExecutorHooks {
9596
}
9697
}
9798

98-
fn truncate_log_files<F: RuntimeFactors>(
99-
&self,
100-
configured_app: &spin_factors::ConfiguredApp<F>,
101-
) {
102-
let sanitized_component_ids: Vec<String> = configured_app
103-
.app()
104-
.components()
105-
.map(|c| sanitize_filename::sanitize(c.locked.id.clone()))
106-
.collect();
107-
108-
for sanitized_component_id in sanitized_component_ids {
109-
if let Some(stdout_log_path) = sanitized_log_path(
110-
self.log_dir.as_deref(),
111-
&sanitized_component_id,
112-
STDOUT_LOG_FILE_SUFFIX,
113-
) {
114-
_ = std::fs::File::create(stdout_log_path);
115-
}
116-
117-
if let Some(stderr_log_path) = sanitized_log_path(
118-
self.log_dir.as_deref(),
119-
&sanitized_component_id,
120-
STDERR_LOG_FILE_SUFFIX,
121-
) {
122-
_ = std::fs::File::create(stderr_log_path);
99+
fn truncate_log_files(&self, log_dir: &Option<PathBuf>) {
100+
let Some(dir) = log_dir else { return };
101+
if let Ok(entries) = dir.read_dir() {
102+
for entry in entries.flatten() {
103+
let path = entry.path();
104+
let Some(name) = path.file_name().and_then(|n| n.to_str()) else {
105+
continue;
106+
};
107+
108+
if name.ends_with(&format!("{STDOUT_LOG_FILE_SUFFIX}.txt"))
109+
|| name.ends_with(&format!("{STDERR_LOG_FILE_SUFFIX}.txt"))
110+
{
111+
let _ = std::fs::File::create(dir.join(name));
112+
}
123113
}
124114
}
125115
}
@@ -139,7 +129,7 @@ impl<F: RuntimeFactors, U> ExecutorHooks<F, U> for StdioLoggingExecutorHooks {
139129
.with_context(|| format!("Failed to create log dir {}", quoted_path(dir)))?;
140130

141131
if self.truncate_log {
142-
self.truncate_log_files(configured_app);
132+
self.truncate_log_files(&self.log_dir);
143133
}
144134

145135
println!("Logging component stdio to {}", quoted_path(dir.join("")))
@@ -370,11 +360,3 @@ fn bullet_list<S: std::fmt::Display>(items: impl IntoIterator<Item = S>) -> Stri
370360
.collect::<Vec<_>>()
371361
.join("\n")
372362
}
373-
374-
fn sanitized_log_path(
375-
log_dir: Option<&Path>,
376-
sanitized_component_id: &str,
377-
log_suffix: &str,
378-
) -> Option<PathBuf> {
379-
log_dir.map(|log_dir| log_dir.join(format!("{sanitized_component_id}_{log_suffix}.txt",)))
380-
}

0 commit comments

Comments
 (0)