use std::process::ExitCode;
use clap::ColorChoice;
use clap::Parser;
use clap::ValueEnum;
use mago_database::DatabaseReader;
use crate::config::Configuration;
use crate::error::Error;
use crate::utils::create_orchestrator;
#[derive(ValueEnum, Debug, Clone, Copy)]
#[value(rename_all = "kebab-case")]
enum Command {
Linter,
Formatter,
Analyzer,
Guard,
}
#[derive(Parser, Debug)]
#[command(
name = "list-files",
about = "Display all files that will be scanned.",
long_about = "Display a list of all files that will be scanned by Mago.\n\n\
This can be useful if you want to see which files you might still need\n\
to include or exclude in your configuration."
)]
pub struct ListFilesCommand {
#[arg(long, value_enum)]
command: Option<Command>,
#[arg(long, short = '0', default_value_t = false)]
zero_terminate: bool,
}
impl ListFilesCommand {
pub fn execute(self, configuration: Configuration, color_choice: ColorChoice) -> Result<ExitCode, Error> {
let mut orchestrator = create_orchestrator(&configuration, color_choice, false, true, false);
if let Some(command) = self.command {
match command {
Command::Linter => orchestrator.add_exclude_patterns(configuration.linter.excludes.iter()),
Command::Formatter => orchestrator.add_exclude_patterns(configuration.formatter.excludes.iter()),
Command::Analyzer => orchestrator.add_exclude_patterns(configuration.analyzer.excludes.iter()),
Command::Guard => orchestrator.add_exclude_patterns(configuration.guard.excludes.iter()),
}
}
let database = orchestrator.load_database(&configuration.source.workspace, false, None, None)?;
for file in database.files() {
print!("{}{}", file.name, if self.zero_terminate { '\0' } else { '\n' });
}
Ok(ExitCode::SUCCESS)
}
}