Skip to content
Snippets Groups Projects
Verified Commit 5a37e60e authored by Jana Dönszelmann's avatar Jana Dönszelmann :sparkling_heart:
Browse files

dry run

parent a98a0d9e
Branches
Tags
No related merge requests found
Pipeline #783147 passed
......@@ -61,9 +61,7 @@ pub fn find_available_serial_port_by_id() -> Result<String> {
.collect();
if ports.is_empty() {
return Err(
eyre!("No serial port to choose from").suggestion("Make sure the usb is plugged in")
);
Err(eyre!("No serial port to choose from").suggestion("Make sure the usb is plugged in"))
} else if ports.len() > 1 {
internal_choose_interactive(ports)
} else {
......
......@@ -49,16 +49,20 @@ fn read_file(file: &Path) -> Result<Vec<u8>> {
/// Exit with an exit code of 1 when the upload fails.
///
/// Returns a path to a serial port over which uploading happened. This path can be used to communicate with the board.
pub fn upload_file_or_stop(port: PortSelector, file: impl AsRef<Path>) -> PathBuf {
pub fn upload_file_or_stop(port: PortSelector, file: Option<impl AsRef<Path>>) -> PathBuf {
if let Some(file) = file {
match read_file(file.as_ref())
.wrap_err_with(|| format!("failed to read from file {:?}", file.as_ref()))
{
Ok(i) => upload_or_stop(port, i),
Ok(i) => upload_or_stop(port, i, false),
Err(e) => {
eprintln!("{e:?}");
exit(1);
}
}
} else {
upload_or_stop(port, [], true)
}
}
/// Upload a file to a connected board. Select which serial port the board is on with the [`PortSelector`]
......@@ -66,11 +70,17 @@ pub fn upload_file_or_stop(port: PortSelector, file: impl AsRef<Path>) -> PathBu
/// Returns an error when the upload fails.
///
/// Returns a path to a serial port over which uploading happened. This path can be used to communicate with the board.
pub fn upload_file(port: PortSelector, file: impl AsRef<Path>) -> Result<PathBuf> {
pub fn upload_file(port: PortSelector, file: Option<impl AsRef<Path>>) -> Result<PathBuf> {
upload(
port,
read_file(file.as_ref())
.wrap_err_with(|| format!("failed to read from file {:?}", file.as_ref()))?,
file.as_ref()
.map(|f| {
read_file(f.as_ref())
.wrap_err_with(|| format!("failed to read from file {:?}", f.as_ref()))
})
.transpose()?
.unwrap_or_default(),
file.is_none(),
)
}
......@@ -80,8 +90,8 @@ pub fn upload_file(port: PortSelector, file: impl AsRef<Path>) -> Result<PathBuf
/// Exit with an exit code of 1 when the upload fails.
///
/// Returns a path to a serial port over which uploading happened. This path can be used to communicate with the board.
pub fn upload_or_stop(port: PortSelector, file: impl AsRef<[u8]>) -> PathBuf {
match upload(port, file.as_ref()) {
pub fn upload_or_stop(port: PortSelector, file: impl AsRef<[u8]>, dry_run: bool) -> PathBuf {
match upload(port, file.as_ref(), dry_run) {
Err(e) => {
eprintln!("{e:?}");
exit(1);
......@@ -96,11 +106,15 @@ pub fn upload_or_stop(port: PortSelector, file: impl AsRef<[u8]>) -> PathBuf {
/// Returns an error when the upload fails.
///
/// Returns a path to a serial port over which uploading happened. This path can be used to communicate with the board.
pub fn upload(port: PortSelector, file: impl AsRef<[u8]>) -> Result<PathBuf> {
upload_internal(port, file.as_ref())
pub fn upload(port: PortSelector, file: impl AsRef<[u8]>, dry_run: bool) -> Result<PathBuf> {
upload_internal(port, file.as_ref(), dry_run)
}
fn upload_internal(port: PortSelector<'_>, file: &[u8], dry_run: bool) -> Result<PathBuf> {
if dry_run && matches!(port, PortSelector::SearchAll) {
bail!("can't use dry_run in SearchAll mode");
}
fn upload_internal(port: PortSelector<'_>, file: &[u8]) -> Result<PathBuf> {
let (ports_to_try, stop_after_first_error): (Vec<Result<Serial>>, bool) = match port {
PortSelector::SearchFirst => (
selector::all_serial_ports()
......@@ -145,6 +159,10 @@ fn upload_internal(port: PortSelector<'_>, file: &[u8]) -> Result<PathBuf> {
}
};
if dry_run {
return Ok(port.path);
}
if let Err(e) = port
.try_do_upload(file)
.wrap_err_with(|| format!("failed to upload to port {:?}", port.path))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment