Skip to main content

provenant/cli/
mod.rs

1// SPDX-FileCopyrightText: nexB Inc. and others
2// ScanCode is a trademark of nexB Inc.
3// SPDX-FileCopyrightText: Provenant contributors
4// SPDX-License-Identifier: Apache-2.0
5// Derived from ScanCode Toolkit (Apache-2.0); modified. See NOTICE.
6
7mod run;
8
9pub use run::run;
10
11use clap::{ArgGroup, Args, Parser, Subcommand};
12use serde_json::{Map as JsonMap, Number as JsonNumber, Value as JsonValue};
13use std::ffi::OsString;
14use std::fs;
15#[cfg(test)]
16use std::ops::Deref;
17use std::path::{Path, PathBuf};
18use yaml_serde::Value as YamlValue;
19
20use crate::app::request::{InputMode, OutputTarget, ScanRequest};
21use crate::license_detection::DEFAULT_LICENSEDB_URL_TEMPLATE;
22use crate::output::OutputFormat;
23use crate::scanner::MemoryMode;
24
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26pub enum ProcessMode {
27    Parallel(usize),
28    SequentialWithTimeouts,
29    SequentialWithoutTimeouts,
30}
31
32impl Default for ProcessMode {
33    fn default() -> Self {
34        let cpus = std::thread::available_parallelism().map_or(1, |n| n.get());
35        if cpus > 1 {
36            ProcessMode::Parallel(cpus - 1)
37        } else {
38            ProcessMode::Parallel(1)
39        }
40    }
41}
42
43impl ProcessMode {
44    fn default_value() -> Self {
45        let cpus = std::thread::available_parallelism().map_or(1, |n| n.get());
46        if cpus > 1 {
47            ProcessMode::Parallel(cpus - 1)
48        } else {
49            ProcessMode::Parallel(1)
50        }
51    }
52
53    pub fn to_i32(self) -> i32 {
54        match self {
55            ProcessMode::Parallel(n) => n as i32,
56            ProcessMode::SequentialWithTimeouts => 0,
57            ProcessMode::SequentialWithoutTimeouts => -1,
58        }
59    }
60}
61
62impl std::fmt::Display for ProcessMode {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        write!(f, "{}", self.to_i32())
65    }
66}
67
68fn parse_processes(value: &str) -> Result<ProcessMode, String> {
69    let parsed: i32 = value
70        .parse()
71        .map_err(|e| format!("invalid integer for --processes: {e}"))?;
72    if parsed > 0 {
73        Ok(ProcessMode::Parallel(
74            u32::try_from(parsed).unwrap() as usize
75        ))
76    } else if parsed == 0 {
77        Ok(ProcessMode::SequentialWithTimeouts)
78    } else {
79        Ok(ProcessMode::SequentialWithoutTimeouts)
80    }
81}
82
83const PDF_OXIDE_LOG_HELP: &str = "Troubleshooting PDF parser logs:\n  Provenant suppresses noisy pdf_oxide logs by default.\n  To inspect raw pdf_oxide logs for debugging, rerun with RUST_LOG=pdf_oxide=warn (or =error).";
84const CLI_ABOUT: &str = "Rust scanner for ScanCode-compatible workflows. Not affiliated with, endorsed by, or sponsored by ScanCode Toolkit, AboutCode, or nexB Inc.";
85const CLI_LONG_ABOUT: &str = "Rust scanner for ScanCode-compatible workflows.\n\nNot affiliated with, endorsed by, or sponsored by ScanCode Toolkit, AboutCode, or nexB Inc.";
86
87fn parse_license_policy_arg(value: &str) -> Result<String, String> {
88    let policy_path = Path::new(value);
89    let metadata = fs::metadata(policy_path).map_err(|err| {
90        format!(
91            "Failed to read license policy file {:?}: {err}",
92            policy_path
93        )
94    })?;
95    if !metadata.is_file() {
96        return Err(format!(
97            "License policy path {:?} is not a regular file",
98            policy_path
99        ));
100    }
101
102    let policy_text = fs::read_to_string(policy_path).map_err(|err| {
103        format!(
104            "Failed to read license policy file {:?}: {err}",
105            policy_path
106        )
107    })?;
108    if policy_text.trim().is_empty() {
109        return Err(format!("License policy file {:?} is empty", policy_path));
110    }
111
112    let policy_value: YamlValue = yaml_serde::from_str(&policy_text).map_err(|err| {
113        format!(
114            "Failed to parse license policy file {:?}: {err}",
115            policy_path
116        )
117    })?;
118    let has_license_policies = policy_value
119        .as_mapping()
120        .and_then(|mapping| mapping.get(YamlValue::String("license_policies".to_string())))
121        .is_some();
122    if !has_license_policies {
123        return Err(format!(
124            "License policy file {:?} is missing a 'license_policies' attribute",
125            policy_path
126        ));
127    }
128
129    Ok(value.to_string())
130}
131
132#[derive(Parser, Debug)]
133#[command(
134    author = "The Provenant contributors",
135    version = crate::version::BUILD_VERSION,
136    long_version = crate::version::build_long_version(),
137    after_help = PDF_OXIDE_LOG_HELP,
138    about = CLI_ABOUT,
139    long_about = CLI_LONG_ABOUT,
140    arg_required_else_help = true,
141    subcommand_required = true
142)]
143pub struct Cli {
144    #[command(subcommand)]
145    pub command: Command,
146}
147
148#[derive(Subcommand, Debug, Clone)]
149pub enum Command {
150    /// Scan files or existing ScanCode-style JSON inputs.
151    Scan(Box<ScanArgs>),
152    /// Run the long-lived HTTP service.
153    Serve(ServeArgs),
154    /// Compare ScanCode and Provenant JSON outputs to review migration-confidence deltas.
155    Compare(CompareArgs),
156    /// Show attribution notices for embedded license detection data.
157    ShowAttribution,
158    /// Export the effective built-in license dataset to DIR and exit.
159    ExportLicenseDataset(ExportLicenseDatasetArgs),
160}
161
162/// Requested output verbosity for a subcommand.
163#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
164pub enum Verbosity {
165    /// Warnings and errors only.
166    Quiet,
167    /// Progress and informational output (the default).
168    #[default]
169    Normal,
170    /// Everything, including debug-level diagnostics.
171    Verbose,
172}
173
174impl Verbosity {
175    /// Default global log level for this verbosity. `RUST_LOG` still overrides.
176    pub fn log_level(self) -> log::LevelFilter {
177        match self {
178            Self::Quiet => log::LevelFilter::Warn,
179            Self::Normal => log::LevelFilter::Info,
180            Self::Verbose => log::LevelFilter::Debug,
181        }
182    }
183}
184
185/// Reusable `-q/-v` verbosity flags, flattened into subcommands other than
186/// `scan` (which has its own richer progress modes tied to its output header).
187#[derive(Args, Debug, Clone, Default)]
188pub struct VerbosityFlags {
189    /// Suppress progress and informational output; show warnings and errors only.
190    #[arg(short = 'q', long = "quiet", conflicts_with = "verbose")]
191    pub quiet: bool,
192
193    /// Emit verbose diagnostics, including debug-level logging.
194    #[arg(short = 'v', long = "verbose", conflicts_with = "quiet")]
195    pub verbose: bool,
196}
197
198impl VerbosityFlags {
199    pub fn verbosity(&self) -> Verbosity {
200        if self.quiet {
201            Verbosity::Quiet
202        } else if self.verbose {
203            Verbosity::Verbose
204        } else {
205            Verbosity::Normal
206        }
207    }
208}
209
210#[derive(Args, Debug, Clone)]
211pub struct CompareArgs {
212    /// Path to an existing ScanCode JSON output file.
213    #[arg(long = "scancode-json", value_name = "PATH")]
214    pub scancode_json: PathBuf,
215
216    /// Path to an existing Provenant JSON output file.
217    #[arg(long = "provenant-json", value_name = "PATH")]
218    pub provenant_json: PathBuf,
219
220    /// Directory where comparison artifacts should be written. Defaults to a timestamped directory in the current working directory.
221    #[arg(long = "artifact-dir", value_name = "DIR")]
222    pub artifact_dir: Option<PathBuf>,
223
224    #[command(flatten)]
225    pub verbosity: VerbosityFlags,
226}
227
228#[derive(Args, Debug, Clone)]
229pub struct ExportLicenseDatasetArgs {
230    #[arg(value_name = "DIR")]
231    pub dir: String,
232
233    #[command(flatten)]
234    pub verbosity: VerbosityFlags,
235}
236
237/// Minimum license-policy compliance severity that fails the build (`--fail-on`).
238#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq, Eq)]
239pub enum FailOn {
240    Warning,
241    Error,
242}
243
244impl FailOn {
245    /// Lowest `ComplianceAlert` severity that should trip the gate.
246    pub fn threshold(self) -> crate::models::ComplianceAlert {
247        match self {
248            Self::Warning => crate::models::ComplianceAlert::Warning,
249            Self::Error => crate::models::ComplianceAlert::Error,
250        }
251    }
252}
253
254#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq, Eq, Default)]
255pub enum CompatibilityMode {
256    #[default]
257    Native,
258    Scancode,
259}
260
261impl CompatibilityMode {
262    fn as_str(self) -> &'static str {
263        match self {
264            Self::Native => "native",
265            Self::Scancode => "scancode",
266        }
267    }
268}
269
270#[derive(Args, Debug, Clone)]
271pub struct ServeArgs {
272    /// Bind the service shell to HOST:PORT.
273    #[arg(long = "bind", value_name = "ADDR", default_value = "127.0.0.1:8080")]
274    pub bind: String,
275
276    /// Allow paths, URL, and repository inputs when bound beyond localhost.
277    #[arg(long = "allow-privileged-inputs")]
278    pub allow_privileged_inputs: bool,
279
280    #[command(flatten)]
281    pub verbosity: VerbosityFlags,
282}
283
284#[derive(Args, Debug, Clone)]
285#[command(
286    group(
287        ArgGroup::new("output")
288            .required(true)
289            .multiple(true)
290            .args([
291                "output_json",
292                "output_json_pp",
293                "output_json_lines",
294                "output_yaml",
295                "output_debian",
296                "output_html",
297                "output_spdx_tv",
298                "output_spdx_rdf",
299                "output_cyclonedx",
300                "output_cyclonedx_xml",
301                "custom_output"
302            ])
303    ),
304    after_help = PDF_OXIDE_LOG_HELP
305)]
306pub struct ScanArgs {
307    /// File or directory paths to scan
308    #[arg(required = false)]
309    pub dir_path: Vec<String>,
310
311    /// Write scan output as compact JSON to FILE
312    #[arg(long = "json", value_name = "FILE", allow_hyphen_values = true)]
313    pub output_json: Option<String>,
314
315    /// Write scan output as pretty-printed JSON to FILE
316    #[arg(long = "json-pp", value_name = "FILE", allow_hyphen_values = true)]
317    pub output_json_pp: Option<String>,
318
319    /// Write scan output as JSON Lines to FILE
320    #[arg(long = "json-lines", value_name = "FILE", allow_hyphen_values = true)]
321    pub output_json_lines: Option<String>,
322
323    /// Write scan output as YAML to FILE
324    #[arg(long = "yaml", value_name = "FILE", allow_hyphen_values = true)]
325    pub output_yaml: Option<String>,
326
327    /// Write scan output in machine-readable Debian copyright format to FILE (requires --license, --copyright, and --license-text)
328    #[arg(
329        long = "debian",
330        value_name = "FILE",
331        allow_hyphen_values = true,
332        requires_all = ["copyright", "license", "license_text"]
333    )]
334    pub output_debian: Option<String>,
335
336    /// Write scan output as HTML report to FILE
337    #[arg(long = "html", value_name = "FILE", allow_hyphen_values = true)]
338    pub output_html: Option<String>,
339
340    /// Write scan output as SPDX tag/value to FILE
341    #[arg(long = "spdx-tv", value_name = "FILE", allow_hyphen_values = true)]
342    pub output_spdx_tv: Option<String>,
343
344    /// Write scan output as SPDX RDF/XML to FILE
345    #[arg(long = "spdx-rdf", value_name = "FILE", allow_hyphen_values = true)]
346    pub output_spdx_rdf: Option<String>,
347
348    /// Write scan output as CycloneDX JSON to FILE
349    #[arg(long = "cyclonedx", value_name = "FILE", allow_hyphen_values = true)]
350    pub output_cyclonedx: Option<String>,
351
352    /// Write scan output as CycloneDX XML to FILE
353    #[arg(
354        long = "cyclonedx-xml",
355        value_name = "FILE",
356        allow_hyphen_values = true
357    )]
358    pub output_cyclonedx_xml: Option<String>,
359
360    /// Write license-policy violations as SARIF 2.1.0 to FILE (needs --license-policy)
361    #[arg(long = "sarif", value_name = "FILE", allow_hyphen_values = true)]
362    pub output_sarif: Option<String>,
363
364    /// Write scan output to FILE formatted with the custom template
365    #[arg(
366        long = "custom-output",
367        value_name = "FILE",
368        requires = "custom_template",
369        allow_hyphen_values = true
370    )]
371    pub custom_output: Option<String>,
372
373    /// Use this template FILE with --custom-output
374    #[arg(
375        long = "custom-template",
376        value_name = "FILE",
377        requires = "custom_output"
378    )]
379    pub custom_template: Option<String>,
380
381    /// Maximum recursion depth (0 means no depth limit)
382    #[arg(short, long, default_value = "0")]
383    pub max_depth: usize,
384
385    #[arg(short = 'n', long, default_value_t = ProcessMode::default_value(), value_parser = parse_processes, allow_hyphen_values = true)]
386    pub processes: ProcessMode,
387
388    #[arg(long, default_value_t = 120.0)]
389    pub timeout: f64,
390
391    #[arg(short, long, conflicts_with = "verbose")]
392    pub quiet: bool,
393
394    /// Emit verbose diagnostics, including the per-phase timing breakdown and, on a TTY, per-file scan detail.
395    #[arg(short, long, conflicts_with = "quiet")]
396    pub verbose: bool,
397
398    #[arg(long, conflicts_with = "full_root")]
399    pub strip_root: bool,
400
401    #[arg(long, conflicts_with = "strip_root")]
402    pub full_root: bool,
403
404    /// Exclude patterns (ScanCode-compatible alias: --ignore)
405    #[arg(long = "exclude", visible_alias = "ignore", value_delimiter = ',')]
406    pub exclude: Vec<String>,
407
408    /// Include files matching PATTERN. Use `**` when you want recursion across directories.
409    #[arg(long, value_delimiter = ',')]
410    pub include: Vec<String>,
411
412    /// Read selected scan paths from FILE (or '-' for stdin), relative to the explicit scan root.
413    #[arg(long = "paths-file", value_name = "FILE", allow_hyphen_values = true)]
414    pub paths_file: Vec<String>,
415
416    #[arg(long = "cache-dir", value_name = "PATH")]
417    pub cache_dir: Option<String>,
418
419    #[arg(long = "cache-clear")]
420    pub cache_clear: bool,
421
422    #[arg(long = "incremental")]
423    pub incremental: bool,
424
425    /// Trust size + mtime for incremental reuse, skipping the content re-hash of
426    /// unchanged files. Speeds up warm incremental re-scans at the cost of
427    /// missing the rare edit that keeps the same size and mtime tick. Such a miss
428    /// is not permanent: the next scan without this flag re-hashes and detects it.
429    /// Default off keeps the paranoid full-hash check so scans stay reproducible.
430    #[arg(long = "cache-trust-mtime", requires = "incremental")]
431    pub cache_trust_mtime: bool,
432
433    /// Maximum number of file and directory scan details kept in memory during
434    /// the in-scan file-processing window before the rest spill to disk.
435    /// Use 0 for unlimited memory or -1 for disk-only spill during the scan.
436    /// This bounds only the in-scan working set, NOT total or peak process
437    /// memory: assembly, summary, and output reconstitute the full result set
438    /// after the scan, so peak RSS is not bounded by this flag.
439    #[arg(
440        long = "max-in-memory",
441        value_name = "INT",
442        default_value_t = MemoryMode::Limit(10000),
443        value_parser = parse_max_in_memory,
444        allow_hyphen_values = true
445    )]
446    pub max_in_memory: MemoryMode,
447
448    /// Collect file information such as checksums, type hints, and source/script flags.
449    #[arg(short = 'i', long)]
450    pub info: bool,
451
452    /// Load one or more existing ScanCode-style JSON scans instead of rescanning inputs.
453    #[arg(long)]
454    pub from_json: bool,
455
456    /// Scan input for application package and dependency manifests, lockfiles and related data
457    #[arg(short = 'p', long)]
458    pub package: bool,
459
460    /// Select a compatibility bundle for intentional Provenant-vs-ScanCode behavior differences.
461    #[arg(
462        long = "compat-mode",
463        visible_alias = "compat",
464        value_enum,
465        default_value_t = CompatibilityMode::Native
466    )]
467    pub compat_mode: CompatibilityMode,
468
469    /// Scan input for installed system package databases (RPM, dpkg, apk, etc.)
470    #[arg(long = "system-package")]
471    pub system_package: bool,
472
473    /// Scan supported compiled Go and Rust binaries for embedded package metadata.
474    #[arg(long = "package-in-compiled")]
475    pub package_in_compiled: bool,
476
477    /// Scan for system and application package data and skip license/copyright detection and top-level package creation.
478    #[arg(
479        long = "package-only",
480        conflicts_with_all = ["license", "summary", "package", "system_package"]
481    )]
482    pub package_only: bool,
483
484    /// Disable package assembly (merging related manifest/lockfiles into packages)
485    #[arg(long)]
486    pub no_assemble: bool,
487
488    /// Path to a custom license dataset root containing manifest.json, rules/, and licenses/.
489    /// If not specified, uses the built-in embedded license index.
490    #[arg(
491        long = "license-dataset-path",
492        value_name = "PATH",
493        requires = "license"
494    )]
495    pub license_dataset_path: Option<String>,
496
497    /// Force rebuild of the license index cache, ignoring any existing cache.
498    #[arg(long)]
499    pub reindex: bool,
500
501    /// Build the license index in memory for this run without reading or writing persistent cache files.
502    #[arg(long = "no-license-index-cache")]
503    pub no_license_index_cache: bool,
504
505    /// Include matched text in license detection output
506    #[arg(long = "license-text", requires = "license")]
507    pub license_text: bool,
508
509    #[arg(long = "license-text-diagnostics", requires = "license_text")]
510    pub license_text_diagnostics: bool,
511
512    #[arg(long = "license-diagnostics", requires = "license")]
513    pub license_diagnostics: bool,
514
515    #[arg(long = "unknown-licenses", requires = "license")]
516    pub unknown_licenses: bool,
517
518    /// Disable approximate sequence matching during `--license` detection.
519    #[arg(long = "no-sequence-matching", requires = "license")]
520    pub no_sequence_matching: bool,
521
522    #[arg(
523        long = "license-score",
524        default_value_t = 0,
525        requires = "license",
526        value_parser = clap::value_parser!(u8).range(0..=100)
527    )]
528    pub license_score: u8,
529
530    #[arg(
531        long = "license-url-template",
532        default_value = DEFAULT_LICENSEDB_URL_TEMPLATE,
533        requires = "license"
534    )]
535    pub license_url_template: String,
536
537    #[arg(long)]
538    pub filter_clues: bool,
539
540    #[arg(
541        long = "ignore-author",
542        value_name = "PATTERN",
543        help = "Ignore a file and all its findings if an author matches the regex PATTERN"
544    )]
545    pub ignore_author: Vec<String>,
546
547    #[arg(
548        long = "ignore-copyright-holder",
549        value_name = "PATTERN",
550        help = "Ignore a file and all its findings if a copyright holder matches the regex PATTERN"
551    )]
552    pub ignore_copyright_holder: Vec<String>,
553
554    #[arg(long)]
555    pub only_findings: bool,
556
557    #[arg(long, requires = "info")]
558    pub mark_source: bool,
559
560    #[arg(long)]
561    pub classify: bool,
562
563    #[arg(long, requires = "classify")]
564    pub summary: bool,
565
566    #[arg(long = "license-clarity-score", requires = "classify")]
567    pub license_clarity_score: bool,
568
569    #[arg(long = "license-references", requires = "license")]
570    pub license_references: bool,
571
572    /// Evaluate file license detections against a YAML license policy file.
573    #[arg(
574        long = "license-policy",
575        value_name = "FILE",
576        value_parser = parse_license_policy_arg
577    )]
578    pub license_policy: Option<String>,
579
580    /// Exit non-zero (code 3) when a scanned file matches a license policy whose
581    /// compliance_alert is at or above this level. Requires --license-policy.
582    #[arg(long = "fail-on", value_enum, requires = "license_policy")]
583    pub fail_on: Option<FailOn>,
584
585    #[arg(long)]
586    pub tallies: bool,
587
588    #[arg(long = "tallies-key-files", requires_all = ["tallies", "classify"])]
589    pub tallies_key_files: bool,
590
591    #[arg(long = "tallies-with-details")]
592    pub tallies_with_details: bool,
593
594    #[arg(long = "facet", value_name = "<facet>=<pattern>")]
595    pub facet: Vec<String>,
596
597    #[arg(long = "tallies-by-facet", requires_all = ["facet", "tallies"])]
598    pub tallies_by_facet: bool,
599
600    #[arg(long)]
601    pub generated: bool,
602
603    /// Scan input for licenses
604    #[arg(short = 'l', long)]
605    pub license: bool,
606
607    #[arg(short = 'c', long)]
608    pub copyright: bool,
609
610    /// Scan input for email addresses
611    #[arg(short = 'e', long)]
612    pub email: bool,
613
614    /// Report only up to INT emails found in a file. Use 0 for no limit.
615    #[arg(long, default_value_t = 50, requires = "email")]
616    pub max_email: usize,
617
618    /// Scan input for URLs
619    #[arg(short = 'u', long)]
620    pub url: bool,
621
622    /// Report only up to INT URLs found in a file. Use 0 for no limit.
623    #[arg(long, default_value_t = 50, requires = "url")]
624    pub max_url: usize,
625}
626
627impl Cli {
628    pub fn parse() -> Self {
629        <Self as Parser>::parse_from(rewrite_args_for_default_scan(std::env::args_os()))
630    }
631
632    pub fn try_parse_from<I, T>(itr: I) -> Result<Self, clap::Error>
633    where
634        I: IntoIterator<Item = T>,
635        T: Into<OsString>,
636    {
637        <Self as Parser>::try_parse_from(rewrite_args_for_default_scan(itr))
638    }
639
640    pub(crate) fn scan_args(&self) -> Option<&ScanArgs> {
641        match &self.command {
642            Command::Scan(scan_args) => Some(scan_args.as_ref()),
643            Command::Serve(_)
644            | Command::Compare(_)
645            | Command::ShowAttribution
646            | Command::ExportLicenseDataset(_) => None,
647        }
648    }
649}
650
651#[cfg(test)]
652impl Deref for Cli {
653    type Target = ScanArgs;
654
655    fn deref(&self) -> &Self::Target {
656        self.scan_args()
657            .expect("scan arguments are only available for the scan command")
658    }
659}
660
661fn rewrite_args_for_default_scan<I, T>(itr: I) -> Vec<OsString>
662where
663    I: IntoIterator<Item = T>,
664    T: Into<OsString>,
665{
666    let mut args: Vec<OsString> = itr.into_iter().map(Into::into).collect();
667    if args.len() <= 1 {
668        return args;
669    }
670
671    let first = args[1].to_string_lossy();
672    if matches!(
673        first.as_ref(),
674        "scan"
675            | "serve"
676            | "compare"
677            | "show-attribution"
678            | "export-license-dataset"
679            | "help"
680            | "-h"
681            | "--help"
682            | "-V"
683            | "--version"
684    ) {
685        return args;
686    }
687
688    if first.starts_with('-') || Path::new(first.as_ref()).exists() {
689        args.insert(1, OsString::from("scan"));
690    }
691
692    args
693}
694
695fn parse_max_in_memory(value: &str) -> Result<MemoryMode, String> {
696    let parsed = value
697        .parse::<i64>()
698        .map_err(|_| format!("invalid integer value: {value}"))?;
699    if parsed < -1 {
700        return Err("--max-in-memory must be -1, 0, or a positive integer".to_string());
701    }
702    match parsed {
703        -1 => Ok(MemoryMode::StreamUnlimited),
704        0 => Ok(MemoryMode::CollectFirst),
705        n if n > 0 => Ok(MemoryMode::Limit(usize::try_from(n).unwrap_or(usize::MAX))),
706        _ => Ok(MemoryMode::CollectFirst),
707    }
708}
709
710type OutputTargetFieldAccessor = fn(&ScanArgs) -> &Option<String>;
711
712/// Single source of truth mapping each plain-file `ScanArgs` output flag to
713/// its [`OutputFormat`], in the order `output_targets` should emit them.
714/// `--custom-output` is handled separately (see `output_targets`) since it
715/// also needs `--custom-template` attached. Adding a new plain output flag
716/// means adding its clap field above, one row here, and one row in
717/// `output::OUTPUT_WRITERS` (`src/output/mod.rs`).
718const OUTPUT_TARGET_FIELDS: &[(OutputFormat, OutputTargetFieldAccessor)] = &[
719    (OutputFormat::Json, |args| &args.output_json),
720    (OutputFormat::JsonPretty, |args| &args.output_json_pp),
721    (OutputFormat::JsonLines, |args| &args.output_json_lines),
722    (OutputFormat::Yaml, |args| &args.output_yaml),
723    (OutputFormat::Debian, |args| &args.output_debian),
724    (OutputFormat::Html, |args| &args.output_html),
725    (OutputFormat::SpdxTv, |args| &args.output_spdx_tv),
726    (OutputFormat::SpdxRdf, |args| &args.output_spdx_rdf),
727    (OutputFormat::CycloneDxJson, |args| &args.output_cyclonedx),
728    (OutputFormat::CycloneDxXml, |args| {
729        &args.output_cyclonedx_xml
730    }),
731    (OutputFormat::Sarif, |args| &args.output_sarif),
732];
733
734impl ScanArgs {
735    pub(crate) fn output_targets(&self) -> Vec<OutputTarget> {
736        let mut targets: Vec<OutputTarget> = OUTPUT_TARGET_FIELDS
737            .iter()
738            .filter_map(|(format, field)| {
739                field(self).as_ref().map(|file| OutputTarget {
740                    format: *format,
741                    file: file.clone(),
742                    custom_template: None,
743                })
744            })
745            .collect();
746
747        if let Some(file) = &self.custom_output {
748            targets.push(OutputTarget {
749                format: OutputFormat::CustomTemplate,
750                file: file.clone(),
751                custom_template: self.custom_template.clone(),
752            });
753        }
754
755        targets
756    }
757
758    pub(crate) fn output_header_options(&self) -> JsonMap<String, JsonValue> {
759        let mut options = JsonMap::new();
760        if !self.dir_path.is_empty() {
761            options.insert(
762                "input".to_string(),
763                JsonValue::Array(
764                    self.dir_path
765                        .iter()
766                        .cloned()
767                        .map(JsonValue::String)
768                        .collect(),
769                ),
770            );
771        }
772
773        let mut flags = Vec::new();
774
775        push_string_option(&mut flags, "--cache-dir", self.cache_dir.as_ref());
776        push_bool_option(&mut flags, "--cache-clear", self.cache_clear);
777        push_bool_option(&mut flags, "--cache-trust-mtime", self.cache_trust_mtime);
778        push_bool_option(&mut flags, "--classify", self.classify);
779        push_string_option(&mut flags, "--custom-output", self.custom_output.as_ref());
780        push_string_option(
781            &mut flags,
782            "--custom-template",
783            self.custom_template.as_ref(),
784        );
785        push_bool_option(&mut flags, "--copyright", self.copyright);
786        if self.compat_mode != CompatibilityMode::Native {
787            flags.push((
788                "--compat-mode".to_string(),
789                JsonValue::String(self.compat_mode.as_str().to_string()),
790            ));
791        }
792        push_string_option(&mut flags, "--cyclonedx", self.output_cyclonedx.as_ref());
793        push_string_option(
794            &mut flags,
795            "--cyclonedx-xml",
796            self.output_cyclonedx_xml.as_ref(),
797        );
798        push_string_option(&mut flags, "--debian", self.output_debian.as_ref());
799        push_bool_option(&mut flags, "--email", self.email);
800        push_array_option(&mut flags, "--facet", &self.facet);
801        push_bool_option(&mut flags, "--filter-clues", self.filter_clues);
802        push_bool_option(&mut flags, "--from-json", self.from_json);
803        push_bool_option(&mut flags, "--full-root", self.full_root);
804        push_bool_option(&mut flags, "--generated", self.generated);
805        push_string_option(&mut flags, "--html", self.output_html.as_ref());
806        push_array_option(&mut flags, "--ignore", &self.exclude);
807        push_array_option(&mut flags, "--ignore-author", &self.ignore_author);
808        push_array_option(
809            &mut flags,
810            "--ignore-copyright-holder",
811            &self.ignore_copyright_holder,
812        );
813        push_bool_option(&mut flags, "--incremental", self.incremental);
814        push_array_option(&mut flags, "--include", &self.include);
815        push_bool_option(&mut flags, "--info", self.info);
816        push_string_option(&mut flags, "--json", self.output_json.as_ref());
817        push_string_option(&mut flags, "--json-lines", self.output_json_lines.as_ref());
818        push_string_option(&mut flags, "--json-pp", self.output_json_pp.as_ref());
819        push_bool_option(&mut flags, "--license", self.license);
820        push_bool_option(
821            &mut flags,
822            "--license-clarity-score",
823            self.license_clarity_score,
824        );
825        push_bool_option(
826            &mut flags,
827            "--license-diagnostics",
828            self.license_diagnostics,
829        );
830        push_string_option(
831            &mut flags,
832            "--license-dataset-path",
833            self.license_dataset_path.as_ref(),
834        );
835        push_string_option(&mut flags, "--license-policy", self.license_policy.as_ref());
836        push_bool_option(
837            &mut flags,
838            "--no-license-index-cache",
839            self.no_license_index_cache,
840        );
841        push_bool_option(&mut flags, "--license-references", self.license_references);
842        push_bool_option(&mut flags, "--reindex", self.reindex);
843        push_non_default_u8_option(&mut flags, "--license-score", self.license_score, 0);
844        push_bool_option(&mut flags, "--license-text", self.license_text);
845        push_bool_option(
846            &mut flags,
847            "--license-text-diagnostics",
848            self.license_text_diagnostics,
849        );
850        push_non_default_string_option(
851            &mut flags,
852            "--license-url-template",
853            &self.license_url_template,
854            DEFAULT_LICENSEDB_URL_TEMPLATE,
855        );
856        push_non_default_usize_option(&mut flags, "--max-depth", self.max_depth, 0);
857        match self.max_in_memory {
858            MemoryMode::Limit(10000) => {}
859            MemoryMode::CollectFirst => {
860                flags.push(("--max-in-memory".to_string(), JsonValue::Number(0.into())));
861            }
862            MemoryMode::StreamUnlimited => {
863                flags.push((
864                    "--max-in-memory".to_string(),
865                    JsonValue::Number((-1i64).into()),
866                ));
867            }
868            MemoryMode::Limit(n) => {
869                flags.push(("--max-in-memory".to_string(), JsonValue::Number(n.into())));
870            }
871        }
872        if self.email {
873            push_non_default_usize_option(&mut flags, "--max-email", self.max_email, 50);
874        }
875        if self.url {
876            push_non_default_usize_option(&mut flags, "--max-url", self.max_url, 50);
877        }
878        push_bool_option(&mut flags, "--mark-source", self.mark_source);
879        push_bool_option(&mut flags, "--no-assemble", self.no_assemble);
880        push_bool_option(&mut flags, "--only-findings", self.only_findings);
881        push_bool_option(&mut flags, "--package", self.package);
882        push_bool_option(
883            &mut flags,
884            "--package-in-compiled",
885            self.package_in_compiled,
886        );
887        push_bool_option(&mut flags, "--package-only", self.package_only);
888        push_array_option(&mut flags, "--paths-file", &self.paths_file);
889        push_non_default_process_mode_option(
890            &mut flags,
891            "--processes",
892            self.processes,
893            ProcessMode::default_value(),
894        );
895        push_bool_option(&mut flags, "--quiet", self.quiet);
896        push_string_option(&mut flags, "--spdx-rdf", self.output_spdx_rdf.as_ref());
897        push_string_option(&mut flags, "--spdx-tv", self.output_spdx_tv.as_ref());
898        push_bool_option(&mut flags, "--strip-root", self.strip_root);
899        push_bool_option(&mut flags, "--summary", self.summary);
900        push_bool_option(&mut flags, "--system-package", self.system_package);
901        push_bool_option(&mut flags, "--tallies", self.tallies);
902        push_bool_option(&mut flags, "--tallies-by-facet", self.tallies_by_facet);
903        push_bool_option(&mut flags, "--tallies-key-files", self.tallies_key_files);
904        push_bool_option(
905            &mut flags,
906            "--tallies-with-details",
907            self.tallies_with_details,
908        );
909        push_non_default_f64_option(&mut flags, "--timeout", self.timeout, 120.0);
910        push_bool_option(&mut flags, "--unknown-licenses", self.unknown_licenses);
911        push_bool_option(
912            &mut flags,
913            "--no-sequence-matching",
914            self.no_sequence_matching,
915        );
916        push_bool_option(&mut flags, "--url", self.url);
917        push_bool_option(&mut flags, "--verbose", self.verbose);
918        push_string_option(&mut flags, "--yaml", self.output_yaml.as_ref());
919
920        flags.sort_by(|left, right| left.0.cmp(&right.0));
921        for (key, value) in flags {
922            options.insert(key, value);
923        }
924
925        options
926    }
927}
928
929impl From<&ScanArgs> for ScanRequest {
930    fn from(cli: &ScanArgs) -> Self {
931        Self {
932            input_paths: cli.dir_path.clone(),
933            input_mode: if cli.from_json {
934                InputMode::FromJson
935            } else {
936                InputMode::Native
937            },
938            output_targets: cli.output_targets(),
939            output_header_options: cli.output_header_options(),
940            progress_mode: if cli.quiet {
941                crate::progress::ProgressMode::Quiet
942            } else if cli.verbose {
943                crate::progress::ProgressMode::Verbose
944            } else {
945                crate::progress::ProgressMode::Default
946            },
947            process_mode: cli.processes,
948            timeout_seconds: cli.timeout,
949            quiet: cli.quiet,
950            verbose: cli.verbose,
951            strip_root: cli.strip_root,
952            full_root: cli.full_root,
953            exclude: cli.exclude.clone(),
954            include: cli.include.clone(),
955            paths_files: cli.paths_file.clone(),
956            respect_process_cache_env: true,
957            cache_dir: cli.cache_dir.clone(),
958            cache_clear: cli.cache_clear,
959            incremental: cli.incremental,
960            cache_trust_mtime: cli.cache_trust_mtime,
961            max_depth: cli.max_depth,
962            max_in_memory: cli.max_in_memory,
963            info: cli.info,
964            package: cli.package,
965            system_package: cli.system_package,
966            package_in_compiled: cli.package_in_compiled,
967            package_only: cli.package_only,
968            no_assemble: cli.no_assemble,
969            license_dataset_path: cli.license_dataset_path.clone(),
970            reindex: cli.reindex,
971            no_license_index_cache: cli.no_license_index_cache,
972            license_text: cli.license_text,
973            license_text_diagnostics: cli.license_text_diagnostics,
974            license_diagnostics: cli.license_diagnostics,
975            unknown_licenses: cli.unknown_licenses,
976            no_sequence_matching: cli.no_sequence_matching,
977            license_score: cli.license_score,
978            license_url_template: cli.license_url_template.clone(),
979            filter_clues: cli.filter_clues,
980            ignore_author: cli.ignore_author.clone(),
981            ignore_copyright_holder: cli.ignore_copyright_holder.clone(),
982            only_findings: cli.only_findings,
983            mark_source: cli.mark_source,
984            classify: cli.classify,
985            summary: cli.summary,
986            license_clarity_score: cli.license_clarity_score,
987            license_references: cli.license_references,
988            license_policy: cli.license_policy.clone(),
989            fail_on: cli.fail_on.map(FailOn::threshold),
990            tallies: cli.tallies,
991            tallies_key_files: cli.tallies_key_files,
992            tallies_with_details: cli.tallies_with_details,
993            facet: cli.facet.clone(),
994            tallies_by_facet: cli.tallies_by_facet,
995            generated: cli.generated,
996            license: cli.license,
997            copyright: cli.copyright,
998            email: cli.email,
999            max_email: cli.max_email,
1000            url: cli.url,
1001            max_url: cli.max_url,
1002            scan_bounds: crate::app::request::ScanBounds::default(),
1003        }
1004    }
1005}
1006
1007fn push_bool_option(options: &mut Vec<(String, JsonValue)>, key: &str, enabled: bool) {
1008    if enabled {
1009        options.push((key.to_string(), JsonValue::Bool(true)));
1010    }
1011}
1012
1013fn push_string_option(options: &mut Vec<(String, JsonValue)>, key: &str, value: Option<&String>) {
1014    if let Some(value) = value {
1015        options.push((key.to_string(), JsonValue::String(value.clone())));
1016    }
1017}
1018
1019fn push_non_default_string_option(
1020    options: &mut Vec<(String, JsonValue)>,
1021    key: &str,
1022    value: &str,
1023    default: &str,
1024) {
1025    if value != default {
1026        options.push((key.to_string(), JsonValue::String(value.to_string())));
1027    }
1028}
1029
1030fn push_array_option(options: &mut Vec<(String, JsonValue)>, key: &str, values: &[String]) {
1031    if !values.is_empty() {
1032        options.push((
1033            key.to_string(),
1034            JsonValue::Array(values.iter().cloned().map(JsonValue::String).collect()),
1035        ));
1036    }
1037}
1038
1039fn push_non_default_usize_option(
1040    options: &mut Vec<(String, JsonValue)>,
1041    key: &str,
1042    value: usize,
1043    default: usize,
1044) {
1045    if value != default {
1046        options.push((key.to_string(), JsonValue::Number(value.into())));
1047    }
1048}
1049
1050fn push_non_default_u8_option(
1051    options: &mut Vec<(String, JsonValue)>,
1052    key: &str,
1053    value: u8,
1054    default: u8,
1055) {
1056    if value != default {
1057        options.push((key.to_string(), JsonValue::Number(value.into())));
1058    }
1059}
1060
1061fn push_non_default_process_mode_option(
1062    options: &mut Vec<(String, JsonValue)>,
1063    key: &str,
1064    value: ProcessMode,
1065    default: ProcessMode,
1066) {
1067    if value != default {
1068        options.push((key.to_string(), JsonValue::Number(value.to_i32().into())));
1069    }
1070}
1071
1072fn push_non_default_f64_option(
1073    options: &mut Vec<(String, JsonValue)>,
1074    key: &str,
1075    value: f64,
1076    default: f64,
1077) {
1078    if (value - default).abs() > f64::EPSILON
1079        && let Some(number) = JsonNumber::from_f64(value)
1080    {
1081        options.push((key.to_string(), JsonValue::Number(number)));
1082    }
1083}
1084
1085#[cfg(test)]
1086mod tests;