From d0ad41c9f5caa6e7d06ed54442284668991712fb Mon Sep 17 00:00:00 2001 From: Nora Widdecke Date: Wed, 26 Oct 2022 12:37:59 +0200 Subject: [PATCH] opgpcard: Extract factory_reset command into module --- tools/src/bin/opgpcard/cli.rs | 5 +--- .../bin/opgpcard/commands/factory_reset.rs | 25 +++++++++++++++++++ tools/src/bin/opgpcard/commands/mod.rs | 1 + tools/src/bin/opgpcard/main.rs | 13 ++-------- 4 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 tools/src/bin/opgpcard/commands/factory_reset.rs diff --git a/tools/src/bin/opgpcard/cli.rs b/tools/src/bin/opgpcard/cli.rs index 714ca23..11ba087 100644 --- a/tools/src/bin/opgpcard/cli.rs +++ b/tools/src/bin/opgpcard/cli.rs @@ -87,10 +87,7 @@ pub enum Command { }, /// Completely reset a card (deletes all data, including the keys on the card!) - FactoryReset { - #[clap(name = "card ident", short = 'c', long = "card")] - ident: String, - }, + FactoryReset(commands::factory_reset::FactoryResetCommand), /// Change identity (applies only to Nitrokey Start) SetIdentity { diff --git a/tools/src/bin/opgpcard/commands/factory_reset.rs b/tools/src/bin/opgpcard/commands/factory_reset.rs new file mode 100644 index 0000000..f472a21 --- /dev/null +++ b/tools/src/bin/opgpcard/commands/factory_reset.rs @@ -0,0 +1,25 @@ +// SPDX-FileCopyrightText: 2021-2022 Heiko Schaefer +// SPDX-FileCopyrightText: 2022 Nora Widdecke +// SPDX-License-Identifier: MIT OR Apache-2.0 + +use anyhow::{anyhow, Result}; +use clap::Parser; + +use openpgp_card_sequoia::card::Card; + +use crate::util; + +#[derive(Parser, Debug)] +pub struct FactoryResetCommand { + #[clap(name = "card ident", short = 'c', long = "card")] + ident: String, +} + +pub fn factory_reset(command: FactoryResetCommand) -> Result<()> { + println!("Resetting Card {}", command.ident); + let card = util::open_card(&command.ident)?; + let mut card = Card::new(card); + + let mut open = card.transaction()?; + open.factory_reset().map_err(|e| anyhow!(e)) +} diff --git a/tools/src/bin/opgpcard/commands/mod.rs b/tools/src/bin/opgpcard/commands/mod.rs index b692df3..45a2364 100644 --- a/tools/src/bin/opgpcard/commands/mod.rs +++ b/tools/src/bin/opgpcard/commands/mod.rs @@ -3,6 +3,7 @@ // SPDX-License-Identifier: MIT OR Apache-2.0 pub mod decrypt; +pub mod factory_reset; pub mod info; pub mod pubkey; pub mod sign; diff --git a/tools/src/bin/opgpcard/main.rs b/tools/src/bin/opgpcard/main.rs index 8e24c7d..0c554cc 100644 --- a/tools/src/bin/opgpcard/main.rs +++ b/tools/src/bin/opgpcard/main.rs @@ -147,8 +147,8 @@ fn main() -> Result<(), Box> { } } }, - cli::Command::FactoryReset { ident } => { - factory_reset(&ident)?; + cli::Command::FactoryReset(cmd) => { + commands::factory_reset::factory_reset(cmd)?; } cli::Command::Admin { ident, @@ -579,15 +579,6 @@ fn pick_card_for_reading(ident: Option) -> Result Result<()> { - println!("Resetting Card {}", ident); - let card = util::open_card(ident)?; - let mut card = Card::new(card); - - let mut open = card.transaction()?; - open.factory_reset().map_err(|e| anyhow!(e)) -} - fn keys_pick_yolo<'a>( key: &'a Cert, policy: &'a dyn Policy,