Skip to main content

sbom_tools/cli/
watch.rs

1//! CLI handler for the `watch` subcommand.
2
3use crate::config::WatchConfig;
4use crate::pipeline::exit_codes;
5use crate::watch::WatchError;
6use anyhow::Result;
7
8/// Run the watch command with the given configuration.
9///
10/// Exits the process with [`exit_codes::CHANGES_DETECTED`] (1) when the loop
11/// stops because `--exit-on-change` observed a change; returns `Ok(())` (exit
12/// 0) on a clean shutdown. Operational errors propagate as `Err` for `main()`
13/// to map to exit 3. The gate exit mirrors how `main()` handles the other
14/// verdict gates (`process::exit` on a non-zero code from the handler).
15pub fn run_watch(config: WatchConfig) -> Result<()> {
16    // Validate that all watch directories exist
17    for dir in &config.watch_dirs {
18        if !dir.is_dir() {
19            return Err(WatchError::DirNotFound(dir.clone()).into());
20        }
21    }
22
23    let exit_code = crate::watch::run_watch_loop(&config)?;
24    if exit_code != exit_codes::SUCCESS {
25        std::process::exit(exit_code);
26    }
27    Ok(())
28}