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        path: PathBuf,
419        kind: PluginKind,
420        sources: Vec<PathBuf>,
421        limits: PluginLimits,
422        format: PluginTestFormat,
423    },
424}
425
426#[derive(Debug, Clone, PartialEq, Eq)]
427pub struct StateUpdateCommand<F> {
428    pub folder: PathBuf,
429    pub reset: bool,
430    pub coverage_reports: Vec<PathBuf>,
431    pub format: F,
432    pub plugin_host: PluginHostConfig,
433}
434
435#[derive(Debug, Clone, PartialEq, Eq)]
436pub enum RatchetCommand {
437    Update(StateUpdateCommand<RatchetFormat>),
438}
439
440#[derive(Debug, Clone, PartialEq, Eq)]
441pub enum BaselineCommand {
442    Update(StateUpdateCommand<BaselineFormat>),
443}
444
445#[derive(Debug, Clone, PartialEq, Eq)]
446pub enum StateCommand {
447    Check {
448        folder: PathBuf,
449        base_ref: String,
450        head_ref: Option<String>,
451        rename_threshold: u8,
452        destination: ReportDestination,
453        format: StateCheckFormat,
454    },
455}
456
457#[derive(Debug, Clone, Copy, PartialEq, Eq)]
458pub enum TrendRange {
459    Latest(usize),
460    Since(CalendarDay),
461}
462
463impl Default for TrendRange {
464    fn default() -> TrendRange {
465        TrendRange::Latest(LATEST_COMMITS_OF_A_TREND)
466    }
467}
468
469#[derive(Debug, Clone, PartialEq)]
470pub enum Command {
471    Check {
472        folder: PathBuf,
473        destination: ReportDestination,
474        findings: FindingsShown,
475        format: Option<ReportFormat>,
476        package: Option<String>,
477        owner: Option<String>,
478        coverage_reports: Vec<PathBuf>,
479        ignore_baseline: bool,
480        plugin_host: PluginHostConfig,
481    },
482    Diff {
483        folder: PathBuf,
484        base_ref: String,
485        head_ref: Option<String>,
486        rename_threshold: u8,
487        destination: ReportDestination,
488        findings: FindingsShown,
489        format: Option<ReportFormat>,
490        package: Option<String>,
491        owner: Option<String>,
492        result_cache: Option<PathBuf>,
493        plugin_host: PluginHostConfig,
494    },
495    Trend {
496        folder: PathBuf,
497        range: TrendRange,
498        destination: ReportDestination,
499        format: Option<TrendFormat>,
500        result_cache: Option<PathBuf>,
501        plugin_host: PluginHostConfig,
502    },
503    Impact {
504        folder: PathBuf,
505        paths: Vec<String>,
506        changed: Option<String>,
507        depth: Option<usize>,
508        package: Option<String>,
509        destination: ReportDestination,
510        format: Option<ImpactFormat>,
511        plugin_host: PluginHostConfig,
512    },
513    Trace {
514        folder: PathBuf,
515        from: String,
516        to: String,
517        all: bool,
518        max_paths: usize,
519        package: Option<String>,
520        destination: ReportDestination,
521        format: Option<TraceFormat>,
522        plugin_host: PluginHostConfig,
523    },
524    Init {
525        folder: PathBuf,
526        adopt: bool,
527        format: InitFormat,
528        plugin_host: PluginHostConfig,
529    },
530    Update {
531        check_only: bool,
532        version: Option<String>,
533        release_base_url: Option<String>,
534    },
535    Mcp {
536        folder: PathBuf,
537        result_cache: Option<PathBuf>,
538        plugin_host: PluginHostConfig,
539    },
540    Guard {
541        folder: PathBuf,
542        target: PathBuf,
543        format: GuardFormat,
544    },
545    Schema {
546        destination: ReportDestination,
547        format: SchemaFormat,
548    },
549    Config {
550        folder: PathBuf,
551        format: ConfigFormat,
552        overrides: ConfigOverrides,
553    },
554    Report {
555        folder: PathBuf,
556        input: ReportInput,
557        destination: ReportDestination,
558        mode: ReportMode,
559    },
560    Doctor {
561        folder: PathBuf,
562        format: DoctorFormat,
563        plugin_host: PluginHostConfig,
564    },
565    Agent(AgentCommand),
566    Ratchet(RatchetCommand),
567    Plugin(PluginCommand),
568    Baseline(BaselineCommand),
569    State(StateCommand),
570    Churn {
571        folder: PathBuf,
572        window: ChurnWindow,
573        destination: ReportDestination,
574        findings: FindingsShown,
575        format: Option<ChurnFormat>,
576        plugin_host: PluginHostConfig,
577    },
578    Coupling {
579        folder: PathBuf,
580        settings: CouplingSettings,
581        destination: ReportDestination,
582        findings: FindingsShown,
583        format: Option<ChurnFormat>,
584        plugin_host: PluginHostConfig,
585    },
586}
587
588#[derive(Debug)]
589pub struct Invocation {
590    pub command: Command,
591}