-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add external processor support #705
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -30,7 +30,7 @@ pub struct Cli { | |||||
subcommand: Commands, | ||||||
|
||||||
/// Do not check for updates | ||||||
#[clap(short, long, global = true, action)] | ||||||
#[clap(long, global = true, action)] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess it clashed with
Suggested change
|
||||||
skip_update_check: bool, | ||||||
} | ||||||
|
||||||
|
@@ -303,6 +303,8 @@ fn flash(args: FlashArgs, config: &Config) -> Result<()> { | |||||
args.flash_args.monitor_baud.unwrap_or(default_baud), | ||||||
args.flash_args.log_format, | ||||||
true, | ||||||
args.flash_args.processors, | ||||||
Some(args.image), | ||||||
) | ||||||
} else { | ||||||
Ok(()) | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
//! External processor support | ||
//! | ||
//! Via the command line argument `--processors` you can instruct espflash to run external executables to pre-process | ||
//! the logs received from the target. Multiple processors are supported by separating them via `,`. Processors are executed in the specified order. | ||
//! | ||
//! You can use full-qualified paths or run an executable which is already in the search path. | ||
//! | ||
//! A processors reads from stdin and output to stdout. Be aware this runs before further processing by espflash. | ||
//! i.e. addresses are not resolved and when using `defmt` you will see encoded data. | ||
//! | ||
//! Additionally be aware that you might receive chunked data which is not always split at valid UTF character boundaries. | ||
//! | ||
//! The executable will get the path of the ELF file as the first argument if available. | ||
//! | ||
//! Example processor which turns some letters into uppercase | ||
//! ```rust,no-run | ||
//! use std::io::{stdin, stdout, Read, Write}; | ||
//! | ||
//! fn main() { | ||
//! let args: Vec<String> = std::env::args().collect(); | ||
//! println!("ELF file: {:?}", args[1]); | ||
//! | ||
//! let mut buf = [0u8; 1024]; | ||
//! loop { | ||
//! if let Ok(len) = stdin().read(&mut buf) { | ||
//! for b in &mut buf[..len] { | ||
//! *b = if b"abdfeo".contains(b) { | ||
//! b.to_ascii_uppercase() | ||
//! } else { | ||
//! *b | ||
//! }; | ||
//! } | ||
//! | ||
//! stdout().write(&buf[..len]).unwrap(); | ||
//! stdout().flush().unwrap(); | ||
//! } else { | ||
//! // ignored | ||
//! } | ||
//! } | ||
//! } | ||
//! ``` | ||
|
||
use std::{ | ||
fmt::Display, | ||
io::{Read, Write}, | ||
path::PathBuf, | ||
process::{Child, ChildStdin, Stdio}, | ||
sync::mpsc, | ||
}; | ||
|
||
use miette::Diagnostic; | ||
|
||
#[derive(Debug)] | ||
pub struct Error { | ||
executable: String, | ||
} | ||
|
||
impl Display for Error { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "Failed to launch '{}'", self.executable) | ||
} | ||
} | ||
|
||
impl std::error::Error for Error {} | ||
|
||
impl Diagnostic for Error {} | ||
|
||
struct Processor { | ||
rx: mpsc::Receiver<u8>, | ||
stdin: ChildStdin, | ||
child: Child, | ||
} | ||
|
||
impl Processor { | ||
pub fn new(child: Child) -> Self { | ||
let mut child = child; | ||
let (tx, rx) = mpsc::channel::<u8>(); | ||
|
||
let mut stdout = child.stdout.take().unwrap(); | ||
let stdin = child.stdin.take().unwrap(); | ||
|
||
std::thread::spawn(move || { | ||
let mut buffer = [0u8; 1024]; | ||
loop { | ||
if let Ok(len) = stdout.read(&mut buffer) { | ||
for b in &buffer[..len] { | ||
if tx.send(*b).is_err() { | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
Self { rx, stdin, child } | ||
} | ||
|
||
pub fn try_receive(&mut self) -> Vec<u8> { | ||
let mut res = Vec::new(); | ||
while let Ok(b) = self.rx.try_recv() { | ||
res.push(b); | ||
} | ||
res | ||
} | ||
|
||
pub fn send(&mut self, data: Vec<u8>) { | ||
self.stdin.write(&data).ok(); | ||
} | ||
} | ||
|
||
impl Drop for Processor { | ||
fn drop(&mut self) { | ||
self.child.kill().unwrap(); | ||
} | ||
} | ||
|
||
pub struct ExternalProcessors { | ||
processors: Vec<Processor>, | ||
} | ||
|
||
impl ExternalProcessors { | ||
pub fn new(processors: Option<String>, elf: Option<PathBuf>) -> Result<Self, Error> { | ||
let mut args = Vec::new(); | ||
|
||
if let Some(elf) = elf { | ||
args.push(elf.as_os_str().to_str().unwrap().to_string()); | ||
}; | ||
|
||
let mut spawned = Vec::new(); | ||
if let Some(processors) = processors { | ||
for processor in processors.split(",").into_iter() { | ||
let processor = std::process::Command::new(processor) | ||
.args(args.clone()) | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::inherit()) | ||
.spawn() | ||
.map_err(|_| Error { | ||
executable: processor.to_string(), | ||
})?; | ||
spawned.push(Processor::new(processor)); | ||
} | ||
} | ||
|
||
Ok(Self { | ||
processors: spawned, | ||
}) | ||
} | ||
|
||
pub fn process(&mut self, read: &[u8]) -> Vec<u8> { | ||
let mut buffer = Vec::new(); | ||
buffer.extend_from_slice(read); | ||
|
||
for processor in &mut self.processors { | ||
processor.send(buffer); | ||
buffer = processor.try_receive(); | ||
} | ||
|
||
buffer | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
short
clashed with some other option's short-name