Skip to content

Commit ce1d927

Browse files
committed
rust: bear run all implemented
1 parent e67a40d commit ce1d927

File tree

5 files changed

+75
-24
lines changed

5 files changed

+75
-24
lines changed

rust/bear/src/bin/bear.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,13 @@ impl Application {
5252
fn configure(args: args::Arguments, config: config::Main) -> anyhow::Result<Self> {
5353
match args.mode {
5454
args::Mode::Intercept { input, output } => {
55+
log::debug!("Mode: intercept");
5556
let intercept_config = config.intercept;
5657
let mode = Intercept::new(input, output, intercept_config);
5758
Ok(Application::Intercept(mode))
5859
}
5960
args::Mode::Semantic { input, output } => {
61+
log::debug!("Mode: semantic analysis");
6062
let event_source = EventFileReader::try_from(input)?;
6163
let semantic_recognition = Recognition::try_from(&config)?;
6264
let semantic_transform = Transformation::from(&config.output);
@@ -70,9 +72,18 @@ impl Application {
7072
Ok(Application::Semantic(mode))
7173
}
7274
args::Mode::All { input, output } => {
75+
log::debug!("Mode: intercept and semantic analysis");
76+
let semantic_recognition = Recognition::try_from(&config)?;
77+
let semantic_transform = Transformation::from(&config.output);
78+
let output_writer = OutputWriter::configure(&output, &config.output)?;
7379
let intercept_config = config.intercept;
74-
let output_config = config.output;
75-
let mode = All::new(input, output, intercept_config, output_config);
80+
let mode = All::new(
81+
input,
82+
intercept_config,
83+
semantic_recognition,
84+
semantic_transform,
85+
output_writer,
86+
);
7687
Ok(Application::All(mode))
7788
}
7889
}
@@ -84,7 +95,12 @@ impl Application {
8495
Application::Semantic(semantic) => semantic.run(),
8596
Application::All(all) => all.run(),
8697
};
87-
// TODO: log the status
88-
status.unwrap_or(ExitCode::FAILURE)
98+
match status {
99+
Ok(code) => code,
100+
Err(error) => {
101+
log::error!("Run failed: {}", error);
102+
ExitCode::FAILURE
103+
}
104+
}
89105
}
90106
}

rust/bear/src/intercept/collector.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
use std::net::{SocketAddr, TcpListener, TcpStream};
44

5-
use std::sync::mpsc::Sender;
65
use std::sync::atomic::{AtomicBool, Ordering};
6+
use std::sync::mpsc::Sender;
77
use std::sync::Arc;
88

99
use super::Envelope;

rust/bear/src/modes/intercept.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
use crate::intercept::collector::{EventCollector, EventCollectorOnTcp};
44
use crate::intercept::{Envelope, KEY_DESTINATION, KEY_PRELOAD_PATH};
55
use crate::{args, config};
6-
use std::sync::mpsc::Receiver;
7-
use std::sync::mpsc::channel;
86
use std::path::{Path, PathBuf};
97
use std::process::{Command, ExitCode};
8+
use std::sync::mpsc::channel;
9+
use std::sync::mpsc::Receiver;
1010
use std::sync::Arc;
1111
use std::{env, thread};
1212

rust/bear/src/modes/mod.rs

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ pub struct Semantic {
3838

3939
/// The all model is combining the intercept and semantic modes.
4040
pub struct All {
41-
input: args::BuildCommand,
42-
output: args::BuildSemantic,
41+
command: args::BuildCommand,
4342
intercept_config: config::Intercept,
44-
output_config: config::Output,
43+
semantic_recognition: Recognition,
44+
semantic_transform: Transformation,
45+
output_writer: OutputWriter,
4546
}
4647

4748
impl Intercept {
@@ -60,23 +61,22 @@ impl Intercept {
6061
/// Write the envelopes into the output file.
6162
fn write_to_file(
6263
output_file_name: String,
63-
envelopes: impl IntoIterator<Item=Envelope>,
64+
envelopes: impl IntoIterator<Item = Envelope>,
6465
) -> anyhow::Result<()> {
6566
let mut writer = std::fs::File::create(&output_file_name)
6667
.map(BufWriter::new)
6768
.with_context(|| format!("Failed to create output file: {:?}", &output_file_name))?;
6869
for envelope in envelopes {
69-
envelope
70-
.write_into(&mut writer)
71-
.with_context(|| "Failed to write the envelope")?;
70+
envelope.write_into(&mut writer).with_context(|| {
71+
format!("Failed to write execution report: {:?}", &output_file_name)
72+
})?;
7273
}
7374
Ok(())
7475
}
7576
}
7677

7778
impl Mode for Intercept {
7879
fn run(self) -> anyhow::Result<ExitCode> {
79-
// TODO: log failures with the right context
8080
let output_file_name = self.output.file_name.clone();
8181
let service = InterceptService::new(move |envelopes| {
8282
Self::write_to_file(output_file_name, envelopes)
@@ -128,23 +128,58 @@ impl Mode for Semantic {
128128

129129
impl All {
130130
pub fn new(
131-
input: args::BuildCommand,
132-
output: args::BuildSemantic,
131+
command: args::BuildCommand,
133132
intercept_config: config::Intercept,
134-
output_config: config::Output,
133+
semantic_recognition: Recognition,
134+
semantic_transform: Transformation,
135+
output_writer: OutputWriter,
135136
) -> Self {
136137
Self {
137-
input,
138-
output,
138+
command,
139139
intercept_config,
140-
output_config,
140+
semantic_recognition,
141+
semantic_transform,
142+
output_writer,
141143
}
142144
}
145+
146+
fn consume_for_analysis(
147+
semantic_recognition: Recognition,
148+
semantic_transform: Transformation,
149+
output_writer: OutputWriter,
150+
envelopes: impl IntoIterator<Item = Envelope>,
151+
) -> anyhow::Result<()> {
152+
let entries = envelopes
153+
.into_iter()
154+
.map(|envelope| envelope.event.execution)
155+
.flat_map(|execution| semantic_recognition.apply(execution))
156+
.flat_map(|semantic| semantic_transform.apply(semantic));
157+
158+
output_writer.run(entries)
159+
}
143160
}
144161

145162
impl Mode for All {
146163
fn run(self) -> anyhow::Result<ExitCode> {
147-
// TODO: Implement the all mode.
148-
Ok(ExitCode::FAILURE)
164+
let semantic_recognition = self.semantic_recognition;
165+
let semantic_transform = self.semantic_transform;
166+
let output_writer = self.output_writer;
167+
let service = InterceptService::new(move |envelopes| {
168+
Self::consume_for_analysis(
169+
semantic_recognition,
170+
semantic_transform,
171+
output_writer,
172+
envelopes,
173+
)
174+
})
175+
.with_context(|| "Failed to create the intercept service")?;
176+
let environment = InterceptEnvironment::new(&self.intercept_config, service.address())
177+
.with_context(|| "Failed to create the intercept environment")?;
178+
179+
let status = environment
180+
.execute_build_command(self.command)
181+
.with_context(|| "Failed to execute the build command")?;
182+
183+
Ok(status)
149184
}
150185
}

rust/bear/tests/intercept.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ use bear::intercept::*;
66

77
mod test {
88
use super::*;
9-
use std::sync::mpsc::channel;
109
use std::collections::HashMap;
1110
use std::io::Cursor;
1211
use std::path::PathBuf;
12+
use std::sync::mpsc::channel;
1313
use std::sync::Arc;
1414
use std::thread;
1515
use std::time::Duration;

0 commit comments

Comments
 (0)