Skip to main content

codeint_config/
command.rs

1use std::path::PathBuf;
2
3use crate::OutputFormat;
4use crate::PluginKind;
5use crate::PluginLimits;
6use crate::calendar_day::CalendarDay;
7use crate::churn::{ChurnFormat, ChurnWindow};
8use crate::coupling::CouplingSettings;
9use crate::resolved::ConfigOverrides;
10
11pub(crate) const DEFAULT_PLUGIN_DOWNLOAD_TIMEOUT_SECONDS: u64 = 60;
12pub const DEFAULT_PLUGIN_MAX_DOWNLOAD_BYTES: u64 = 50 * 1024 * 1024;
13pub(crate) const MAX_PLUGIN_DOWNLOAD_MIB: u64 = (i64::MAX as u64) / (1024 * 1024);
14pub(crate) const WORST_FINDINGS_PER_METRIC: usize = 20;
15pub const DEFAULT_MAX_TRACE_PATHS: usize = 10;
16pub const DEFAULT_RENAME_THRESHOLD: u8 = 50;
17pub const MAX_RENAME_THRESHOLD: u8 = 100;
18pub const DEFAULT_UPDATE_CONNECT_TIMEOUT_SECONDS: u64 = 15;
19pub const DEFAULT_UPDATE_METADATA_MAX_TIME_SECONDS: u64 = 30;
20pub const DEFAULT_UPDATE_DOWNLOAD_MAX_TIME_SECONDS: u64 = 300;
21pub const DEFAULT_CHECK_RUN_NAME: &str = "codeint";
22pub const CHECK_RUN_ANNOTATIONS_PER_REQUEST: usize = 50;
23pub const DEFAULT_CHECK_RUN_MAX_ANNOTATIONS: usize = 250;
24pub const CHECK_RUN_SUMMARY_MAX_BYTES: usize = 65_535;
25pub const CHECK_RUN_ANNOTATION_MESSAGE_MAX_BYTES: usize = 65_535;
26pub const CHECK_RUN_API_ERROR_MAX_CHARS: usize = 200;
27pub const DEFAULT_CHECK_RUN_CONNECT_TIMEOUT_SECONDS: u64 = 15;
28pub const DEFAULT_CHECK_RUN_MAX_TIME_SECONDS: u64 = 30;
29pub const DEFAULT_GITHUB_API_URL: &str = "https://api.github.com";
30const LATEST_COMMITS_OF_A_TREND: usize = 20;
31
32#[derive(Debug, Clone, PartialEq, Eq)]
33pub struct PluginHostConfig {
34    pub cache_dir: Option<PathBuf>,
35    pub ca_cert: Option<PathBuf>,
36    pub download_timeout_seconds: u64,
37    pub max_download_bytes: u64,
38    pub compiled_plugin_cache: Option<PathBuf>,
39}
40
41impl Default for PluginHostConfig {
42    fn default() -> PluginHostConfig {
43        PluginHostConfig {
44            cache_dir: None,
45            ca_cert: None,
46            download_timeout_seconds: DEFAULT_PLUGIN_DOWNLOAD_TIMEOUT_SECONDS,
47            max_download_bytes: DEFAULT_PLUGIN_MAX_DOWNLOAD_BYTES,
48            compiled_plugin_cache: None,
49        }
50    }
51}
52
53#[derive(Debug, thiserror::Error)]
54pub enum ConfigError {
55    #[error("cannot read the current folder")]
56    CurrentFolder(#[source] std::io::Error),
57    #[error("{} does not exist", path.display())]
58    MissingPath { path: PathBuf },
59    #[error("{} is not a folder", path.display())]
60    NotAFolder { path: PathBuf },
61    #[error("cannot open {}", path.display())]
62    UnreadablePath {
63        path: PathBuf,
64        #[source]
65        source: std::io::Error,
66    },
67    #[error(
68        "cannot write the report to {}: {} is not a folder that exists",
69        path.display(),
70        folder.display()
71    )]
72    MissingOutputFolder { path: PathBuf, folder: PathBuf },
73    #[error("cannot determine the home folder for agent configuration")]
74    MissingAgentHome,
75    #[error(
76        "CODEINT_PLUGIN_MAX_DOWNLOAD_MB must be a positive integer in MiB up to {MAX_PLUGIN_DOWNLOAD_MIB}, got {value:?}"
77    )]
78    InvalidPluginMaxDownloadMb { value: String },
79}
80
81#[derive(Debug, Clone, PartialEq, Eq)]
82pub enum ReportDestination {
83    StandardOutput,
84    File(PathBuf),
85}
86
87#[derive(Debug, Clone, PartialEq, Eq)]
88pub enum ReportInput {
89    StandardInput,
90    File(PathBuf),
91}
92
93#[derive(Debug, Clone, Copy, PartialEq, Eq)]
94pub enum FindingsShown {
95    WorstPerMetric(usize),
96    All,
97}
98
99impl Default for FindingsShown {
100    fn default() -> FindingsShown {
101        FindingsShown::WorstPerMetric(WORST_FINDINGS_PER_METRIC)
102    }
103}
104
105macro_rules! impl_full_output_format {
106    ($target:ident) => {
107        impl From<OutputFormat> for $target {
108            fn from(format: OutputFormat) -> $target {
109                match format {
110                    OutputFormat::Toon => $target::Toon,
111                    OutputFormat::Text => $target::Text,
112                    OutputFormat::Json => $target::Json,
113                    OutputFormat::Sarif => $target::Sarif,
114                    OutputFormat::Markdown => $target::Markdown,
115                }
116            }
117        }
118    };
119}
120
121macro_rules! impl_subset_output_format {
122    ($target:ident, $supported:expr, $($from:ident => $to:ident),+ $(,)?) => {
123        impl $target {
124            pub const SUPPORTED: &'static [&'static str] = $supported;
125
126            pub fn try_from_output_format(format: OutputFormat) -> Option<$target> {
127                match format {
128                    $(OutputFormat::$from => Some($target::$to),)+
129                    _ => None,
130                }
131            }
132        }
133    };
134}
135
136#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
137pub enum ReportFormat {
138    Toon,
139    Text,
140    Json,
141    Sarif,
142    Markdown,
143}
144
145impl_full_output_format!(ReportFormat);
146
147#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, clap::ValueEnum)]
148pub enum CheckRunConclusionMode {
149    #[default]
150    Auto,
151    Neutral,
152    Failure,
153}
154
155#[derive(Clone, PartialEq, Eq)]
156pub struct CheckRunPublishConfig {
157    pub repository: Option<String>,
158    pub api_url: String,
159    pub token: Option<String>,
160    pub connect_timeout_seconds: u64,
161    pub max_time_seconds: u64,
162}
163
164impl CheckRunPublishConfig {
165    pub fn new(
166        repository: Option<String>,
167        api_url: String,
168        token: Option<String>,
169    ) -> CheckRunPublishConfig {
170        CheckRunPublishConfig {
171            repository,
172            api_url,
173            token,
174            connect_timeout_seconds: DEFAULT_CHECK_RUN_CONNECT_TIMEOUT_SECONDS,
175            max_time_seconds: DEFAULT_CHECK_RUN_MAX_TIME_SECONDS,
176        }
177    }
178}
179
180impl std::fmt::Debug for CheckRunPublishConfig {
181    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
182        formatter
183            .debug_struct("CheckRunPublishConfig")
184            .field("repository", &self.repository)
185            .field("api_url", &self.api_url)
186            .field("token", &self.token.as_ref().map(|_| "[REDACTED]"))
187            .field("connect_timeout_seconds", &self.connect_timeout_seconds)
188            .field("max_time_seconds", &self.max_time_seconds)
189            .finish()
190    }
191}
192
193#[derive(Debug, Clone, PartialEq, Eq)]
194pub struct CheckRunConfig {
195    pub name: String,
196    pub base_ref: Option<String>,
197    pub env_base_ref: Option<String>,
198    pub head_sha: Option<String>,
199    pub conclusion: CheckRunConclusionMode,
200    pub max_annotations: usize,
201    pub publish: Option<CheckRunPublishConfig>,
202}
203
204#[derive(Debug, Clone, PartialEq, Eq)]
205pub enum ReportMode {
206    Render {
207        all: bool,
208        format: Option<ReportRenderFormat>,
209    },
210    CheckRun(CheckRunConfig),
211}
212
213#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
214pub enum ReportRenderFormat {
215    Toon,
216    Text,
217    Json,
218    Sarif,
219    Markdown,
220}
221
222impl_full_output_format!(ReportRenderFormat);
223
224impl ReportRenderFormat {
225    pub fn as_report_format(self) -> ReportFormat {
226        match self {
227            ReportRenderFormat::Toon => ReportFormat::Toon,
228            ReportRenderFormat::Text => ReportFormat::Text,
229            ReportRenderFormat::Json => ReportFormat::Json,
230            ReportRenderFormat::Sarif => ReportFormat::Sarif,
231            ReportRenderFormat::Markdown => ReportFormat::Markdown,
232        }
233    }
234}
235
236#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
237pub enum TrendFormat {
238    Toon,
239    Text,
240    Json,
241    Markdown,
242}
243
244impl_subset_output_format!(
245    TrendFormat,
246    &["toon", "text", "json", "markdown"],
247    Toon => Toon,
248    Text => Text,
249    Json => Json,
250    Markdown => Markdown,
251);
252
253impl_subset_output_format!(
254    ChurnFormat,
255    &["toon", "text", "json", "markdown"],
256    Toon => Toon,
257    Text => Text,
258    Json => Json,
259    Markdown => Markdown,
260);
261
262#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
263pub enum GuardFormat {
264    Text,
265    Toon,
266    Json,
267}
268
269#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
270pub enum SchemaFormat {
271    Json,
272    Toon,
273}
274
275#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, clap::ValueEnum)]
276pub enum InitFormat {
277    #[default]
278    Jsonc,
279    Json,
280    Toml,
281    Yaml,
282}
283
284impl InitFormat {
285    pub fn file_name(self) -> &'static str {
286        match self {
287            InitFormat::Jsonc => "codeint.jsonc",
288            InitFormat::Json => "codeint.json",
289            InitFormat::Toml => "codeint.toml",
290            InitFormat::Yaml => "codeint.yaml",
291        }
292    }
293}
294
295#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
296pub enum ConfigFormat {
297    Text,
298    Toon,
299    Json,
300}
301
302#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
303pub enum AgentFormat {
304    Text,
305    Toon,
306    Json,
307}
308
309#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
310pub enum RatchetFormat {
311    Text,
312    Toon,
313    Json,
314}
315
316#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
317pub enum BaselineFormat {
318    Text,
319    Toon,
320    Json,
321}
322
323#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
324pub enum StateCheckFormat {
325    Toon,
326    Text,
327    Json,
328}
329
330#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
331pub enum DoctorFormat {
332    Text,
333    Json,
334    Toon,
335}
336
337pub type PluginTestFormat = DoctorFormat;
338
339#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
340pub enum ImpactFormat {
341    Toon,
342    Json,
343    Text,
344}
345
346impl_subset_output_format!(
347    ImpactFormat,
348    &["toon", "text", "json"],
349    Toon => Toon,
350    Text => Text,
351    Json => Json,
352);
353
354#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
355pub enum TraceFormat {
356    Toon,
357    Json,
358    Text,
359}
360
361impl_subset_output_format!(
362    TraceFormat,
363    &["toon", "text", "json"],
364    Toon => Toon,
365    Text => Text,
366    Json => Json,
367);
368
369#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
370pub enum AgentClient {
371    ClaudeCode,
372    Cursor,
373    Codex,
374}
375
376impl AgentClient {
377    pub const ALL: [AgentClient; 3] = [
378        AgentClient::ClaudeCode,
379        AgentClient::Cursor,
380        AgentClient::Codex,
381    ];
382
383    pub fn as_str(self) -> &'static str {
384        match self {
385            AgentClient::ClaudeCode => "claude-code",
386            AgentClient::Cursor => "cursor",
387            AgentClient::Codex => "codex",
388        }
389    }
390
391    pub fn display_name(self) -> &'static str {
392        match self {
393            AgentClient::ClaudeCode => "Claude Code",
394            AgentClient::Cursor => "Cursor",
395            AgentClient::Codex => "Codex",
396        }
397    }
398}
399
400#[derive(Debug, Clone, PartialEq, Eq)]
401pub enum AgentCommand {
402    Status {
403        config_root: PathBuf,
404        client: Option<AgentClient>,
405        format: AgentFormat,
406    },
407    Install {
408        config_root: PathBuf,
409        client: Option<AgentClient>,
410        dry_run: bool,
411        format: AgentFormat,
412    },
413}
414
415#[derive(Debug, Clone, PartialEq)]
416pub enum PluginCommand {
417    Test {
418        plugin_host: PluginHostConfig,
419        path: PathBuf,
420        kind: PluginKind,
421        sources: Vec<PathBuf>,
422        limits: PluginLimits,
423        format: PluginTestFormat,
424    },
425}
426
427#[derive(Debug, Clone, PartialEq, Eq)]
428pub struct StateUpdateCommand<F> {
429    pub folder: PathBuf,
430    pub reset: bool,
431    pub coverage_reports: Vec<PathBuf>,
432    pub format: F,
433    pub plugin_host: PluginHostConfig,
434}
435
436#[derive(Debug, Clone, PartialEq, Eq)]
437pub enum RatchetCommand {
438    Update(StateUpdateCommand<RatchetFormat>),
439}
440
441#[derive(Debug, Clone, PartialEq, Eq)]
442pub enum BaselineCommand {
443    Update(StateUpdateCommand<BaselineFormat>),
444}
445
446#[derive(Debug, Clone, PartialEq, Eq)]
447pub enum StateCommand {
448    Check {
449        folder: PathBuf,
450        base_ref: String,
451        head_ref: Option<String>,
452        rename_threshold: u8,
453        destination: ReportDestination,
454        format: StateCheckFormat,
455    },
456}
457
458#[derive(Debug, Clone, Copy, PartialEq, Eq)]
459pub enum TrendRange {
460    Latest(usize),
461    Since(CalendarDay),
462}
463
464impl Default for TrendRange {
465    fn default() -> TrendRange {
466        TrendRange::Latest(LATEST_COMMITS_OF_A_TREND)
467    }
468}
469
470#[derive(Debug, Clone, PartialEq)]
471pub enum Command {
472    Check {
473        folder: PathBuf,
474        destination: ReportDestination,
475        findings: FindingsShown,
476        format: Option<ReportFormat>,
477        package: Option<String>,
478        owner: Option<String>,
479        coverage_reports: Vec<PathBuf>,
480        ignore_baseline: bool,
481        plugin_host: PluginHostConfig,
482    },
483    Diff {
484        folder: PathBuf,
485        base_ref: String,
486        head_ref: Option<String>,
487        rename_threshold: u8,
488        destination: ReportDestination,
489        findings: FindingsShown,
490        format: Option<ReportFormat>,
491        package: Option<String>,
492        owner: Option<String>,
493        result_cache: Option<PathBuf>,
494        plugin_host: PluginHostConfig,
495    },
496    Trend {
497        folder: PathBuf,
498        range: TrendRange,
499        destination: ReportDestination,
500        format: Option<TrendFormat>,
501        result_cache: Option<PathBuf>,
502        plugin_host: PluginHostConfig,
503    },
504    Impact {
505        folder: PathBuf,
506        paths: Vec<String>,
507        changed: Option<String>,
508        depth: Option<usize>,
509        package: Option<String>,
510        destination: ReportDestination,
511        format: Option<ImpactFormat>,
512        plugin_host: PluginHostConfig,
513    },
514    Trace {
515        folder: PathBuf,
516        from: String,
517        to: String,
518        all: bool,
519        max_paths: usize,
520        package: Option<String>,
521        destination: ReportDestination,
522        format: Option<TraceFormat>,
523        plugin_host: PluginHostConfig,
524    },
525    Init {
526        folder: PathBuf,
527        adopt: bool,
528        format: InitFormat,
529        plugin_host: PluginHostConfig,
530    },
531    Update {
532        check_only: bool,
533        version: Option<String>,
534        release_base_url: Option<String>,
535    },
536    Mcp {
537        folder: PathBuf,
538        result_cache: Option<PathBuf>,
539        plugin_host: PluginHostConfig,
540    },
541    Guard {
542        folder: PathBuf,
543        target: PathBuf,
544        format: GuardFormat,
545    },
546    Schema {
547        destination: ReportDestination,
548        format: SchemaFormat,
549    },
550    Config {
551        folder: PathBuf,
552        format: ConfigFormat,
553        overrides: ConfigOverrides,
554    },
555    Report {
556        folder: PathBuf,
557        input: ReportInput,
558        destination: ReportDestination,
559        mode: ReportMode,
560    },
561    Doctor {
562        folder: PathBuf,
563        format: DoctorFormat,
564        plugin_host: PluginHostConfig,
565    },
566    Agent(AgentCommand),
567    Ratchet(RatchetCommand),
568    Plugin(PluginCommand),
569    Baseline(BaselineCommand),
570    State(StateCommand),
571    Churn {
572        folder: PathBuf,
573        window: ChurnWindow,
574        destination: ReportDestination,
575        findings: FindingsShown,
576        format: Option<ChurnFormat>,
577        plugin_host: PluginHostConfig,
578    },
579    Coupling {
580        folder: PathBuf,
581        settings: CouplingSettings,
582        destination: ReportDestination,
583        findings: FindingsShown,
584        format: Option<ChurnFormat>,
585        plugin_host: PluginHostConfig,
586    },
587}
588
589#[derive(Debug)]
590pub struct Invocation {
591    pub command: Command,
592}