aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs73
1 files changed, 53 insertions, 20 deletions
diff --git a/src/main.rs b/src/main.rs
index dd7793f..22c2937 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -293,7 +293,14 @@ fn run_command(command: Commands) -> Result<()> {
293 let show_stdout = !args.stderr || args.stdout; 293 let show_stdout = !args.stderr || args.stdout;
294 let show_stderr = !args.stdout || args.stderr; 294 let show_stderr = !args.stdout || args.stderr;
295 let root_dir = resolve_root_dir(&args.global)?; 295 let root_dir = resolve_root_dir(&args.global)?;
296 tail_logs(&args.id, show_stdout, show_stderr, args.follow, args.lines, &root_dir) 296 tail_logs(
297 &args.id,
298 show_stdout,
299 show_stderr,
300 args.follow,
301 args.lines,
302 &root_dir,
303 )
297 } 304 }
298 Commands::Cat(args) => { 305 Commands::Cat(args) => {
299 let show_stdout = !args.stderr || args.stdout; 306 let show_stdout = !args.stderr || args.stdout;
@@ -326,25 +333,27 @@ fn run_command(command: Commands) -> Result<()> {
326 333
327fn find_git_root() -> Result<PathBuf> { 334fn find_git_root() -> Result<PathBuf> {
328 let mut current = std::env::current_dir()?; 335 let mut current = std::env::current_dir()?;
329 336
330 // Find the git root directory 337 // Find the git root directory
331 let git_root = loop { 338 let git_root = loop {
332 let git_path = current.join(".git"); 339 let git_path = current.join(".git");
333 if git_path.exists() { 340 if git_path.exists() {
334 break current; 341 break current;
335 } 342 }
336 343
337 match current.parent() { 344 match current.parent() {
338 Some(parent) => current = parent.to_path_buf(), 345 Some(parent) => current = parent.to_path_buf(),
339 None => return Err(anyhow::anyhow!( 346 None => {
340 "No git repository found. Please specify --root-dir or run from within a git repository" 347 return Err(anyhow::anyhow!(
341 )), 348 "No git repository found. Please specify --root-dir or run from within a git repository"
349 ));
350 }
342 } 351 }
343 }; 352 };
344 353
345 // Create .demon subdirectory within git root 354 // Create .demon subdirectory within git root
346 let demon_dir = git_root.join(".demon"); 355 let demon_dir = git_root.join(".demon");
347 356
348 // Handle the case where .demon already exists 357 // Handle the case where .demon already exists
349 if demon_dir.exists() { 358 if demon_dir.exists() {
350 if !demon_dir.is_dir() { 359 if !demon_dir.is_dir() {
@@ -356,13 +365,13 @@ fn find_git_root() -> Result<PathBuf> {
356 // .demon exists and is a directory, we can use it 365 // .demon exists and is a directory, we can use it
357 return Ok(demon_dir); 366 return Ok(demon_dir);
358 } 367 }
359 368
360 // Create .demon directory 369 // Create .demon directory
361 std::fs::create_dir(&demon_dir) 370 std::fs::create_dir(&demon_dir)
362 .with_context(|| format!("Failed to create daemon directory {}", demon_dir.display()))?; 371 .with_context(|| format!("Failed to create daemon directory {}", demon_dir.display()))?;
363 372
364 tracing::info!("Created daemon directory: {}", demon_dir.display()); 373 tracing::info!("Created daemon directory: {}", demon_dir.display());
365 374
366 Ok(demon_dir) 375 Ok(demon_dir)
367} 376}
368 377
@@ -370,13 +379,19 @@ fn resolve_root_dir(global: &Global) -> Result<PathBuf> {
370 match &global.root_dir { 379 match &global.root_dir {
371 Some(dir) => { 380 Some(dir) => {
372 if !dir.exists() { 381 if !dir.exists() {
373 return Err(anyhow::anyhow!("Specified root directory does not exist: {}", dir.display())); 382 return Err(anyhow::anyhow!(
383 "Specified root directory does not exist: {}",
384 dir.display()
385 ));
374 } 386 }
375 if !dir.is_dir() { 387 if !dir.is_dir() {
376 return Err(anyhow::anyhow!("Specified root path is not a directory: {}", dir.display())); 388 return Err(anyhow::anyhow!(
389 "Specified root path is not a directory: {}",
390 dir.display()
391 ));
377 } 392 }
378 Ok(dir.clone()) 393 Ok(dir.clone())
379 }, 394 }
380 None => find_git_root(), 395 None => find_git_root(),
381 } 396 }
382} 397}
@@ -428,7 +443,11 @@ fn run_daemon(id: &str, command: &[String], root_dir: &Path) -> Result<()> {
428 // Don't wait for the child - let it run detached 443 // Don't wait for the child - let it run detached
429 std::mem::forget(child); 444 std::mem::forget(child);
430 445
431 println!("Started daemon '{}' with PID written to {}", id, pid_file.display()); 446 println!(
447 "Started daemon '{}' with PID written to {}",
448 id,
449 pid_file.display()
450 );
432 451
433 Ok(()) 452 Ok(())
434} 453}
@@ -820,7 +839,8 @@ fn list_daemons(quiet: bool, root_dir: &Path) -> Result<()> {
820 for entry in find_pid_files(root_dir)? { 839 for entry in find_pid_files(root_dir)? {
821 found_any = true; 840 found_any = true;
822 let path = entry.path(); 841 let path = entry.path();
823 let filename = path.file_name() 842 let filename = path
843 .file_name()
824 .and_then(|name| name.to_str()) 844 .and_then(|name| name.to_str())
825 .unwrap_or_default(); 845 .unwrap_or_default();
826 846
@@ -904,14 +924,22 @@ fn status_daemon(id: &str, root_dir: &Path) -> Result<()> {
904 // Show file information 924 // Show file information
905 if stdout_file.exists() { 925 if stdout_file.exists() {
906 let metadata = std::fs::metadata(&stdout_file)?; 926 let metadata = std::fs::metadata(&stdout_file)?;
907 println!("Stdout file: {} ({} bytes)", stdout_file.display(), metadata.len()); 927 println!(
928 "Stdout file: {} ({} bytes)",
929 stdout_file.display(),
930 metadata.len()
931 );
908 } else { 932 } else {
909 println!("Stdout file: {} (not found)", stdout_file.display()); 933 println!("Stdout file: {} (not found)", stdout_file.display());
910 } 934 }
911 935
912 if stderr_file.exists() { 936 if stderr_file.exists() {
913 let metadata = std::fs::metadata(&stderr_file)?; 937 let metadata = std::fs::metadata(&stderr_file)?;
914 println!("Stderr file: {} ({} bytes)", stderr_file.display(), metadata.len()); 938 println!(
939 "Stderr file: {} ({} bytes)",
940 stderr_file.display(),
941 metadata.len()
942 );
915 } else { 943 } else {
916 println!("Stderr file: {} (not found)", stderr_file.display()); 944 println!("Stderr file: {} (not found)", stderr_file.display());
917 } 945 }
@@ -942,7 +970,8 @@ fn clean_orphaned_files(root_dir: &Path) -> Result<()> {
942 // Find all .pid files in root directory 970 // Find all .pid files in root directory
943 for entry in find_pid_files(root_dir)? { 971 for entry in find_pid_files(root_dir)? {
944 let path = entry.path(); 972 let path = entry.path();
945 let filename = path.file_name() 973 let filename = path
974 .file_name()
946 .and_then(|name| name.to_str()) 975 .and_then(|name| name.to_str())
947 .unwrap_or_default(); 976 .unwrap_or_default();
948 let id = filename.strip_suffix(".pid").unwrap_or(filename); 977 let id = filename.strip_suffix(".pid").unwrap_or(filename);
@@ -1000,7 +1029,11 @@ fn clean_orphaned_files(root_dir: &Path) -> Result<()> {
1000 Err(PidFileReadError::FileInvalid(_)) | Err(PidFileReadError::IoError(_)) => { 1029 Err(PidFileReadError::FileInvalid(_)) | Err(PidFileReadError::IoError(_)) => {
1001 println!("Cleaning up invalid PID file: {}", path.display()); 1030 println!("Cleaning up invalid PID file: {}", path.display());
1002 if let Err(e) = std::fs::remove_file(&path) { 1031 if let Err(e) = std::fs::remove_file(&path) {
1003 tracing::warn!("Failed to remove invalid PID file {}: {}", path.display(), e); 1032 tracing::warn!(
1033 "Failed to remove invalid PID file {}: {}",
1034 path.display(),
1035 e
1036 );
1004 } else { 1037 } else {
1005 tracing::info!("Removed invalid PID file {}", path.display()); 1038 tracing::info!("Removed invalid PID file {}", path.display());
1006 cleaned_count += 1; 1039 cleaned_count += 1;