secenv 0.0.0

Secure environments.
use {
    anyhow::{
        Context,
        Result,
    },
    std::{
        io::Write,
        process::{
            Command,
            Stdio,
        },
    },
};

pub struct PgpManager;

impl PgpManager {
    pub fn new() -> Result<Self> {
        Ok(Self)
    }

    pub fn decrypt(&self, _key_fingerprint: &str, encrypted_data: &str) -> Result<String> {
        let mut child = Command::new("gpg")
            .args(&["--decrypt", "--quiet"])
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped())
            .spawn()
            .context("Failed to spawn gpg process")?;

        if let Some(stdin) = child.stdin.as_mut() {
            stdin
                .write_all(encrypted_data.as_bytes())
                .context("Failed to write encrypted data to gpg stdin")?;
        }

        let output = child.wait_with_output().context("Failed to read gpg output")?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(anyhow::anyhow!("GPG decryption failed: {}", stderr));
        }

        let decrypted_data = String::from_utf8(output.stdout).context("Decrypted data is not valid UTF-8")?;

        Ok(decrypted_data)
    }
}