void-cli 0.0.4

CLI for void — anonymous encrypted source control
//! Create a CID from data.
//!
//! Reads from stdin and creates a CIDv1 using SHA-256.

use std::io::{self, Read};
use std::path::Path;

use serde::Serialize;
use void_core::cid;

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

/// Command-line arguments for create-cid.
#[derive(Debug)]
pub struct CreateCidArgs {
    // No arguments - reads from stdin
}

/// JSON output for the create-cid command.
#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateCidOutput {
    /// The created CID.
    pub cid: String,
    /// Size of the input data.
    pub input_size: usize,
}

/// Run the create-cid command.
///
/// # Arguments
///
/// * `cwd` - Current working directory (unused)
/// * `_args` - Create-cid arguments (unused)
/// * `opts` - CLI options
pub fn run(_cwd: &Path, _args: CreateCidArgs, opts: &CliOptions) -> Result<(), CliError> {
    run_command("create-cid", opts, |ctx| {
        ctx.progress("Reading input...");

        // Read from stdin
        let mut input = Vec::new();
        io::stdin()
            .read_to_end(&mut input)
            .map_err(|e| CliError::io_error(format!("failed to read stdin: {}", e)))?;

        let input_size = input.len();

        // Create CID
        let created_cid = cid::create(&input);
        let cid_str = created_cid.to_string();

        // Human-readable output
        if !ctx.use_json() {
            // Just print the CID to stdout for easy piping
            println!("{}", cid_str);
        }

        Ok(CreateCidOutput {
            cid: cid_str,
            input_size,
        })
    })
}