Use non-short flags as defaults for TUI

Closes str4d/age-plugin-yubikey#27.
This commit is contained in:
Jack Grigg
2021-04-26 18:11:33 +12:00
parent d9b4fba546
commit acdbb79083
2 changed files with 27 additions and 13 deletions
+2 -2
View File
@@ -15,8 +15,8 @@ use crate::{
BINARY_NAME, USABLE_SLOTS, BINARY_NAME, USABLE_SLOTS,
}; };
const DEFAULT_PIN_POLICY: PinPolicy = PinPolicy::Once; pub(crate) const DEFAULT_PIN_POLICY: PinPolicy = PinPolicy::Once;
const DEFAULT_TOUCH_POLICY: TouchPolicy = TouchPolicy::Always; pub(crate) const DEFAULT_TOUCH_POLICY: TouchPolicy = TouchPolicy::Always;
pub(crate) struct IdentityBuilder { pub(crate) struct IdentityBuilder {
slot: Option<RetiredSlotId>, slot: Option<RetiredSlotId>,
+25 -11
View File
@@ -283,6 +283,8 @@ fn main() -> Result<(), Error> {
} else if opts.list_all { } else if opts.list_all {
list(true) list(true)
} else { } else {
let flags: PluginFlags = opts.try_into()?;
eprintln!("✨ Let's get your YubiKey set up for age! ✨"); eprintln!("✨ Let's get your YubiKey set up for age! ✨");
eprintln!(); eprintln!();
eprintln!("This tool can create a new age identity in a free slot of your YubiKey."); eprintln!("This tool can create a new age identity in a free slot of your YubiKey.");
@@ -408,17 +410,13 @@ fn main() -> Result<(), Error> {
return Ok(()); return Ok(());
} }
} else { } else {
let name = match Input::<String>::new() let name = Input::<String>::new()
.with_prompt(format!( .with_prompt(format!(
"📛 Name this identity [{}]", "📛 Name this identity [{}]",
opts.name.as_deref().unwrap_or("age identity TAG_HEX") flags.name.as_deref().unwrap_or("age identity TAG_HEX")
)) ))
.allow_empty(true) .allow_empty(true)
.interact_text()? .interact_text()?;
{
s if s.is_empty() => opts.name,
s => Some(s),
};
let pin_policy = match Select::new() let pin_policy = match Select::new()
.with_prompt("🔤 Select a PIN policy") .with_prompt("🔤 Select a PIN policy")
@@ -427,7 +425,14 @@ fn main() -> Result<(), Error> {
"Once (A PIN is required once per session, if set)", "Once (A PIN is required once per session, if set)",
"Never (A PIN is NOT required to decrypt)", "Never (A PIN is NOT required to decrypt)",
]) ])
.default(1) .default(
[PinPolicy::Always, PinPolicy::Once, PinPolicy::Never]
.iter()
.position(|p| {
p == &flags.pin_policy.unwrap_or(builder::DEFAULT_PIN_POLICY)
})
.unwrap(),
)
.interact_opt()? .interact_opt()?
{ {
Some(0) => PinPolicy::Always, Some(0) => PinPolicy::Always,
@@ -444,7 +449,13 @@ fn main() -> Result<(), Error> {
"Cached (A physical touch is required for decryption, and is cached for 15 seconds)", "Cached (A physical touch is required for decryption, and is cached for 15 seconds)",
"Never (A physical touch is NOT required to decrypt)", "Never (A physical touch is NOT required to decrypt)",
]) ])
.default(0) .default(
[TouchPolicy::Always, TouchPolicy::Cached, TouchPolicy::Never]
.iter()
.position(|p| p == &flags
.touch_policy.unwrap_or(builder::DEFAULT_TOUCH_POLICY))
.unwrap(),
)
.interact_opt()? .interact_opt()?
{ {
Some(0) => TouchPolicy::Always, Some(0) => TouchPolicy::Always,
@@ -460,10 +471,13 @@ fn main() -> Result<(), Error> {
{ {
eprintln!(); eprintln!();
builder::IdentityBuilder::new(Some(slot)) builder::IdentityBuilder::new(Some(slot))
.with_name(name) .with_name(match name {
s if s.is_empty() => flags.name,
s => Some(s),
})
.with_pin_policy(Some(pin_policy)) .with_pin_policy(Some(pin_policy))
.with_touch_policy(Some(touch_policy)) .with_touch_policy(Some(touch_policy))
.force(opts.force) .force(flags.force)
.build(&mut yubikey)? .build(&mut yubikey)?
} else { } else {
return Ok(()); return Ok(());