code_baseline/cli/mod.rs
1pub mod format;
2pub mod toml_config;
3
4use clap::{Parser, Subcommand, ValueEnum};
5use std::path::PathBuf;
6
7#[derive(Parser)]
8#[command(name = "baseline", about = "Enforce architectural decisions AI coding tools keep ignoring")]
9pub struct Cli {
10 #[command(subcommand)]
11 pub command: Commands,
12}
13
14#[derive(Subcommand)]
15pub enum Commands {
16 /// Scan files for rule violations
17 Scan {
18 /// Paths to scan (files or directories)
19 #[arg(required_unless_present = "stdin")]
20 paths: Vec<PathBuf>,
21
22 /// Path to baseline.toml config file
23 #[arg(short, long, default_value = "baseline.toml")]
24 config: PathBuf,
25
26 /// Output format
27 #[arg(short, long, value_enum, default_value_t = OutputFormat::Pretty)]
28 format: OutputFormat,
29
30 /// Read file content from stdin instead of disk
31 #[arg(long)]
32 stdin: bool,
33
34 /// Filename to use for glob matching when using --stdin
35 #[arg(long, requires = "stdin")]
36 filename: Option<String>,
37
38 /// Only scan files changed relative to a base branch (requires git)
39 #[arg(long, conflicts_with = "stdin")]
40 changed_only: bool,
41
42 /// Base ref for --changed-only (default: auto-detect from CI env or "main")
43 #[arg(long, requires = "changed_only")]
44 base: Option<String>,
45
46 /// Apply fixes automatically
47 #[arg(long)]
48 fix: bool,
49
50 /// Preview fixes without applying (requires --fix)
51 #[arg(long, requires = "fix")]
52 dry_run: bool,
53 },
54
55 /// Count current occurrences of ratchet patterns and write a baseline JSON file
56 Baseline {
57 /// Paths to scan (files or directories)
58 #[arg(required = true)]
59 paths: Vec<PathBuf>,
60
61 /// Path to baseline.toml config file
62 #[arg(short, long, default_value = "baseline.toml")]
63 config: PathBuf,
64
65 /// Output file path for the baseline JSON
66 #[arg(short, long, default_value = ".baseline-snapshot.json")]
67 output: PathBuf,
68 },
69
70 /// Run as an MCP (Model Context Protocol) server over stdio
71 Mcp {
72 /// Path to baseline.toml config file
73 #[arg(short, long, default_value = "baseline.toml")]
74 config: PathBuf,
75 },
76
77 /// Generate a starter baseline.toml for your project
78 Init {
79 /// Output file path for the generated config
80 #[arg(short, long, default_value = "baseline.toml")]
81 output: PathBuf,
82
83 /// Overwrite existing config file
84 #[arg(long)]
85 force: bool,
86 },
87
88 /// Manage ratchet rules (add, tighten, import from baseline)
89 Ratchet {
90 #[command(subcommand)]
91 command: RatchetCommands,
92 },
93}
94
95#[derive(Subcommand)]
96pub enum RatchetCommands {
97 /// Add a new ratchet rule, auto-counting current occurrences
98 Add {
99 /// Pattern to match (literal string or regex with --regex)
100 pattern: String,
101
102 /// Rule ID (default: slugified pattern)
103 #[arg(long)]
104 id: Option<String>,
105
106 /// File glob filter
107 #[arg(long, default_value = "**/*")]
108 glob: String,
109
110 /// Treat pattern as regex
111 #[arg(long)]
112 regex: bool,
113
114 /// Custom message
115 #[arg(long)]
116 message: Option<String>,
117
118 /// Path to baseline.toml config file
119 #[arg(short, long, default_value = "baseline.toml")]
120 config: PathBuf,
121
122 /// Paths to scan (files or directories)
123 #[arg(default_value = ".")]
124 paths: Vec<PathBuf>,
125 },
126
127 /// Re-count and lower max_count for an existing ratchet rule
128 Down {
129 /// Rule ID of the ratchet rule to tighten
130 rule_id: String,
131
132 /// Path to baseline.toml config file
133 #[arg(short, long, default_value = "baseline.toml")]
134 config: PathBuf,
135
136 /// Paths to scan (files or directories)
137 #[arg(default_value = ".")]
138 paths: Vec<PathBuf>,
139 },
140
141 /// Create ratchet rules from a baseline JSON file
142 From {
143 /// Path to the baseline JSON file (output of `baseline snapshot`)
144 baseline: PathBuf,
145
146 /// Path to baseline.toml config file
147 #[arg(short, long, default_value = "baseline.toml")]
148 config: PathBuf,
149 },
150}
151
152#[derive(Clone, ValueEnum)]
153pub enum OutputFormat {
154 Pretty,
155 Json,
156 Compact,
157 Github,
158 Sarif,
159 Markdown,
160}