malwaredb 0.3.2

Service for storing malicious, benign, or unknown files and related metadata and relationships.
// SPDX-License-Identifier: Apache-2.0

use crate::cli::config::Config;
use malwaredb_server::vt::VtUpdater;

use std::path::PathBuf;
use std::process::ExitCode;

use anyhow::Context;
use clap::Parser;
use malwaredb_virustotal::common::ReportResponseHeader;
use malwaredb_virustotal::filereport::ScanResultAttributes;

#[derive(Parser, Debug)]
pub struct DataLoader {
    /// Virus Total Report
    #[arg(short, long)]
    pub report: PathBuf,

    /// Configuration for Virus Total
    #[clap(subcommand)]
    cmd: Option<super::VtConfig>,
}

impl DataLoader {
    pub async fn execute(self) -> anyhow::Result<ExitCode> {
        let report =
            std::fs::read_to_string(&self.report).context("failed to read VT report file")?;
        let report: ReportResponseHeader<ScanResultAttributes> =
            toml::from_str(&report).context("failed to parse VT report")?;

        let cfg = match self.cmd {
            Some(config) => config.config()?,
            None => Config::from_found_files()?,
        };

        let updater: VtUpdater = cfg.into_state().await?.try_into()?;
        updater.loader(&report.attributes).await?;

        Ok(ExitCode::SUCCESS)
    }
}