From c230d93726c4f3c6c0b864732443a0c5e85a88cf Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Mon, 26 Apr 2021 22:21:23 +1200 Subject: [PATCH] TUI: Write identity to file Closes str4d/age-plugin-yubikey#23. --- src/main.rs | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index e26493b..c05a624 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,6 @@ use std::convert::{TryFrom, TryInto}; +use std::fs::{File, OpenOptions}; +use std::io::{self, Write}; use age_plugin::run_state_machine; use dialoguer::{Confirm, Input, Select}; @@ -485,7 +487,40 @@ fn main() -> Result<(), Error> { } }; - util::print_identity(stub, recipient, metadata); + eprintln!(); + let file_name = Input::::new() + .with_prompt("📝 File name to write this identity to") + .default(format!( + "age-yubikey-identity-{}.txt", + hex::encode(stub.tag) + )) + .interact_text()?; + + let mut file = match OpenOptions::new() + .create_new(true) + .write(true) + .open(&file_name) + { + Ok(file) => file, + Err(e) if e.kind() == io::ErrorKind::AlreadyExists => { + if Confirm::new() + .with_prompt("File exists. Overwrite it?") + .interact()? + { + File::create(&file_name)? + } else { + return Ok(()); + } + } + Err(e) => return Err(e.into()), + }; + + writeln!(file, "{}", metadata)?; + writeln!(file, "# Recipient: {}", recipient)?; + writeln!(file, "{}", stub.to_string())?; + + eprintln!(); + eprintln!("💭 Remember: everything breaks, have a backup plan for when this YubiKey does."); Ok(()) }