Skip to main content

sc_lint/
cli.rs

1use std::path::PathBuf;
2
3use clap::Parser;
4use clap::Subcommand;
5use clap::ValueEnum;
6use serde::Serialize;
7
8use crate::profile_plan::Platform;
9use crate::profile_plan::PlatformScope;
10
11#[derive(Debug, Clone, Parser)]
12#[command(name = "sc-lint")]
13#[command(about = "Stable top-level CLI for the sc-lint tool family")]
14#[command(
15    after_long_help = "Documentation guides: `sc-lint docs` (overview), `installation` (alias: `setup`), `using-sc-lint`, `configuration`, `just-setup`, `ci`, `upgrade`, `troubleshooting`, `best-practices`, `cli-reference`, and one guide for each `sc-lint-*` package: `sc-lint`, `sc-lint-analyzer-support`, `sc-lint-attributes`, `sc-lint-boundary`, `sc-lint-directives`, `sc-lint-portability`, `sc-lint-runtime`, and `sc-lint-schema`. The overview accepts `README`, `README.md`, or `overview`. Use `sc-lint docs --path` for the installed bundle path."
16)]
17#[command(disable_version_flag = true)]
18pub struct Cli {
19    /// Write the stable JSON envelope to stdout (errors are written to stderr).
20    #[arg(long, global = true)]
21    pub json: bool,
22    /// Print the sc-lint and analyzer versions.
23    #[arg(long, short = 'V', global = true)]
24    pub version: bool,
25    /// Repository root used by lint, view, check, clippy, compatibility check, test, and CI.
26    #[arg(long, global = true, value_name = "PATH")]
27    pub root: Option<PathBuf>,
28    /// Read the consumer configuration from this path, relative to the repository root when relative.
29    #[arg(long, global = true, value_name = "PATH")]
30    pub config: Option<PathBuf>,
31    /// Override the configured command log directory.
32    #[arg(long, global = true, value_name = "PATH")]
33    pub log_root: Option<PathBuf>,
34    /// Enable console logging for this command.
35    #[arg(long, global = true)]
36    pub log_console: bool,
37    #[command(subcommand)]
38    pub(crate) command: Option<Command>,
39}
40
41#[derive(Debug, Clone, Subcommand)]
42pub enum Command {
43    /// Run one analyzer, maintainer lint profile, or configured consumer lint profile.
44    Lint {
45        #[arg(value_enum, value_name = "TARGET")]
46        target: LintTarget,
47        /// Run a configured consumer profile. Supported only with `ci`.
48        #[arg(long)]
49        consumer: bool,
50        /// Select a consumer lint profile or step (`all` runs every step in declaration order); requires `--consumer`.
51        #[arg(long, requires = "consumer")]
52        profile: Option<String>,
53        /// Consumer CI platform scope: `host` runs unannotated and host-tagged steps; `all` also reports unsupported cross-platform steps; a named platform adds its steps. Requires `--consumer`.
54        #[arg(long = "platform-scope", value_enum, requires = "consumer")]
55        platform_scope: Option<PlatformScopeArg>,
56        /// Run these analyzer rules (`<RULE-ID|family>[,...]`); forwarded unchanged to the selected analyzer.
57        #[arg(long = "rule-filter", conflicts_with = "consumer")]
58        rule_filter: Option<String>,
59        /// Stop starting new cargo-lane and `after_all` steps after the first failure; running steps finish.
60        #[arg(long = "fail-fast", requires = "consumer")]
61        fail_fast: bool,
62    },
63    /// Show analyzer findings or the reserved graph view.
64    View {
65        #[arg(value_enum, value_name = "TARGET")]
66        target: ViewTarget,
67    },
68    /// Run the native or Windows cross-target cargo check.
69    Check {
70        #[arg(value_enum, value_name = "TARGET")]
71        target: CheckTarget,
72    },
73    /// Run the native or Windows cross-target cargo clippy analysis.
74    Clippy {
75        #[arg(value_enum, value_name = "TARGET")]
76        target: ClippyTarget,
77    },
78    /// Check the configured consumer installation and version requirement.
79    Compatibility {
80        #[command(subcommand)]
81        command: CompatibilityCommand,
82    },
83    /// Install or repair the managed sc-lint release required by sc-lint.toml.
84    Setup {
85        /// Report the selected release and install location without changing either.
86        #[arg(long)]
87        dry_run: bool,
88    },
89    /// Inspect or update the managed sc-lint release required by sc-lint.toml.
90    Upgrade {
91        /// Report whether the configured minimum version requires an update.
92        #[arg(long)]
93        check: bool,
94        /// Report the selected release and install location without changing either.
95        #[arg(long)]
96        dry_run: bool,
97    },
98    /// Seed consumer integration files and manage the bootstrap.
99    Init {
100        /// Generate the canonical thin Just integration.
101        #[arg(long, required = true)]
102        just: bool,
103        /// Verify that the generated integration is current without changing files.
104        #[arg(long)]
105        check: bool,
106        /// Report the changes that would be made without changing files.
107        #[arg(long)]
108        dry_run: bool,
109    },
110    /// Read an installed guide from the offline documentation bundle.
111    #[command(
112        after_help = "Guide aliases: `README.md` and `overview` select `README`; `setup` selects `installation`."
113    )]
114    Docs {
115        /// Guide to print or resolve. Defaults to the overview (`README`).
116        #[arg(value_enum)]
117        guide: Option<DocsGuide>,
118        /// Print the guide or bundle path without a label.
119        #[arg(long)]
120        path: bool,
121    },
122    /// Run the explicitly configured consumer test profile.
123    Test {
124        /// Selector to run, then the arguments appended to its command. Omitted
125        /// runs `default` (every row of a `[[tool.sc-lint.test]]` list). Flags
126        /// (`--fail-fast`, `--list`, `--config`, `--json`) must precede the
127        /// selector; everything after it is forwarded verbatim. One positional,
128        /// so clap stops parsing flags at the selector.
129        #[arg(
130            value_name = "SELECTOR [ARGS]",
131            trailing_var_arg = true,
132            allow_hyphen_values = true
133        )]
134        selector_and_args: Vec<String>,
135        /// Print the declared selectors instead of running them.
136        #[arg(long, conflicts_with_all = ["selector_and_args", "fail_fast"])]
137        list: bool,
138        /// Same as `lint --consumer --fail-fast`.
139        #[arg(long = "fail-fast")]
140        fail_fast: bool,
141    },
142    /// Print the product, backend, and contract versions.
143    Version,
144    /// Run the source-maintainer CI profile. Use `sc-lint lint ci --consumer` for a configured consumer profile.
145    Ci,
146}
147
148/// Command-line spelling of a consumer `--platform-scope`.
149#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
150pub enum PlatformScopeArg {
151    #[value(name = "host")]
152    Host,
153    #[value(name = "all")]
154    All,
155    #[value(name = "linux")]
156    Linux,
157    #[value(name = "macos")]
158    Macos,
159    #[value(name = "windows")]
160    Windows,
161}
162
163impl From<PlatformScopeArg> for PlatformScope {
164    fn from(value: PlatformScopeArg) -> Self {
165        match value {
166            PlatformScopeArg::Host => Self::Host,
167            PlatformScopeArg::All => Self::All,
168            PlatformScopeArg::Linux => Self::Named(Platform::Linux),
169            PlatformScopeArg::Macos => Self::Named(Platform::MacOS),
170            PlatformScopeArg::Windows => Self::Named(Platform::Windows),
171        }
172    }
173}
174
175#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
176pub enum DocsGuide {
177    #[value(name = "README", aliases = ["README.md", "overview"])]
178    Overview,
179    #[value(name = "installation", alias = "setup")]
180    Installation,
181    #[value(name = "using-sc-lint")]
182    UsingScLint,
183    Configuration,
184    #[value(name = "just-setup")]
185    JustSetup,
186    Ci,
187    Upgrade,
188    Troubleshooting,
189    #[value(name = "best-practices")]
190    BestPractices,
191    #[value(name = "cli-reference")]
192    CliReference,
193    #[value(name = "sc-lint")]
194    ScLint,
195    #[value(name = "sc-lint-attributes")]
196    ScLintAttributes,
197    #[value(name = "sc-lint-analyzer-support")]
198    ScLintAnalyzerSupport,
199    #[value(name = "sc-lint-boundary")]
200    ScLintBoundary,
201    #[value(name = "sc-lint-directives")]
202    ScLintDirectives,
203    #[value(name = "sc-lint-portability")]
204    ScLintPortability,
205    #[value(name = "sc-lint-runtime")]
206    ScLintRuntime,
207    #[value(name = "sc-lint-schema")]
208    ScLintSchema,
209}
210
211#[derive(Debug, Clone, Copy, PartialEq, Eq)]
212pub(crate) struct DocsGuideMetadata {
213    pub(crate) name: &'static str,
214    pub(crate) path: &'static str,
215}
216
217impl DocsGuide {
218    pub(crate) const fn metadata(self) -> DocsGuideMetadata {
219        match self {
220            Self::Overview => DocsGuideMetadata {
221                name: "README.md",
222                path: "README.md",
223            },
224            Self::Installation => DocsGuideMetadata {
225                name: "installation",
226                path: "installation.md",
227            },
228            Self::UsingScLint => DocsGuideMetadata {
229                name: "using-sc-lint",
230                path: "using-sc-lint.md",
231            },
232            Self::Configuration => DocsGuideMetadata {
233                name: "configuration",
234                path: "configuration.md",
235            },
236            Self::JustSetup => DocsGuideMetadata {
237                name: "just-setup",
238                path: "just-setup.md",
239            },
240            Self::Ci => DocsGuideMetadata {
241                name: "ci",
242                path: "ci.md",
243            },
244            Self::Upgrade => DocsGuideMetadata {
245                name: "upgrade",
246                path: "upgrade.md",
247            },
248            Self::Troubleshooting => DocsGuideMetadata {
249                name: "troubleshooting",
250                path: "troubleshooting.md",
251            },
252            Self::BestPractices => DocsGuideMetadata {
253                name: "best-practices",
254                path: "best-practices.md",
255            },
256            Self::CliReference => DocsGuideMetadata {
257                name: "cli-reference",
258                path: "cli-reference/README.md",
259            },
260            Self::ScLint => DocsGuideMetadata {
261                name: "sc-lint",
262                path: "packages/sc-lint.md",
263            },
264            Self::ScLintAttributes => DocsGuideMetadata {
265                name: "sc-lint-attributes",
266                path: "packages/sc-lint-attributes.md",
267            },
268            Self::ScLintAnalyzerSupport => DocsGuideMetadata {
269                name: "sc-lint-analyzer-support",
270                path: "packages/sc-lint-analyzer-support.md",
271            },
272            Self::ScLintBoundary => DocsGuideMetadata {
273                name: "sc-lint-boundary",
274                path: "packages/sc-lint-boundary.md",
275            },
276            Self::ScLintDirectives => DocsGuideMetadata {
277                name: "sc-lint-directives",
278                path: "packages/sc-lint-directives.md",
279            },
280            Self::ScLintPortability => DocsGuideMetadata {
281                name: "sc-lint-portability",
282                path: "packages/sc-lint-portability.md",
283            },
284            Self::ScLintRuntime => DocsGuideMetadata {
285                name: "sc-lint-runtime",
286                path: "packages/sc-lint-runtime.md",
287            },
288            Self::ScLintSchema => DocsGuideMetadata {
289                name: "sc-lint-schema",
290                path: "packages/sc-lint-schema.md",
291            },
292        }
293    }
294}
295
296#[derive(Debug, Clone, Subcommand)]
297pub enum CompatibilityCommand {
298    /// Verify the installed sc-lint binary satisfies sc-lint.toml.
299    Check {
300        /// Binary to probe. Defaults to `sc-lint` resolved from PATH.
301        #[arg(long, value_name = "path")]
302        binary: Option<PathBuf>,
303    },
304}
305
306#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, ValueEnum)]
307#[serde(rename_all = "snake_case")]
308pub enum LintProfile {
309    Fast,
310    Full,
311    Ci,
312}
313
314impl LintProfile {
315    pub const fn command_suffix(self) -> &'static str {
316        match self {
317            Self::Fast => "fast",
318            Self::Full => "full",
319            Self::Ci => "ci",
320        }
321    }
322}
323
324#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
325#[serde(rename_all = "snake_case")]
326pub enum OutputMode {
327    Human,
328    Json,
329}
330
331impl OutputMode {
332    pub const fn from_json_flag(json: bool) -> Self {
333        if json { Self::Json } else { Self::Human }
334    }
335
336    pub const fn is_json(self) -> bool {
337        matches!(self, Self::Json)
338    }
339}
340
341#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
342pub enum LintTarget {
343    #[value(
344        name = "sc-boundary",
345        help = "Run the Rust dependency and manifest boundary analyzer"
346    )]
347    ScBoundary,
348    #[value(
349        name = "sc-portability",
350        help = "Run the Rust cross-platform portability analyzer"
351    )]
352    ScPortability,
353    #[value(
354        name = "sc-runtime",
355        help = "Run the Rust async and runtime-pattern analyzer"
356    )]
357    ScRuntime,
358    #[value(
359        name = "function-length",
360        help = "Check configured function-length limits"
361    )]
362    FunctionLength,
363    #[value(
364        name = "line-counts",
365        help = "Check configured source line-count limits"
366    )]
367    LineCounts,
368    #[value(
369        name = "identity-literals",
370        help = "Check configured identity and sentinel literals"
371    )]
372    IdentityLiterals,
373    #[value(name = "fast", help = "Run the source-maintainer fast lint profile")]
374    Fast,
375    #[value(
376        name = "full",
377        help = "Run the complete source-maintainer lint profile"
378    )]
379    Full,
380    #[value(
381        name = "ci",
382        help = "Run the source-maintainer profile, or the configured consumer profile with `--consumer`"
383    )]
384    Ci,
385}
386
387impl LintTarget {
388    pub const fn command_suffix(self) -> &'static str {
389        match self {
390            Self::ScBoundary => "sc-boundary",
391            Self::ScPortability => "sc-portability",
392            Self::ScRuntime => "sc-runtime",
393            Self::FunctionLength => "function-length",
394            Self::LineCounts => "line-counts",
395            Self::IdentityLiterals => "identity-literals",
396            Self::Fast => "fast",
397            Self::Full => "full",
398            Self::Ci => "ci",
399        }
400    }
401
402    pub const fn profile(self) -> Option<LintProfile> {
403        match self {
404            Self::Fast => Some(LintProfile::Fast),
405            Self::Full => Some(LintProfile::Full),
406            Self::Ci => Some(LintProfile::Ci),
407            Self::ScBoundary
408            | Self::ScPortability
409            | Self::ScRuntime
410            | Self::FunctionLength
411            | Self::LineCounts
412            | Self::IdentityLiterals => None,
413        }
414    }
415}
416
417#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
418pub enum ViewTarget {
419    #[value(name = "graph", help = "Reserved graph view; not implemented yet")]
420    Graph,
421    #[value(
422        name = "findings",
423        help = "Show findings through the installed Python adapter"
424    )]
425    Findings,
426}
427
428impl ViewTarget {}
429
430#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
431pub enum CheckTarget {
432    #[value(name = "native", help = "Run cargo check for the current host")]
433    Native,
434    #[value(
435        name = "xwin",
436        help = "Run cargo check for x86_64-pc-windows-msvc with cargo-xwin"
437    )]
438    Xwin,
439}
440
441impl CheckTarget {
442    pub const fn command_suffix(self) -> &'static str {
443        match self {
444            Self::Native => "native",
445            Self::Xwin => "xwin",
446        }
447    }
448}
449
450#[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
451pub enum ClippyTarget {
452    #[value(name = "native", help = "Run cargo clippy for the current host")]
453    Native,
454    #[value(
455        name = "xwin",
456        help = "Run cargo clippy for x86_64-pc-windows-msvc with cargo-xwin"
457    )]
458    Xwin,
459}
460
461impl ClippyTarget {
462    pub const fn command_suffix(self) -> &'static str {
463        match self {
464            Self::Native => "native",
465            Self::Xwin => "xwin",
466        }
467    }
468}