fn main()

in license-scan/src/main.rs [59:142]


fn main() -> Result<()> {
    let opt = Opt::from_args();

    let clarify = match opt.clarify {
        None => Clarifications::default(),
        Some(path) => toml::from_slice(&fs::read(path)?)?,
    };

    let mut store = Store::new();
    store
        .load_spdx(&opt.spdx_data, false)
        .map_err(|err| err.compat())?;
    let scanner = ScanStrategy::new(&store)
        .confidence_threshold(0.93)
        .shallow_limit(1.0)
        .optimize(true);

    match opt.cmd {
        Cmd::GoVendor { vendor_dir } => {
            for repo in scan_go_vendor_repos(&vendor_dir)? {
                write_attribution(
                    repo.to_str().with_context(|| {
                        format!(
                            "package name is not valid UTF-8; lossy version is '{}'",
                            repo.to_string_lossy()
                        )
                    })?,
                    &vendor_dir.join(&repo),
                    &opt.out_dir.join(&repo),
                    &scanner,
                    &clarify,
                    None,
                )?;
            }
            Ok(())
        }
        Cmd::Cargo {
            manifest_path,
            locked,
            offline,
        } => {
            let mut builder = cargo_metadata::MetadataCommand::new();
            builder.manifest_path(manifest_path);
            if locked {
                builder.other_options(vec!["--locked".to_owned()]);
            }
            if offline {
                builder.other_options(vec!["--offline".to_owned()]);
            }
            let metadata = builder.exec()?;
            for package in metadata.packages {
                if package.source.is_none() {
                    if let Some(publish) = package.publish {
                        if publish.is_empty() {
                            // `package.source` is None if the project is a local project;
                            // `package.publish` is an empty Vec if `publish = false` is set
                            continue;
                        }
                    }
                }
                write_attribution(
                    &package.name,
                    package
                        .manifest_path
                        .parent()
                        .expect("expected a path to Cargo.toml to have a parent"),
                    &opt.out_dir
                        .join(format!("{}-{}", package.name, package.version)),
                    &scanner,
                    &clarify,
                    if let Some(license) = package.license {
                        Some(Expression::parse(&unslash(&license)).map_err(|err| {
                            // spdx errors use the lifetime of the string
                            anyhow!(err.to_string())
                        })?)
                    } else {
                        None
                    },
                )?;
            }
            Ok(())
        }
    }
}