forked from rtk-ai/rtk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.rs
More file actions
201 lines (178 loc) · 5.4 KB
/
runner.rs
File metadata and controls
201 lines (178 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//! Shared command execution skeleton for filter modules.
use anyhow::{Context, Result};
use std::process::Command;
use crate::core::stream::{self, FilterMode, StdinMode, StreamFilter};
use crate::core::tracking;
pub fn print_with_hint(filtered: &str, raw: &str, tee_label: &str, exit_code: i32) {
if let Some(hint) = crate::core::tee::tee_and_hint(raw, tee_label, exit_code) {
println!("{}\n{}", filtered, hint);
} else {
println!("{}", filtered);
}
}
#[derive(Default)]
pub struct RunOptions<'a> {
pub tee_label: Option<&'a str>,
pub filter_stdout_only: bool,
pub skip_filter_on_failure: bool,
pub no_trailing_newline: bool,
}
impl<'a> RunOptions<'a> {
pub fn with_tee(label: &'a str) -> Self {
Self {
tee_label: Some(label),
..Default::default()
}
}
pub fn stdout_only() -> Self {
Self {
filter_stdout_only: true,
..Default::default()
}
}
pub fn tee(mut self, label: &'a str) -> Self {
self.tee_label = Some(label);
self
}
pub fn early_exit_on_failure(mut self) -> Self {
self.skip_filter_on_failure = true;
self
}
pub fn no_trailing_newline(mut self) -> Self {
self.no_trailing_newline = true;
self
}
}
pub enum RunMode<'a> {
Filtered(Box<dyn Fn(&str) -> String + 'a>),
Streamed(Box<dyn StreamFilter + 'a>),
Passthrough,
}
pub fn run(
mut cmd: Command,
tool_name: &str,
args_display: &str,
mode: RunMode<'_>,
opts: RunOptions<'_>,
) -> Result<i32> {
let timer = tracking::TimedExecution::start();
let cmd_label = format!("{} {}", tool_name, args_display);
match mode {
RunMode::Filtered(filter_fn) => {
let result = stream::run_streaming(&mut cmd, StdinMode::Null, FilterMode::CaptureOnly)
.with_context(|| format!("Failed to run {}", tool_name))?;
let exit_code = result.exit_code;
let raw = &result.raw;
let raw_stdout = &result.raw_stdout;
if opts.skip_filter_on_failure && exit_code != 0 {
if !result.raw_stdout.trim().is_empty() {
print!("{}", result.raw_stdout);
}
if !result.raw_stderr.trim().is_empty() {
eprint!("{}", result.raw_stderr);
}
timer.track(&cmd_label, &format!("rtk {}", cmd_label), raw, raw);
return Ok(exit_code);
}
let text_to_filter = if opts.filter_stdout_only {
raw_stdout
} else {
raw
};
let filtered = filter_fn(text_to_filter);
if let Some(label) = opts.tee_label {
print_with_hint(&filtered, raw, label, exit_code);
} else if opts.no_trailing_newline {
print!("{}", filtered);
} else {
println!("{}", filtered);
}
let raw_for_tracking = if opts.filter_stdout_only {
raw_stdout
} else {
raw
};
timer.track(
&cmd_label,
&format!("rtk {}", cmd_label),
raw_for_tracking,
&filtered,
);
Ok(exit_code)
}
RunMode::Streamed(filter) => {
let result =
stream::run_streaming(&mut cmd, StdinMode::Null, FilterMode::Streaming(filter))
.with_context(|| format!("Failed to run {}", tool_name))?;
if let Some(label) = opts.tee_label {
if let Some(hint) =
crate::core::tee::tee_and_hint(&result.raw, label, result.exit_code)
{
println!("{}", hint);
}
}
timer.track(
&cmd_label,
&format!("rtk {}", cmd_label),
&result.raw,
&result.filtered,
);
Ok(result.exit_code)
}
RunMode::Passthrough => {
let result =
stream::run_streaming(&mut cmd, StdinMode::Inherit, FilterMode::Passthrough)
.with_context(|| format!("Failed to run {}", tool_name))?;
timer.track_passthrough(&cmd_label, &format!("rtk {} (passthrough)", cmd_label));
Ok(result.exit_code)
}
}
}
pub fn run_filtered<F>(
cmd: Command,
tool_name: &str,
args_display: &str,
filter_fn: F,
opts: RunOptions<'_>,
) -> Result<i32>
where
F: Fn(&str) -> String,
{
run(
cmd,
tool_name,
args_display,
RunMode::Filtered(Box::new(filter_fn)),
opts,
)
}
pub fn run_passthrough(tool: &str, args: &[std::ffi::OsString], verbose: u8) -> Result<i32> {
if verbose > 0 {
eprintln!("{} passthrough: {:?}", tool, args);
}
let mut cmd = crate::core::utils::resolved_command(tool);
cmd.args(args);
let args_str = tracking::args_display(args);
run(
cmd,
tool,
&args_str,
RunMode::Passthrough,
RunOptions::default(),
)
}
pub fn run_streamed(
cmd: Command,
tool_name: &str,
args_display: &str,
filter: Box<dyn StreamFilter + '_>,
opts: RunOptions<'_>,
) -> Result<i32> {
run(
cmd,
tool_name,
args_display,
RunMode::Streamed(filter),
opts,
)
}