void-cli 0.0.4

CLI for void — anonymous encrypted source control
//! Generate a random 32-byte encryption key.
//!
//! Outputs a 64-character hex string suitable for use as a void repository key.

use rand::RngCore;
use serde::Serialize;

use crate::output::{run_command, CliError, CliOptions};

#[derive(Debug, Serialize)]
pub struct GenerateKeyOutput {
    pub key: String,
}

pub fn run(opts: &CliOptions) -> Result<(), CliError> {
    run_command("debug generate-key", opts, |ctx| {
        let mut key = [0u8; 32];
        rand::thread_rng().fill_bytes(&mut key);
        let hex_str = hex::encode(key);

        if !ctx.use_json() {
            ctx.info(hex_str.clone());
        }

        Ok(GenerateKeyOutput { key: hex_str })
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_output_serialization() {
        let output = GenerateKeyOutput {
            key: "ab".repeat(32),
        };
        let json = serde_json::to_string(&output).unwrap();
        assert!(json.contains("\"key\""));
    }
}