malwaredb 0.3.2

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

mod data_loader;
mod updater;
use crate::cli::config::Config;

use std::process::ExitCode;

use anyhow::Result;
use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
pub struct VtOption {
    #[clap(subcommand)]
    pub vt: Vt,
}

/// Modes of operation for VT: either we query the data or load data already queried and saved.
#[derive(Subcommand, Debug)]
pub enum Vt {
    /// Run the VT update agent which queries VT for reports for files in the database and exits when finished
    Updater(updater::Updater),

    /// Load serialized Virus Total data into Malware DB, if the sample file is already known to Malware DB
    Loader(data_loader::DataLoader),
}

impl VtOption {
    pub async fn execute(self) -> Result<ExitCode> {
        match self.vt {
            Vt::Updater(u) => u.execute().await,
            Vt::Loader(l) => l.execute().await,
        }
    }
}

/// Virus Total config either from disk or command line for VT operations outside the normal running of Malware DB
#[derive(Subcommand, Debug)]
enum VtConfig {
    /// Load Virus Total config from a file
    Load(Load),

    /// Virus Total config is passed on the command line
    Config(Config),
}

#[derive(Parser, Debug)]
struct Load {
    #[arg(value_name = "FILE", value_hint = clap::ValueHint::FilePath)]
    pub file: std::path::PathBuf,
}

impl Load {
    fn config(self) -> Result<Config> {
        Config::from_file(&self.file)
    }
}

impl VtConfig {
    pub fn config(self) -> Result<Config> {
        match self {
            VtConfig::Load(loader) => loader.config(),
            VtConfig::Config(config) => Ok(config),
        }
    }
}