Skip to main content

catl/
main.rs

1//! Display file contents or long-list directories.
2//!
3//! # Errors
4//! - Metadata retrieval or command execution fails.
5#![feature(exit_status_error)]
6
7use std::process::Command;
8
9use rootcause::prelude::ResultExt as _;
10use rootcause::report;
11use ytil_sys::cli::Args;
12
13/// Display file contents or long‑list directories.
14#[ytil_sys::main]
15fn main() -> rootcause::Result<()> {
16    let args = ytil_sys::cli::get();
17
18    if args.has_help() {
19        println!("{}", include_str!("../help.txt"));
20        return Ok(());
21    }
22
23    let path = args
24        .first()
25        .ok_or_else(|| report!("missing path arg"))
26        .attach_with(|| format!("args={args:#?}"))?;
27
28    let metadata = std::fs::metadata(path)?;
29
30    if metadata.is_dir() {
31        return Ok(Command::new("ls")
32            .args(["-llAtrh", "--color=always", path])
33            .status()?
34            .exit_ok()?);
35    }
36
37    if metadata.is_file() || metadata.is_symlink() {
38        return Ok(Command::new("cat").args([path]).status()?.exit_ok()?);
39    }
40
41    Err(report!("unsupported file type").attach(format!("path={path:?}")))
42}