opgpcard: Extract set_identity command into module

This commit is contained in:
Nora Widdecke 2022-10-26 12:46:03 +02:00
parent d0ad41c9f5
commit 9b7e614772
No known key found for this signature in database
GPG key ID: 2D4111B31DBB99B6
4 changed files with 52 additions and 38 deletions

View file

@ -90,13 +90,7 @@ pub enum Command {
FactoryReset(commands::factory_reset::FactoryResetCommand),
/// Change identity (applies only to Nitrokey Start)
SetIdentity {
#[clap(name = "card ident", short = 'c', long = "card")]
ident: String,
#[clap(name = "identity", value_enum)]
id: SetIdentityId,
},
SetIdentity(commands::set_identity::SetIdentityCommand),
}
#[derive(Parser, Debug)]
@ -307,26 +301,6 @@ impl From<TouchPolicy> for openpgp_card_sequoia::types::TouchPolicy {
}
}
#[derive(ValueEnum, Debug, Clone)]
pub enum SetIdentityId {
#[clap(name = "0")]
Zero,
#[clap(name = "1")]
One,
#[clap(name = "2")]
Two,
}
impl From<SetIdentityId> for u8 {
fn from(id: SetIdentityId) -> Self {
match id {
SetIdentityId::Zero => 0,
SetIdentityId::One => 1,
SetIdentityId::Two => 2,
}
}
}
#[derive(ValueEnum, Debug, Clone)]
#[clap(rename_all = "lower")]
pub enum AdminGenerateAlgo {

View file

@ -6,6 +6,7 @@ pub mod decrypt;
pub mod factory_reset;
pub mod info;
pub mod pubkey;
pub mod set_identity;
pub mod sign;
pub mod ssh;
pub mod status;

View file

@ -0,0 +1,48 @@
// 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::Result;
use clap::{Parser, ValueEnum};
use openpgp_card_sequoia::card::Card;
use crate::util;
#[derive(Parser, Debug)]
pub struct SetIdentityCommand {
#[clap(name = "card ident", short = 'c', long = "card")]
ident: String,
#[clap(name = "identity", value_enum)]
id: SetIdentityId,
}
#[derive(ValueEnum, Debug, Clone)]
pub enum SetIdentityId {
#[clap(name = "0")]
Zero,
#[clap(name = "1")]
One,
#[clap(name = "2")]
Two,
}
impl From<SetIdentityId> for u8 {
fn from(id: SetIdentityId) -> Self {
match id {
SetIdentityId::Zero => 0,
SetIdentityId::One => 1,
SetIdentityId::Two => 2,
}
}
}
pub fn set_identity(command: SetIdentityCommand) -> Result<(), Box<dyn std::error::Error>> {
let backend = util::open_card(&command.ident)?;
let mut card = Card::new(backend);
let mut open = card.transaction()?;
open.set_identity(u8::from(command.id))?;
Ok(())
}

View file

@ -60,8 +60,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
cli::Command::Pubkey(cmd) => {
commands::pubkey::print_pubkey(cli.output_format, cli.output_version, cmd)?;
}
cli::Command::SetIdentity { ident, id } => {
set_identity(&ident, id)?;
cli::Command::SetIdentity(cmd) => {
commands::set_identity::set_identity(cmd)?;
}
cli::Command::Decrypt(cmd) => {
commands::decrypt::decrypt(cmd)?;
@ -548,15 +548,6 @@ fn list_cards(format: OutputFormat, output_version: OutputVersion) -> Result<()>
Ok(())
}
fn set_identity(ident: &str, id: cli::SetIdentityId) -> Result<(), Box<dyn std::error::Error>> {
let backend = util::open_card(ident)?;
let mut card = Card::new(backend);
let mut open = card.transaction()?;
open.set_identity(u8::from(id))?;
Ok(())
}
/// Return a card for a read operation. If `ident` is None, and exactly one card
/// is plugged in, that card is returned. (We don't This
fn pick_card_for_reading(ident: Option<String>) -> Result<Box<dyn CardBackend + Send + Sync>> {