smart-password-manager-cli 0.1.0

Command line interface for smart password management built on smartpasslib. Generate deterministic passwords from secret phrases without storing them.
use std::io::Read;

use anyhow::{Context, Result, bail};

use crate::output;

pub fn resolve_secret(from_arg: Option<String>, from_stdin: bool, prompt: &str) -> Result<String> {
    if let Some(s) = from_arg {
        return Ok(s);
    }

    if from_stdin {
        let mut buf = String::new();
        std::io::stdin()
            .read_to_string(&mut buf)
            .context("Failed to read secret from stdin")?;
        let trimmed = buf.trim_end_matches(['\n', '\r']).to_string();
        if trimmed.is_empty() {
            bail!("Empty secret phrase on stdin");
        }
        return Ok(trimmed);
    }

    if let Ok(env_secret) = std::env::var("SPM_SECRET") {
        if !env_secret.is_empty() {
            return Ok(env_secret);
        }
    }

    let secret = rpassword::prompt_password(prompt).context("Failed to read secret phrase")?;

    if secret.is_empty() {
        bail!("Empty secret phrase");
    }

    output::note("Hint: pass --secret-stdin, SPM_SECRET or --secret to avoid interactive prompt");
    Ok(secret)
}