Skip to main content

systemprompt_cli/commands/admin/config/
list.rs

1//! `admin config list` command.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use clap::Args;
7
8use super::types::{ConfigFileInfo, ConfigListOutput, ConfigSection, read_yaml_file};
9use crate::CliConfig;
10use crate::shared::CommandOutput;
11
12#[derive(Debug, Clone, Copy, Args)]
13pub struct ListArgs {
14    #[arg(long, short = 'e')]
15    pub errors_only: bool,
16}
17
18pub fn execute(args: ListArgs, _config: &CliConfig) -> CommandOutput {
19    let mut files = Vec::new();
20    let mut valid_count = 0;
21    let mut invalid_count = 0;
22
23    for section in ConfigSection::all() {
24        let section_files = match section.all_files() {
25            Ok(f) => f,
26            Err(e) => {
27                tracing::debug!(section = %section, error = %e, "Failed to list config files for section");
28                continue;
29            },
30        };
31
32        for file_path in section_files {
33            let exists = file_path.exists();
34            let (valid, error) = if exists {
35                match read_yaml_file(&file_path) {
36                    Ok(_) => (true, None),
37                    Err(e) => (false, Some(e.to_string())),
38                }
39            } else {
40                (false, Some("File not found".to_owned()))
41            };
42
43            if valid {
44                valid_count += 1;
45            } else {
46                invalid_count += 1;
47            }
48
49            if args.errors_only && valid {
50                continue;
51            }
52
53            files.push(ConfigFileInfo {
54                path: file_path.display().to_string(),
55                section: section.to_string(),
56                exists,
57                valid,
58                error,
59            });
60        }
61    }
62
63    let output = ConfigListOutput {
64        total: valid_count + invalid_count,
65        valid: valid_count,
66        invalid: invalid_count,
67        files,
68    };
69
70    CommandOutput::table_of(
71        vec!["path", "section", "exists", "valid", "error"],
72        &output.files,
73    )
74    .with_title("Configuration Files")
75}