Skip to content

Commit

Permalink
Implement non-interactive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
gblach committed Nov 1, 2024
1 parent 492b68a commit 18a6fe6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "imge"
version = "0.3.0"
version = "0.4.0"
edition = "2021"
description = "Write disk images to physical drive or vice versa."
readme = "README.md"
Expand Down
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,3 @@ since choosing the wrong disk may have a big impact on the data on your hard dri
- Verify if data was copied correctly.
- Verify checksum before making copy.
- Support copying /dev/zero and /dev/urandom to the drive.
- Implement non-interactive mode.
4 changes: 4 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ struct Args {
#[argh(switch, short='a')]
all_drives: bool,

/// use this drive, do not ask
#[argh(option, short='d')]
drive: Option<OsString>,

/// copy drive to image (instead of image to drive)
#[argh(switch, short='f')]
from_drive: bool,
Expand Down
103 changes: 57 additions & 46 deletions src/mainloop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct Mainloop {
image_mime_type: Mime,
drives: Vec<imge::Drive>,
selected_row: usize,
selected_name: OsString,
selected_name: Option<OsString>,
selected_size: u64,
modal: Modal,
progress: Option<Arc<Mutex<imge::Progress>>>,
Expand All @@ -63,6 +63,7 @@ impl Mainloop {
ui_accent,
image_basename,
image_mime_type,
selected_name: args.drive,
..Default::default()
}
}
Expand All @@ -72,16 +73,22 @@ impl Mainloop {

self.update_drives(true);

if self.args.drive.is_some() {
self.start_copying();
}

while !self.exit {
if self.error.lock().unwrap().is_some() {
self.modal = Modal::Error;
}

else if let Some(progress) = &self.progress {
if progress.lock().unwrap().finished {
if !progress.lock().unwrap().finished {
self.modal = Modal::Progress;
} else if self.args.drive.is_none() {
self.modal = Modal::Victory;
} else {
self.modal = Modal::Progress;
self.exit = true;
}
}

Expand Down Expand Up @@ -253,10 +260,10 @@ impl Mainloop {
let (src, dest) = match self.args.from_drive {
false => (
&self.image_basename,
&self.selected_name.to_string_lossy().to_string(),
&self.selected_name.clone().unwrap().to_string_lossy().to_string(),
),
true => (
&self.selected_name.to_string_lossy().to_string(),
&self.selected_name.clone().unwrap().to_string_lossy().to_string(),
&self.image_basename,
),
};
Expand Down Expand Up @@ -397,43 +404,7 @@ impl Mainloop {
}

else if self.modal == Modal::Warning && key.code == KeyCode::Enter {
let image_path = imge::Path {
path: self.args.image.clone(),
size: if self.image_mime_type == mime::APPLICATION_OCTET_STREAM {
match std::fs::metadata(&self.args.image) {
Ok(metadata) => Some(metadata.len()),
Err(_) => None,
}
} else {
None
},
};

let drive_path = imge::Path {
path: self.selected_name.clone(),
size: Some(self.selected_size),
};

let (src, dest) = match self.args.from_drive {
false => (image_path, drive_path),
true => (drive_path, image_path),
};

let from_drive = self.args.from_drive;
let image_mime_type = self.image_mime_type.clone();
let progress = Arc::new(Mutex::new(imge::Progress::default()));
let error = self.error.clone();

self.progress = Some(progress.clone());
self.modal = Modal::None;

thread::spawn(move || {
let result = imge::copy(&src, &dest,
from_drive, image_mime_type, &progress);
if let Err(err) = result {
*error.lock().unwrap() = Some(err);
}
});
self.start_copying();
}

else if self.modal == Modal::None {
Expand All @@ -458,7 +429,7 @@ impl Mainloop {
}
},
KeyCode::Enter => {
if !self.selected_name.is_empty() {
if self.selected_name.is_some() {
self.modal = Modal::Warning;
}
},
Expand All @@ -482,7 +453,7 @@ impl Mainloop {
self.selected_row = 0;

for i in 0..self.drives.len() {
if self.selected_name == self.drives[i].name {
if self.selected_name == Some(self.drives[i].name.clone()) {
self.selected_row = i;
break;
}
Expand All @@ -493,13 +464,53 @@ impl Mainloop {
}

if self.drives.is_empty() {
self.selected_name = OsString::from("");
self.selected_name = None;
self.selected_size = 0;
return;
}
}

self.selected_name.clone_from(&self.drives[self.selected_row].name);
self.selected_name.clone_from(&Some(self.drives[self.selected_row].name.clone()));
self.selected_size = self.drives[self.selected_row].size;
}

fn start_copying(&mut self) {
let image_path = imge::Path {
path: self.args.image.clone(),
size: if self.image_mime_type == mime::APPLICATION_OCTET_STREAM {
match std::fs::metadata(&self.args.image) {
Ok(metadata) => Some(metadata.len()),
Err(_) => None,
}
} else {
None
},
};

let drive_path = imge::Path {
path: self.selected_name.clone().unwrap(),
size: Some(self.selected_size),
};

let (src, dest) = match self.args.from_drive {
false => (image_path, drive_path),
true => (drive_path, image_path),
};

let from_drive = self.args.from_drive;
let image_mime_type = self.image_mime_type.clone();
let progress = Arc::new(Mutex::new(imge::Progress::default()));
let error = self.error.clone();

self.progress = Some(progress.clone());
self.modal = Modal::None;

thread::spawn(move || {
let result = imge::copy(&src, &dest,
from_drive, image_mime_type, &progress);
if let Err(err) = result {
*error.lock().unwrap() = Some(err);
}
});
}
}

0 comments on commit 18a6fe6

Please sign in to comment.