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#[derive(clap::ValueEnum, Debug, Clone, Copy, PartialEq, Eq, Default)]
238pub enum CompatibilityMode {
239    #[default]
240    Native,
241    Scancode,
242}
243
244impl CompatibilityMode {
245    fn as_str(self) -> &'static str {
246        match self {
247            Self::Native => "native",
248            Self::Scancode => "scancode",
249        }
250    }
251}
252
253#[derive(Args, Debug, Clone)]
254pub struct ServeArgs {
255    /// Bind the service shell to HOST:PORT.
256    #[arg(long = "bind", value_name = "ADDR", default_value = "127.0.0.1:8080")]
257    pub bind: String,
258
259    /// Allow paths, URL, and repository inputs when bound beyond localhost.
260    #[arg(long = "allow-privileged-inputs")]
261    pub allow_privileged_inputs: bool,
262
263    #[command(flatten)]
264    pub verbosity: VerbosityFlags,
265}
266
267#[derive(Args, Debug, Clone)]
268#[command(
269    group(
270        ArgGroup::new("output")
271            .required(true)
272            .multiple(true)
273            .args([
274                "output_json",
275                "output_json_pp",
276                "output_json_lines",
277                "output_yaml",
278                "output_debian",
279                "output_html",
280                "output_spdx_tv",
281                "output_spdx_rdf",
282                "output_cyclonedx",
283                "output_cyclonedx_xml",
284                "custom_output"
285            ])
286    ),
287    after_help = PDF_OXIDE_LOG_HELP
288)]
289pub struct ScanArgs {
290    /// File or directory paths to scan
291    #[arg(required = false)]
292    pub dir_path: Vec<String>,
293
294    /// Write scan output as compact JSON to FILE
295    #[arg(long = "json", value_name = "FILE", allow_hyphen_values = true)]
296    pub output_json: Option<String>,
297
298    /// Write scan output as pretty-printed JSON to FILE
299    #[arg(long = "json-pp", value_name = "FILE", allow_hyphen_values = true)]
300    pub output_json_pp: Option<String>,
301
302    /// Write scan output as JSON Lines to FILE
303    #[arg(long = "json-lines", value_name = "FILE", allow_hyphen_values = true)]
304    pub output_json_lines: Option<String>,
305
306    /// Write scan output as YAML to FILE
307    #[arg(long = "yaml", value_name = "FILE", allow_hyphen_values = true)]
308    pub output_yaml: Option<String>,
309
310    /// Write scan output in machine-readable Debian copyright format to FILE (requires --license, --copyright, and --license-text)
311    #[arg(
312        long = "debian",
313        value_name = "FILE",
314        allow_hyphen_values = true,
315        requires_all = ["copyright", "license", "license_text"]
316    )]
317    pub output_debian: Option<String>,
318
319    /// Write scan output as HTML report to FILE
320    #[arg(long = "html", value_name = "FILE", allow_hyphen_values = true)]
321    pub output_html: Option<String>,
322
323    /// Write scan output as SPDX tag/value to FILE
324    #[arg(long = "spdx-tv", value_name = "FILE", allow_hyphen_values = true)]
325    pub output_spdx_tv: Option<String>,
326
327    /// Write scan output as SPDX RDF/XML to FILE
328    #[arg(long = "spdx-rdf", value_name = "FILE", allow_hyphen_values = true)]
329    pub output_spdx_rdf: Option<String>,
330
331    /// Write scan output as CycloneDX JSON to FILE
332    #[arg(long = "cyclonedx", value_name = "FILE", allow_hyphen_values = true)]
333    pub output_cyclonedx: Option<String>,
334
335    /// Write scan output as CycloneDX XML to FILE
336    #[arg(
337        long = "cyclonedx-xml",
338        value_name = "FILE",
339        allow_hyphen_values = true
340    )]
341    pub output_cyclonedx_xml: Option<String>,
342
343    /// Write scan output to FILE formatted with the custom template
344    #[arg(
345        long = "custom-output",
346        value_name = "FILE",
347        requires = "custom_template",
348        allow_hyphen_values = true
349    )]
350    pub custom_output: Option<String>,
351
352    /// Use this template FILE with --custom-output
353    #[arg(
354        long = "custom-template",
355        value_name = "FILE",
356        requires = "custom_output"
357    )]
358    pub custom_template: Option<String>,
359
360    /// Maximum recursion depth (0 means no depth limit)
361    #[arg(short, long, default_value = "0")]
362    pub max_depth: usize,
363
364    #[arg(short = 'n', long, default_value_t = ProcessMode::default_value(), value_parser = parse_processes, allow_hyphen_values = true)]
365    pub processes: ProcessMode,
366
367    #[arg(long, default_value_t = 120.0)]
368    pub timeout: f64,
369
370    #[arg(short, long, conflicts_with = "verbose")]
371    pub quiet: bool,
372
373    /// Emit verbose diagnostics, including the per-phase timing breakdown and, on a TTY, per-file scan detail.
374    #[arg(short, long, conflicts_with = "quiet")]
375    pub verbose: bool,
376
377    #[arg(long, conflicts_with = "full_root")]
378    pub strip_root: bool,
379
380    #[arg(long, conflicts_with = "strip_root")]
381    pub full_root: bool,
382
383    /// Exclude patterns (ScanCode-compatible alias: --ignore)
384    #[arg(long = "exclude", visible_alias = "ignore", value_delimiter = ',')]
385    pub exclude: Vec<String>,
386
387    /// Include files matching PATTERN. Use `**` when you want recursion across directories.
388    #[arg(long, value_delimiter = ',')]
389    pub include: Vec<String>,
390
391    /// Read selected scan paths from FILE (or '-' for stdin), relative to the explicit scan root.
392    #[arg(long = "paths-file", value_name = "FILE", allow_hyphen_values = true)]
393    pub paths_file: Vec<String>,
394
395    #[arg(long = "cache-dir", value_name = "PATH")]
396    pub cache_dir: Option<String>,
397
398    #[arg(long = "cache-clear")]
399    pub cache_clear: bool,
400
401    #[arg(long = "incremental")]
402    pub incremental: bool,
403
404    /// Trust size + mtime for incremental reuse, skipping the content re-hash of
405    /// unchanged files. Speeds up warm incremental re-scans at the cost of
406    /// missing the rare edit that keeps the same size and mtime tick. Such a miss
407    /// is not permanent: the next scan without this flag re-hashes and detects it.
408    /// Default off keeps the paranoid full-hash check so scans stay reproducible.
409    #[arg(long = "cache-trust-mtime", requires = "incremental")]
410    pub cache_trust_mtime: bool,
411
412    /// Maximum number of file and directory scan details kept in memory during
413    /// the in-scan file-processing window before the rest spill to disk.
414    /// Use 0 for unlimited memory or -1 for disk-only spill during the scan.
415    /// This bounds only the in-scan working set, NOT total or peak process
416    /// memory: assembly, summary, and output reconstitute the full result set
417    /// after the scan, so peak RSS is not bounded by this flag.
418    #[arg(
419        long = "max-in-memory",
420        value_name = "INT",
421        default_value_t = MemoryMode::Limit(10000),
422        value_parser = parse_max_in_memory,
423        allow_hyphen_values = true
424    )]
425    pub max_in_memory: MemoryMode,
426
427    /// Collect file information such as checksums, type hints, and source/script flags.
428    #[arg(short = 'i', long)]
429    pub info: bool,
430
431    /// Load one or more existing ScanCode-style JSON scans instead of rescanning inputs.
432    #[arg(long)]
433    pub from_json: bool,
434
435    /// Scan input for application package and dependency manifests, lockfiles and related data
436    #[arg(short = 'p', long)]
437    pub package: bool,
438
439    /// Select a compatibility bundle for intentional Provenant-vs-ScanCode behavior differences.
440    #[arg(
441        long = "compat-mode",
442        visible_alias = "compat",
443        value_enum,
444        default_value_t = CompatibilityMode::Native
445    )]
446    pub compat_mode: CompatibilityMode,
447
448    /// Scan input for installed system package databases (RPM, dpkg, apk, etc.)
449    #[arg(long = "system-package")]
450    pub system_package: bool,
451
452    /// Scan supported compiled Go and Rust binaries for embedded package metadata.
453    #[arg(long = "package-in-compiled")]
454    pub package_in_compiled: bool,
455
456    /// Scan for system and application package data and skip license/copyright detection and top-level package creation.
457    #[arg(
458        long = "package-only",
459        conflicts_with_all = ["license", "summary", "package", "system_package"]
460    )]
461    pub package_only: bool,
462
463    /// Disable package assembly (merging related manifest/lockfiles into packages)
464    #[arg(long)]
465    pub no_assemble: bool,
466
467    /// Path to a custom license dataset root containing manifest.json, rules/, and licenses/.
468    /// If not specified, uses the built-in embedded license index.
469    #[arg(
470        long = "license-dataset-path",
471        value_name = "PATH",
472        requires = "license"
473    )]
474    pub license_dataset_path: Option<String>,
475
476    /// Force rebuild of the license index cache, ignoring any existing cache.
477    #[arg(long)]
478    pub reindex: bool,
479
480    /// Build the license index in memory for this run without reading or writing persistent cache files.
481    #[arg(long = "no-license-index-cache")]
482    pub no_license_index_cache: bool,
483
484    /// Include matched text in license detection output
485    #[arg(long = "license-text", requires = "license")]
486    pub license_text: bool,
487
488    #[arg(long = "license-text-diagnostics", requires = "license_text")]
489    pub license_text_diagnostics: bool,
490
491    #[arg(long = "license-diagnostics", requires = "license")]
492    pub license_diagnostics: bool,
493
494    #[arg(long = "unknown-licenses", requires = "license")]
495    pub unknown_licenses: bool,
496
497    /// Disable approximate sequence matching during `--license` detection.
498    #[arg(long = "no-sequence-matching", requires = "license")]
499    pub no_sequence_matching: bool,
500
501    #[arg(
502        long = "license-score",
503        default_value_t = 0,
504        requires = "license",
505        value_parser = clap::value_parser!(u8).range(0..=100)
506    )]
507    pub license_score: u8,
508
509    #[arg(
510        long = "license-url-template",
511        default_value = DEFAULT_LICENSEDB_URL_TEMPLATE,
512        requires = "license"
513    )]
514    pub license_url_template: String,
515
516    #[arg(long)]
517    pub filter_clues: bool,
518
519    #[arg(
520        long = "ignore-author",
521        value_name = "PATTERN",
522        help = "Ignore a file and all its findings if an author matches the regex PATTERN"
523    )]
524    pub ignore_author: Vec<String>,
525
526    #[arg(
527        long = "ignore-copyright-holder",
528        value_name = "PATTERN",
529        help = "Ignore a file and all its findings if a copyright holder matches the regex PATTERN"
530    )]
531    pub ignore_copyright_holder: Vec<String>,
532
533    #[arg(long)]
534    pub only_findings: bool,
535
536    #[arg(long, requires = "info")]
537    pub mark_source: bool,
538
539    #[arg(long)]
540    pub classify: bool,
541
542    #[arg(long, requires = "classify")]
543    pub summary: bool,
544
545    #[arg(long = "license-clarity-score", requires = "classify")]
546    pub license_clarity_score: bool,
547
548    #[arg(long = "license-references", requires = "license")]
549    pub license_references: bool,
550
551    /// Evaluate file license detections against a YAML license policy file.
552    #[arg(
553        long = "license-policy",
554        value_name = "FILE",
555        value_parser = parse_license_policy_arg
556    )]
557    pub license_policy: Option<String>,
558
559    #[arg(long)]
560    pub tallies: bool,
561
562    #[arg(long = "tallies-key-files", requires_all = ["tallies", "classify"])]
563    pub tallies_key_files: bool,
564
565    #[arg(long = "tallies-with-details")]
566    pub tallies_with_details: bool,
567
568    #[arg(long = "facet", value_name = "<facet>=<pattern>")]
569    pub facet: Vec<String>,
570
571    #[arg(long = "tallies-by-facet", requires_all = ["facet", "tallies"])]
572    pub tallies_by_facet: bool,
573
574    #[arg(long)]
575    pub generated: bool,
576
577    /// Scan input for licenses
578    #[arg(short = 'l', long)]
579    pub license: bool,
580
581    #[arg(short = 'c', long)]
582    pub copyright: bool,
583
584    /// Scan input for email addresses
585    #[arg(short = 'e', long)]
586    pub email: bool,
587
588    /// Report only up to INT emails found in a file. Use 0 for no limit.
589    #[arg(long, default_value_t = 50, requires = "email")]
590    pub max_email: usize,
591
592    /// Scan input for URLs
593    #[arg(short = 'u', long)]
594    pub url: bool,
595
596    /// Report only up to INT URLs found in a file. Use 0 for no limit.
597    #[arg(long, default_value_t = 50, requires = "url")]
598    pub max_url: usize,
599}
600
601impl Cli {
602    pub fn parse() -> Self {
603        <Self as Parser>::parse_from(rewrite_args_for_default_scan(std::env::args_os()))
604    }
605
606    pub fn try_parse_from<I, T>(itr: I) -> Result<Self, clap::Error>
607    where
608        I: IntoIterator<Item = T>,
609        T: Into<OsString>,
610    {
611        <Self as Parser>::try_parse_from(rewrite_args_for_default_scan(itr))
612    }
613
614    pub(crate) fn scan_args(&self) -> Option<&ScanArgs> {
615        match &self.command {
616            Command::Scan(scan_args) => Some(scan_args.as_ref()),
617            Command::Serve(_)
618            | Command::Compare(_)
619            | Command::ShowAttribution
620            | Command::ExportLicenseDataset(_) => None,
621        }
622    }
623}
624
625#[cfg(test)]
626impl Deref for Cli {
627    type Target = ScanArgs;
628
629    fn deref(&self) -> &Self::Target {
630        self.scan_args()
631            .expect("scan arguments are only available for the scan command")
632    }
633}
634
635fn rewrite_args_for_default_scan<I, T>(itr: I) -> Vec<OsString>
636where
637    I: IntoIterator<Item = T>,
638    T: Into<OsString>,
639{
640    let mut args: Vec<OsString> = itr.into_iter().map(Into::into).collect();
641    if args.len() <= 1 {
642        return args;
643    }
644
645    let first = args[1].to_string_lossy();
646    if matches!(
647        first.as_ref(),
648        "scan"
649            | "serve"
650            | "compare"
651            | "show-attribution"
652            | "export-license-dataset"
653            | "help"
654            | "-h"
655            | "--help"
656            | "-V"
657            | "--version"
658    ) {
659        return args;
660    }
661
662    if first.starts_with('-') || Path::new(first.as_ref()).exists() {
663        args.insert(1, OsString::from("scan"));
664    }
665
666    args
667}
668
669fn parse_max_in_memory(value: &str) -> Result<MemoryMode, String> {
670    let parsed = value
671        .parse::<i64>()
672        .map_err(|_| format!("invalid integer value: {value}"))?;
673    if parsed < -1 {
674        return Err("--max-in-memory must be -1, 0, or a positive integer".to_string());
675    }
676    match parsed {
677        -1 => Ok(MemoryMode::StreamUnlimited),
678        0 => Ok(MemoryMode::CollectFirst),
679        n if n > 0 => Ok(MemoryMode::Limit(usize::try_from(n).unwrap_or(usize::MAX))),
680        _ => Ok(MemoryMode::CollectFirst),
681    }
682}
683
684impl ScanArgs {
685    pub(crate) fn output_targets(&self) -> Vec<OutputTarget> {
686        let mut targets = Vec::new();
687
688        if let Some(file) = &self.output_json {
689            targets.push(OutputTarget {
690                format: OutputFormat::Json,
691                file: file.clone(),
692                custom_template: None,
693            });
694        }
695
696        if let Some(file) = &self.output_json_pp {
697            targets.push(OutputTarget {
698                format: OutputFormat::JsonPretty,
699                file: file.clone(),
700                custom_template: None,
701            });
702        }
703
704        if let Some(file) = &self.output_json_lines {
705            targets.push(OutputTarget {
706                format: OutputFormat::JsonLines,
707                file: file.clone(),
708                custom_template: None,
709            });
710        }
711
712        if let Some(file) = &self.output_yaml {
713            targets.push(OutputTarget {
714                format: OutputFormat::Yaml,
715                file: file.clone(),
716                custom_template: None,
717            });
718        }
719
720        if let Some(file) = &self.output_debian {
721            targets.push(OutputTarget {
722                format: OutputFormat::Debian,
723                file: file.clone(),
724                custom_template: None,
725            });
726        }
727
728        if let Some(file) = &self.output_html {
729            targets.push(OutputTarget {
730                format: OutputFormat::Html,
731                file: file.clone(),
732                custom_template: None,
733            });
734        }
735
736        if let Some(file) = &self.output_spdx_tv {
737            targets.push(OutputTarget {
738                format: OutputFormat::SpdxTv,
739                file: file.clone(),
740                custom_template: None,
741            });
742        }
743
744        if let Some(file) = &self.output_spdx_rdf {
745            targets.push(OutputTarget {
746                format: OutputFormat::SpdxRdf,
747                file: file.clone(),
748                custom_template: None,
749            });
750        }
751
752        if let Some(file) = &self.output_cyclonedx {
753            targets.push(OutputTarget {
754                format: OutputFormat::CycloneDxJson,
755                file: file.clone(),
756                custom_template: None,
757            });
758        }
759
760        if let Some(file) = &self.output_cyclonedx_xml {
761            targets.push(OutputTarget {
762                format: OutputFormat::CycloneDxXml,
763                file: file.clone(),
764                custom_template: None,
765            });
766        }
767
768        if let Some(file) = &self.custom_output {
769            targets.push(OutputTarget {
770                format: OutputFormat::CustomTemplate,
771                file: file.clone(),
772                custom_template: self.custom_template.clone(),
773            });
774        }
775
776        targets
777    }
778
779    pub(crate) fn output_header_options(&self) -> JsonMap<String, JsonValue> {
780        let mut options = JsonMap::new();
781        if !self.dir_path.is_empty() {
782            options.insert(
783                "input".to_string(),
784                JsonValue::Array(
785                    self.dir_path
786                        .iter()
787                        .cloned()
788                        .map(JsonValue::String)
789                        .collect(),
790                ),
791            );
792        }
793
794        let mut flags = Vec::new();
795
796        push_string_option(&mut flags, "--cache-dir", self.cache_dir.as_ref());
797        push_bool_option(&mut flags, "--cache-clear", self.cache_clear);
798        push_bool_option(&mut flags, "--cache-trust-mtime", self.cache_trust_mtime);
799        push_bool_option(&mut flags, "--classify", self.classify);
800        push_string_option(&mut flags, "--custom-output", self.custom_output.as_ref());
801        push_string_option(
802            &mut flags,
803            "--custom-template",
804            self.custom_template.as_ref(),
805        );
806        push_bool_option(&mut flags, "--copyright", self.copyright);
807        if self.compat_mode != CompatibilityMode::Native {
808            flags.push((
809                "--compat-mode".to_string(),
810                JsonValue::String(self.compat_mode.as_str().to_string()),
811            ));
812        }
813        push_string_option(&mut flags, "--cyclonedx", self.output_cyclonedx.as_ref());
814        push_string_option(
815            &mut flags,
816            "--cyclonedx-xml",
817            self.output_cyclonedx_xml.as_ref(),
818        );
819        push_string_option(&mut flags, "--debian", self.output_debian.as_ref());
820        push_bool_option(&mut flags, "--email", self.email);
821        push_array_option(&mut flags, "--facet", &self.facet);
822        push_bool_option(&mut flags, "--filter-clues", self.filter_clues);
823        push_bool_option(&mut flags, "--from-json", self.from_json);
824        push_bool_option(&mut flags, "--full-root", self.full_root);
825        push_bool_option(&mut flags, "--generated", self.generated);
826        push_string_option(&mut flags, "--html", self.output_html.as_ref());
827        push_array_option(&mut flags, "--ignore", &self.exclude);
828        push_array_option(&mut flags, "--ignore-author", &self.ignore_author);
829        push_array_option(
830            &mut flags,
831            "--ignore-copyright-holder",
832            &self.ignore_copyright_holder,
833        );
834        push_bool_option(&mut flags, "--incremental", self.incremental);
835        push_array_option(&mut flags, "--include", &self.include);
836        push_bool_option(&mut flags, "--info", self.info);
837        push_string_option(&mut flags, "--json", self.output_json.as_ref());
838        push_string_option(&mut flags, "--json-lines", self.output_json_lines.as_ref());
839        push_string_option(&mut flags, "--json-pp", self.output_json_pp.as_ref());
840        push_bool_option(&mut flags, "--license", self.license);
841        push_bool_option(
842            &mut flags,
843            "--license-clarity-score",
844            self.license_clarity_score,
845        );
846        push_bool_option(
847            &mut flags,
848            "--license-diagnostics",
849            self.license_diagnostics,
850        );
851        push_string_option(
852            &mut flags,
853            "--license-dataset-path",
854            self.license_dataset_path.as_ref(),
855        );
856        push_string_option(&mut flags, "--license-policy", self.license_policy.as_ref());
857        push_bool_option(
858            &mut flags,
859            "--no-license-index-cache",
860            self.no_license_index_cache,
861        );
862        push_bool_option(&mut flags, "--license-references", self.license_references);
863        push_bool_option(&mut flags, "--reindex", self.reindex);
864        push_non_default_u8_option(&mut flags, "--license-score", self.license_score, 0);
865        push_bool_option(&mut flags, "--license-text", self.license_text);
866        push_bool_option(
867            &mut flags,
868            "--license-text-diagnostics",
869            self.license_text_diagnostics,
870        );
871        push_non_default_string_option(
872            &mut flags,
873            "--license-url-template",
874            &self.license_url_template,
875            DEFAULT_LICENSEDB_URL_TEMPLATE,
876        );
877        push_non_default_usize_option(&mut flags, "--max-depth", self.max_depth, 0);
878        match self.max_in_memory {
879            MemoryMode::Limit(10000) => {}
880            MemoryMode::CollectFirst => {
881                flags.push(("--max-in-memory".to_string(), JsonValue::Number(0.into())));
882            }
883            MemoryMode::StreamUnlimited => {
884                flags.push((
885                    "--max-in-memory".to_string(),
886                    JsonValue::Number((-1i64).into()),
887                ));
888            }
889            MemoryMode::Limit(n) => {
890                flags.push(("--max-in-memory".to_string(), JsonValue::Number(n.into())));
891            }
892        }
893        if self.email {
894            push_non_default_usize_option(&mut flags, "--max-email", self.max_email, 50);
895        }
896        if self.url {
897            push_non_default_usize_option(&mut flags, "--max-url", self.max_url, 50);
898        }
899        push_bool_option(&mut flags, "--mark-source", self.mark_source);
900        push_bool_option(&mut flags, "--no-assemble", self.no_assemble);
901        push_bool_option(&mut flags, "--only-findings", self.only_findings);
902        push_bool_option(&mut flags, "--package", self.package);
903        push_bool_option(
904            &mut flags,
905            "--package-in-compiled",
906            self.package_in_compiled,
907        );
908        push_bool_option(&mut flags, "--package-only", self.package_only);
909        push_array_option(&mut flags, "--paths-file", &self.paths_file);
910        push_non_default_process_mode_option(
911            &mut flags,
912            "--processes",
913            self.processes,
914            ProcessMode::default_value(),
915        );
916        push_bool_option(&mut flags, "--quiet", self.quiet);
917        push_string_option(&mut flags, "--spdx-rdf", self.output_spdx_rdf.as_ref());
918        push_string_option(&mut flags, "--spdx-tv", self.output_spdx_tv.as_ref());
919        push_bool_option(&mut flags, "--strip-root", self.strip_root);
920        push_bool_option(&mut flags, "--summary", self.summary);
921        push_bool_option(&mut flags, "--system-package", self.system_package);
922        push_bool_option(&mut flags, "--tallies", self.tallies);
923        push_bool_option(&mut flags, "--tallies-by-facet", self.tallies_by_facet);
924        push_bool_option(&mut flags, "--tallies-key-files", self.tallies_key_files);
925        push_bool_option(
926            &mut flags,
927            "--tallies-with-details",
928            self.tallies_with_details,
929        );
930        push_non_default_f64_option(&mut flags, "--timeout", self.timeout, 120.0);
931        push_bool_option(&mut flags, "--unknown-licenses", self.unknown_licenses);
932        push_bool_option(
933            &mut flags,
934            "--no-sequence-matching",
935            self.no_sequence_matching,
936        );
937        push_bool_option(&mut flags, "--url", self.url);
938        push_bool_option(&mut flags, "--verbose", self.verbose);
939        push_string_option(&mut flags, "--yaml", self.output_yaml.as_ref());
940
941        flags.sort_by(|left, right| left.0.cmp(&right.0));
942        for (key, value) in flags {
943            options.insert(key, value);
944        }
945
946        options
947    }
948}
949
950impl From<&ScanArgs> for ScanRequest {
951    fn from(cli: &ScanArgs) -> Self {
952        Self {
953            input_paths: cli.dir_path.clone(),
954            input_mode: if cli.from_json {
955                InputMode::FromJson
956            } else {
957                InputMode::Native
958            },
959            output_targets: cli.output_targets(),
960            output_header_options: cli.output_header_options(),
961            progress_mode: if cli.quiet {
962                crate::progress::ProgressMode::Quiet
963            } else if cli.verbose {
964                crate::progress::ProgressMode::Verbose
965            } else {
966                crate::progress::ProgressMode::Default
967            },
968            process_mode: cli.processes,
969            timeout_seconds: cli.timeout,
970            quiet: cli.quiet,
971            verbose: cli.verbose,
972            strip_root: cli.strip_root,
973            full_root: cli.full_root,
974            exclude: cli.exclude.clone(),
975            include: cli.include.clone(),
976            paths_files: cli.paths_file.clone(),
977            respect_process_cache_env: true,
978            cache_dir: cli.cache_dir.clone(),
979            cache_clear: cli.cache_clear,
980            incremental: cli.incremental,
981            cache_trust_mtime: cli.cache_trust_mtime,
982            max_depth: cli.max_depth,
983            max_in_memory: cli.max_in_memory,
984            info: cli.info,
985            package: cli.package,
986            system_package: cli.system_package,
987            package_in_compiled: cli.package_in_compiled,
988            package_only: cli.package_only,
989            no_assemble: cli.no_assemble,
990            license_dataset_path: cli.license_dataset_path.clone(),
991            reindex: cli.reindex,
992            no_license_index_cache: cli.no_license_index_cache,
993            license_text: cli.license_text,
994            license_text_diagnostics: cli.license_text_diagnostics,
995            license_diagnostics: cli.license_diagnostics,
996            unknown_licenses: cli.unknown_licenses,
997            no_sequence_matching: cli.no_sequence_matching,
998            license_score: cli.license_score,
999            license_url_template: cli.license_url_template.clone(),
1000            filter_clues: cli.filter_clues,
1001            ignore_author: cli.ignore_author.clone(),
1002            ignore_copyright_holder: cli.ignore_copyright_holder.clone(),
1003            only_findings: cli.only_findings,
1004            mark_source: cli.mark_source,
1005            classify: cli.classify,
1006            summary: cli.summary,
1007            license_clarity_score: cli.license_clarity_score,
1008            license_references: cli.license_references,
1009            license_policy: cli.license_policy.clone(),
1010            tallies: cli.tallies,
1011            tallies_key_files: cli.tallies_key_files,
1012            tallies_with_details: cli.tallies_with_details,
1013            facet: cli.facet.clone(),
1014            tallies_by_facet: cli.tallies_by_facet,
1015            generated: cli.generated,
1016            license: cli.license,
1017            copyright: cli.copyright,
1018            email: cli.email,
1019            max_email: cli.max_email,
1020            url: cli.url,
1021            max_url: cli.max_url,
1022            scan_bounds: crate::app::request::ScanBounds::default(),
1023        }
1024    }
1025}
1026
1027fn push_bool_option(options: &mut Vec<(String, JsonValue)>, key: &str, enabled: bool) {
1028    if enabled {
1029        options.push((key.to_string(), JsonValue::Bool(true)));
1030    }
1031}
1032
1033fn push_string_option(options: &mut Vec<(String, JsonValue)>, key: &str, value: Option<&String>) {
1034    if let Some(value) = value {
1035        options.push((key.to_string(), JsonValue::String(value.clone())));
1036    }
1037}
1038
1039fn push_non_default_string_option(
1040    options: &mut Vec<(String, JsonValue)>,
1041    key: &str,
1042    value: &str,
1043    default: &str,
1044) {
1045    if value != default {
1046        options.push((key.to_string(), JsonValue::String(value.to_string())));
1047    }
1048}
1049
1050fn push_array_option(options: &mut Vec<(String, JsonValue)>, key: &str, values: &[String]) {
1051    if !values.is_empty() {
1052        options.push((
1053            key.to_string(),
1054            JsonValue::Array(values.iter().cloned().map(JsonValue::String).collect()),
1055        ));
1056    }
1057}
1058
1059fn push_non_default_usize_option(
1060    options: &mut Vec<(String, JsonValue)>,
1061    key: &str,
1062    value: usize,
1063    default: usize,
1064) {
1065    if value != default {
1066        options.push((key.to_string(), JsonValue::Number(value.into())));
1067    }
1068}
1069
1070fn push_non_default_u8_option(
1071    options: &mut Vec<(String, JsonValue)>,
1072    key: &str,
1073    value: u8,
1074    default: u8,
1075) {
1076    if value != default {
1077        options.push((key.to_string(), JsonValue::Number(value.into())));
1078    }
1079}
1080
1081fn push_non_default_process_mode_option(
1082    options: &mut Vec<(String, JsonValue)>,
1083    key: &str,
1084    value: ProcessMode,
1085    default: ProcessMode,
1086) {
1087    if value != default {
1088        options.push((key.to_string(), JsonValue::Number(value.to_i32().into())));
1089    }
1090}
1091
1092fn push_non_default_f64_option(
1093    options: &mut Vec<(String, JsonValue)>,
1094    key: &str,
1095    value: f64,
1096    default: f64,
1097) {
1098    if (value - default).abs() > f64::EPSILON
1099        && let Some(number) = JsonNumber::from_f64(value)
1100    {
1101        options.push((key.to_string(), JsonValue::Number(number)));
1102    }
1103}
1104
1105#[cfg(test)]
1106mod tests;