vdev/commands/check/
examples.rs

1use std::fs;
2
3use anyhow::{bail, Context, Result};
4
5use crate::app;
6
7/// Check that the config/example files are valid
8#[derive(clap::Args, Debug)]
9#[command()]
10pub struct Cli {}
11
12const EXAMPLES: &str = "config/examples";
13
14impl Cli {
15    pub fn exec(self) -> Result<()> {
16        app::set_repo_dir()?;
17
18        for entry in fs::read_dir(EXAMPLES)
19            .with_context(|| format!("Could not read directory {EXAMPLES:?}"))?
20        {
21            let entry = entry.with_context(|| format!("Could not read entry from {EXAMPLES:?}"))?;
22            let filename = entry.path();
23            let Some(filename) = filename.as_os_str().to_str() else {
24                bail!("Invalid filename {filename:?}");
25            };
26
27            let mut command = vec![
28                "run",
29                "--",
30                "validate",
31                "--deny-warnings",
32                "--no-environment",
33            ];
34            if entry
35                .metadata()
36                .with_context(|| format!("Could not get metadata of {filename:?}"))?
37                .is_dir()
38            {
39                command.push("--config-dir");
40            }
41            command.push(filename);
42
43            app::exec("cargo", command, true)?;
44        }
45
46        Ok(())
47    }
48}