malwaredb 0.3.2

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

use malwaredb_server::State;

use std::process::ExitCode;

use clap::{Args, Subcommand};

/// Show (or toggle) the state of Malware DB storing incoming files with zstd compression
#[derive(Clone, Debug, Args, PartialEq)]
pub struct Compression {
    /// Optionally enable or disable compression
    #[clap(subcommand)]
    option: Option<Options>,
}

#[derive(Clone, Debug, Subcommand, PartialEq, Eq)]
enum Options {
    /// Specifies that incoming files should be zstd-compressed on disk. Does not compress
    /// files already in Malware DB.
    Enable,

    /// Clears the compression flag, does not decompress already compressed files.
    Disable,
}

impl Compression {
    pub async fn execute(&self, state: State) -> anyhow::Result<ExitCode> {
        match self.option {
            None => {
                let compression_state = if state.db_config.compression {
                    "enabled"
                } else {
                    "disabled"
                };
                println!("Compression {compression_state}");
            }
            Some(Options::Enable) => {
                state.db_type.enable_compression().await?;
                println!(
                    "Compression flag set. Please restart Malware DB for this change to take effect."
                );
            }
            Some(Options::Disable) => {
                state.db_type.enable_compression().await?;
                println!(
                    "Compression flag unset. Please restart Malware DB for this change to take effect."
                );
            }
        }
        Ok(ExitCode::SUCCESS)
    }
}