1use anyhow::{Context, Result};
2use clap::{Parser, Subcommand};
3use no_mistakes_core::cli::Format;
4use no_mistakes_core::react_traits;
5use std::path::PathBuf;
6use std::process::ExitCode;
7
8#[derive(Parser)]
9#[command(author, version, about)]
10pub(crate) struct Cli {
11 #[arg(long, default_value = ".", global = true)]
13 pub(crate) root: PathBuf,
14 #[arg(long, global = true)]
16 pub(crate) config: Option<PathBuf>,
17 #[arg(
19 long,
20 value_enum,
21 default_value = "human",
22 global = true,
23 conflicts_with = "json"
24 )]
25 pub(crate) format: Format,
26 #[arg(long, global = true, conflicts_with = "format")]
28 pub(crate) json: bool,
29 #[command(subcommand)]
30 pub(crate) command: Command,
31}
32
33#[derive(Subcommand)]
34pub(crate) enum Command {
35 Analyze {
37 #[arg(help = "Glob patterns for component files")]
38 targets: Vec<String>,
39 },
40 Check {
42 #[arg(help = "Glob patterns for component files")]
43 targets: Vec<String>,
44 #[arg(long)]
46 assert_no_fetch: bool,
47 },
48}
49
50fn parse_cli_args() -> Cli {
51 if cfg!(test) {
52 let raw_args = std::env::var("REACT_TRAITS_TEST_ARGS")
53 .expect("REACT_TRAITS_TEST_ARGS must be set in tests - use with_run_args_env()");
54 Cli::parse_from(raw_args.split('\u{1f}'))
55 } else {
56 Cli::parse()
57 }
58}
59
60pub fn run_cli() -> Result<ExitCode> {
61 let cli = parse_cli_args();
62 let base_root = std::env::current_dir().context("reading current directory")?;
63 let root = base_root.join(&cli.root);
64 let format = if cli.json { Format::Json } else { cli.format };
65 match &cli.command {
66 Command::Analyze { targets } => {
67 let results = react_traits::run_analyze(&root, cli.config.as_deref(), targets, None)?;
68 match format {
69 Format::Json => {
70 println!(
71 "{}",
72 serde_json::to_string_pretty(&results)
73 .context("serializing analysis results")?
74 );
75 }
76 Format::Yml => {
77 println!(
78 "{}",
79 serde_yaml::to_string(&results)
80 .context("serializing analysis results to YAML")?
81 );
82 }
83 Format::Md => react_traits::print_results_md(&results),
84 Format::Paths => {
85 for result in &results {
86 println!("{}", result.file);
87 }
88 }
89 Format::Human => react_traits::print_results(&results, 0),
90 }
91 Ok(ExitCode::SUCCESS)
92 }
93 Command::Check {
94 targets,
95 assert_no_fetch,
96 } => {
97 let violations =
98 react_traits::run_check(&root, cli.config.as_deref(), targets, *assert_no_fetch)?;
99 if violations.is_empty() {
100 Ok(ExitCode::SUCCESS)
101 } else {
102 match format {
103 Format::Json => {
104 println!(
105 "{}",
106 serde_json::to_string_pretty(&violations)
107 .context("serializing check violations")?
108 );
109 }
110 Format::Yml => {
111 println!(
112 "{}",
113 serde_yaml::to_string(&violations)
114 .context("serializing check violations to YAML")?
115 );
116 }
117 Format::Md => react_traits::print_violations_md(&violations),
118 Format::Paths => {
119 for violation in &violations {
120 println!("{}", violation.file);
121 }
122 }
123 Format::Human => react_traits::print_violations(&violations),
124 }
125 Ok(ExitCode::from(1))
126 }
127 }
128 }
129}