Skip to content

Commit e7a49bf

Browse files
committed
Extract common functionality into cli module
1 parent 711039c commit e7a49bf

File tree

3 files changed

+73
-139
lines changed

3 files changed

+73
-139
lines changed

cargo-espflash/src/main.rs

Lines changed: 3 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use std::{
22
fs,
3-
io::Write,
43
path::PathBuf,
54
process::{exit, Command, ExitStatus, Stdio},
65
str::FromStr,
@@ -10,10 +9,10 @@ use cargo_metadata::Message;
109
use clap::Parser;
1110
use espflash::{
1211
cli::{
13-
board_info, connect, flash_elf_image, monitor::monitor, save_elf_as_image, ConnectOpts,
14-
FlashConfigOpts, FlashOpts,
12+
board_info, connect, flash_elf_image, monitor::monitor, partition_table, save_elf_as_image,
13+
ConnectOpts, FlashConfigOpts, FlashOpts, PartitionTableOpts,
1514
},
16-
Chip, Config, ImageFormatId, InvalidPartitionTable, PartitionTable,
15+
Chip, Config, ImageFormatId,
1716
};
1817
use miette::{IntoDiagnostic, Result, WrapErr};
1918

@@ -105,24 +104,6 @@ pub struct SaveImageOpts {
105104
pub partition_table: Option<PathBuf>,
106105
}
107106

108-
#[derive(Parser)]
109-
pub struct PartitionTableOpts {
110-
/// Convert CSV parition table to binary representation
111-
#[clap(long, required_unless_present_any = ["info", "to-csv"])]
112-
to_binary: bool,
113-
/// Convert binary partition table to CSV representation
114-
#[clap(long, required_unless_present_any = ["info", "to-binary"])]
115-
to_csv: bool,
116-
/// Show information on partition table
117-
#[clap(short, long, required_unless_present_any = ["to-binary", "to-csv"])]
118-
info: bool,
119-
/// Input partition table
120-
partition_table: PathBuf,
121-
/// Optional output file name, if unset will output to stdout
122-
#[clap(short, long)]
123-
output: Option<PathBuf>,
124-
}
125-
126107
fn main() -> Result<()> {
127108
miette::set_panic_hook();
128109

@@ -366,53 +347,6 @@ fn save_image(
366347
Ok(())
367348
}
368349

369-
fn partition_table(opts: PartitionTableOpts) -> Result<()> {
370-
if opts.to_binary {
371-
let input = fs::read(&opts.partition_table).into_diagnostic()?;
372-
let part_table = PartitionTable::try_from_str(String::from_utf8(input).into_diagnostic()?)
373-
.into_diagnostic()?;
374-
375-
// Use either stdout or a file if provided for the output.
376-
let mut writer: Box<dyn Write> = if let Some(output) = opts.output {
377-
Box::new(fs::File::create(output).into_diagnostic()?)
378-
} else {
379-
Box::new(std::io::stdout())
380-
};
381-
part_table.save_bin(&mut writer).into_diagnostic()?;
382-
} else if opts.to_csv {
383-
let input = fs::read(&opts.partition_table).into_diagnostic()?;
384-
let part_table = PartitionTable::try_from_bytes(input).into_diagnostic()?;
385-
386-
// Use either stdout or a file if provided for the output.
387-
let mut writer: Box<dyn Write> = if let Some(output) = opts.output {
388-
Box::new(fs::File::create(output).into_diagnostic()?)
389-
} else {
390-
Box::new(std::io::stdout())
391-
};
392-
part_table.save_csv(&mut writer).into_diagnostic()?;
393-
} else if opts.info {
394-
let input = fs::read(&opts.partition_table).into_diagnostic()?;
395-
396-
// Try getting the partition table from either the csv or the binary representation and
397-
// fail otherwise.
398-
let part_table = if let Ok(part_table) =
399-
PartitionTable::try_from_bytes(input.clone()).into_diagnostic()
400-
{
401-
part_table
402-
} else if let Ok(part_table) =
403-
PartitionTable::try_from_str(String::from_utf8(input).into_diagnostic()?)
404-
{
405-
part_table
406-
} else {
407-
return Err((InvalidPartitionTable {}).into());
408-
};
409-
410-
part_table.pretty_print();
411-
}
412-
413-
Ok(())
414-
}
415-
416350
#[cfg(unix)]
417351
fn exit_with_process_status(status: ExitStatus) -> ! {
418352
use std::os::unix::process::ExitStatusExt;

espflash/src/cli/mod.rs

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use crate::{
1919
elf::{FirmwareImageBuilder, FlashFrequency, FlashMode},
2020
error::Error,
2121
flasher::FlashSize,
22-
Chip, Flasher, ImageFormatId, PartitionTable,
22+
Chip, Flasher, ImageFormatId, InvalidPartitionTable, PartitionTable,
2323
};
2424

2525
pub mod config;
@@ -65,6 +65,24 @@ pub struct FlashConfigOpts {
6565
pub flash_freq: Option<FlashFrequency>,
6666
}
6767

68+
#[derive(Parser)]
69+
pub struct PartitionTableOpts {
70+
/// Convert CSV parition table to binary representation
71+
#[clap(long, required_unless_present_any = ["info", "to-csv"])]
72+
to_binary: bool,
73+
/// Convert binary partition table to CSV representation
74+
#[clap(long, required_unless_present_any = ["info", "to-binary"])]
75+
to_csv: bool,
76+
/// Show information on partition table
77+
#[clap(short, long, required_unless_present_any = ["to-binary", "to-csv"])]
78+
info: bool,
79+
/// Input partition table
80+
partition_table: PathBuf,
81+
/// Optional output file name, if unset will output to stdout
82+
#[clap(short, long)]
83+
output: Option<PathBuf>,
84+
}
85+
6886
pub fn connect(opts: &ConnectOpts, config: &Config) -> Result<Flasher> {
6987
let port_info = get_serial_port_info(opts, config)?;
7088

@@ -243,3 +261,50 @@ pub fn flash_elf_image(
243261

244262
Ok(())
245263
}
264+
265+
pub fn partition_table(opts: PartitionTableOpts) -> Result<()> {
266+
if opts.to_binary {
267+
let input = fs::read(&opts.partition_table).into_diagnostic()?;
268+
let part_table = PartitionTable::try_from_str(String::from_utf8(input).into_diagnostic()?)
269+
.into_diagnostic()?;
270+
271+
// Use either stdout or a file if provided for the output.
272+
let mut writer: Box<dyn Write> = if let Some(output) = opts.output {
273+
Box::new(fs::File::create(output).into_diagnostic()?)
274+
} else {
275+
Box::new(std::io::stdout())
276+
};
277+
part_table.save_bin(&mut writer).into_diagnostic()?;
278+
} else if opts.to_csv {
279+
let input = fs::read(&opts.partition_table).into_diagnostic()?;
280+
let part_table = PartitionTable::try_from_bytes(input).into_diagnostic()?;
281+
282+
// Use either stdout or a file if provided for the output.
283+
let mut writer: Box<dyn Write> = if let Some(output) = opts.output {
284+
Box::new(fs::File::create(output).into_diagnostic()?)
285+
} else {
286+
Box::new(std::io::stdout())
287+
};
288+
part_table.save_csv(&mut writer).into_diagnostic()?;
289+
} else if opts.info {
290+
let input = fs::read(&opts.partition_table).into_diagnostic()?;
291+
292+
// Try getting the partition table from either the csv or the binary representation and
293+
// fail otherwise.
294+
let part_table = if let Ok(part_table) =
295+
PartitionTable::try_from_bytes(input.clone()).into_diagnostic()
296+
{
297+
part_table
298+
} else if let Ok(part_table) =
299+
PartitionTable::try_from_str(String::from_utf8(input).into_diagnostic()?)
300+
{
301+
part_table
302+
} else {
303+
return Err((InvalidPartitionTable {}).into());
304+
};
305+
306+
part_table.pretty_print();
307+
}
308+
309+
Ok(())
310+
}

espflash/src/main.rs

Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
use std::{fs, io::Write, mem::swap, path::PathBuf, str::FromStr};
1+
use std::{fs, mem::swap, path::PathBuf, str::FromStr};
22

33
use clap::{IntoApp, Parser};
44
use espflash::{
55
cli::{
6-
board_info, connect, flash_elf_image, monitor::monitor, save_elf_as_image, ConnectOpts,
7-
FlashConfigOpts, FlashOpts,
6+
board_info, connect, flash_elf_image, monitor::monitor, partition_table, save_elf_as_image,
7+
ConnectOpts, FlashConfigOpts, FlashOpts, PartitionTableOpts,
88
},
9-
Chip, Config, ImageFormatId, InvalidPartitionTable, PartitionTable,
9+
Chip, Config, ImageFormatId,
1010
};
1111
use miette::{IntoDiagnostic, Result, WrapErr};
1212

@@ -62,24 +62,6 @@ pub struct SaveImageOpts {
6262
pub partition_table: Option<PathBuf>,
6363
}
6464

65-
#[derive(Parser)]
66-
pub struct PartitionTableOpts {
67-
/// Convert CSV parition table to binary representation
68-
#[clap(long, required_unless_present_any = ["info", "to-csv"])]
69-
to_binary: bool,
70-
/// Convert binary partition table to CSV representation
71-
#[clap(long, required_unless_present_any = ["info", "to-binary"])]
72-
to_csv: bool,
73-
/// Show information on partition table
74-
#[clap(short, long, required_unless_present_any = ["to-binary", "to-csv"])]
75-
info: bool,
76-
/// Input partition table
77-
partition_table: PathBuf,
78-
/// Optional output file name, if unset will output to stdout
79-
#[clap(short, long)]
80-
output: Option<PathBuf>,
81-
}
82-
8365
fn main() -> Result<()> {
8466
miette::set_panic_hook();
8567

@@ -191,50 +173,3 @@ fn save_image(opts: SaveImageOpts) -> Result<()> {
191173

192174
Ok(())
193175
}
194-
195-
fn partition_table(opts: PartitionTableOpts) -> Result<()> {
196-
if opts.to_binary {
197-
let input = fs::read(&opts.partition_table).into_diagnostic()?;
198-
let part_table = PartitionTable::try_from_str(String::from_utf8(input).into_diagnostic()?)
199-
.into_diagnostic()?;
200-
201-
// Use either stdout or a file if provided for the output.
202-
let mut writer: Box<dyn Write> = if let Some(output) = opts.output {
203-
Box::new(fs::File::create(output).into_diagnostic()?)
204-
} else {
205-
Box::new(std::io::stdout())
206-
};
207-
part_table.save_bin(&mut writer).into_diagnostic()?;
208-
} else if opts.to_csv {
209-
let input = fs::read(&opts.partition_table).into_diagnostic()?;
210-
let part_table = PartitionTable::try_from_bytes(input).into_diagnostic()?;
211-
212-
// Use either stdout or a file if provided for the output.
213-
let mut writer: Box<dyn Write> = if let Some(output) = opts.output {
214-
Box::new(fs::File::create(output).into_diagnostic()?)
215-
} else {
216-
Box::new(std::io::stdout())
217-
};
218-
part_table.save_csv(&mut writer).into_diagnostic()?;
219-
} else if opts.info {
220-
let input = fs::read(&opts.partition_table).into_diagnostic()?;
221-
222-
// Try getting the partition table from either the csv or the binary representation and
223-
// fail otherwise.
224-
let part_table = if let Ok(part_table) =
225-
PartitionTable::try_from_bytes(input.clone()).into_diagnostic()
226-
{
227-
part_table
228-
} else if let Ok(part_table) =
229-
PartitionTable::try_from_str(String::from_utf8(input).into_diagnostic()?)
230-
{
231-
part_table
232-
} else {
233-
return Err((InvalidPartitionTable {}).into());
234-
};
235-
236-
part_table.pretty_print();
237-
}
238-
239-
Ok(())
240-
}

0 commit comments

Comments
 (0)