opgpcard: Extract factory_reset command into module

This commit is contained in:
Nora Widdecke 2022-10-26 12:37:59 +02:00
parent 3615087065
commit d0ad41c9f5
No known key found for this signature in database
GPG key ID: 2D4111B31DBB99B6
4 changed files with 29 additions and 15 deletions

View file

@ -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 {

View file

@ -0,0 +1,25 @@
// SPDX-FileCopyrightText: 2021-2022 Heiko Schaefer <heiko@schaefer.name>
// SPDX-FileCopyrightText: 2022 Nora Widdecke <mail@nora.pink>
// 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))
}

View file

@ -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;

View file

@ -147,8 +147,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
}
},
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<String>) -> Result<Box<dyn CardBackend +
}
}
fn factory_reset(ident: &str) -> 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,