aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRobin Krahl <robin.krahl@ireas.org>2020-09-19 15:04:18 +0200
committerDaniel Mueller <deso@posteo.net>2021-01-10 21:05:44 -0800
commit3152a6d3c1e46243a69fe8ef8066027ca4d38d47 (patch)
tree05e31b4b8ffdf990911ef5392b23ec2aca945e52 /src
parente085dcda752f7a5007e35c2baecbf094d888faa0 (diff)
downloadnitrocli-3152a6d3c1e46243a69fe8ef8066027ca4d38d47.tar.gz
nitrocli-3152a6d3c1e46243a69fe8ef8066027ca4d38d47.tar.bz2
Add the --progress option to the fill command
The fill command starts a background operation on a Nitrokey Storage device that fills the SD card with random data. This patch adds a new option, --progress, to the fill command that checks if a fill operation is already running on the device and shows its progress.
Diffstat (limited to 'src')
-rw-r--r--src/args.rs10
-rw-r--r--src/commands.rs29
-rw-r--r--src/output.rs4
3 files changed, 31 insertions, 12 deletions
diff --git a/src/args.rs b/src/args.rs
index 4947825..8af38ca 100644
--- a/src/args.rs
+++ b/src/args.rs
@@ -80,7 +80,7 @@ Command! {
/// Interacts with the device's encrypted volume
Encrypted(EncryptedArgs) => |ctx, args: EncryptedArgs| args.subcmd.execute(ctx),
/// Fills the SD card with random data
- Fill => crate::commands::fill,
+ Fill(FillArgs) => |ctx, args: FillArgs| crate::commands::fill(ctx, args.attach),
/// Interacts with the device's hidden volume
Hidden(HiddenArgs) => |ctx, args: HiddenArgs| args.subcmd.execute(ctx),
/// Lists the attached Nitrokey devices
@@ -190,6 +190,14 @@ Command! {EncryptedCommand, [
]}
#[derive(Debug, PartialEq, structopt::StructOpt)]
+pub struct FillArgs {
+ /// Checks if a fill operation is already running and show its progress instead of starting a new
+ /// operation.
+ #[structopt(short, long)]
+ attach: bool,
+}
+
+#[derive(Debug, PartialEq, structopt::StructOpt)]
pub struct HiddenArgs {
#[structopt(subcommand)]
subcmd: HiddenCommand,
diff --git a/src/commands.rs b/src/commands.rs
index 7b48bb2..51ac32f 100644
--- a/src/commands.rs
+++ b/src/commands.rs
@@ -471,19 +471,30 @@ pub fn list(ctx: &mut Context<'_>, no_connect: bool) -> anyhow::Result<()> {
}
/// Fill the SD card with random data
-pub fn fill(ctx: &mut Context<'_>) -> anyhow::Result<()> {
+pub fn fill(ctx: &mut Context<'_>, attach: bool) -> anyhow::Result<()> {
with_storage_device(ctx, |ctx, mut device| {
- let pin_entry = pinentry::PinEntry::from(args::PinType::Admin, &device)?;
+ let mut initial_progress = 0;
+ if attach {
+ let status = device
+ .get_operation_status()
+ .context("Failed to query operation status")?;
+ match status {
+ nitrokey::OperationStatus::Ongoing(progress) => initial_progress = progress,
+ nitrokey::OperationStatus::Idle => anyhow::bail!("No fill operation in progress"),
+ }
+ } else {
+ let pin_entry = pinentry::PinEntry::from(args::PinType::Admin, &device)?;
- // Similar to reset, we want the user to re-enter the admin PIN even if is cached to avoid
- // accidental data loss.
- pinentry::clear(&pin_entry).context("Failed to clear cached secret")?;
+ // Similar to reset, we want the user to re-enter the admin PIN
+ // even if is cached to avoid accidental data loss.
+ pinentry::clear(&pin_entry).context("Failed to clear cached secret")?;
- try_with_pin(ctx, &pin_entry, |pin| {
- device.fill_sd_card(&pin).context("Failed to fill SD card")
- })?;
+ try_with_pin(ctx, &pin_entry, |pin| {
+ device.fill_sd_card(&pin).context("Failed to fill SD card")
+ })?;
+ }
- let mut progress_bar = output::ProgressBar::new();
+ let mut progress_bar = output::ProgressBar::new(initial_progress);
progress_bar.draw(ctx)?;
while !progress_bar.is_finished() {
diff --git a/src/output.rs b/src/output.rs
index bd6c03c..1811cc3 100644
--- a/src/output.rs
+++ b/src/output.rs
@@ -26,10 +26,10 @@ pub struct ProgressBar {
impl ProgressBar {
/// Creates a new empty progress bar.
- pub fn new() -> ProgressBar {
+ pub fn new(progress: u8) -> ProgressBar {
ProgressBar {
redraw: true,
- progress: 0,
+ progress,
toggle: false,
finished: false,
}