-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathrunner.rs
More file actions
206 lines (182 loc) · 6.41 KB
/
Copy pathrunner.rs
File metadata and controls
206 lines (182 loc) · 6.41 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
202
203
204
205
206
use color_eyre::eyre::{Result, WrapErr};
use rust_i18n::t;
use std::borrow::Cow;
use std::fmt::{Debug, Display};
use std::io;
use tracing::debug;
use crate::ctrlc;
use crate::error::{DryRun, MissingSudo, SkipStep};
use crate::execution_context::ExecutionContext;
use crate::step::Step;
use crate::terminal::{print_error, print_warning, should_retry, ShouldRetry};
pub enum StepResult {
Success(Option<UpdatedComponents>),
Failure,
Ignored,
SkippedMissingSudo,
Skipped(String),
}
impl StepResult {
pub fn failed(&self) -> bool {
use StepResult::*;
match self {
Success(_) | Ignored | Skipped(_) | SkippedMissingSudo => false,
Failure => true,
}
}
}
type Report<'a> = Vec<(Cow<'a, str>, StepResult)>;
pub struct UpdatedComponents(Vec<UpdatedComponent>);
impl UpdatedComponents {
pub fn new(updated: Vec<UpdatedComponent>) -> Self {
Self(updated)
}
}
impl Display for UpdatedComponents {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.0.as_slice() {
[] => write!(f, "No updates found"),
components => {
writeln!(f, "Updated:")?;
let updates = components
.iter()
.map(|c| format!("- {c}"))
.collect::<Vec<_>>()
.join("\n");
write!(f, "{updates}")?;
Ok(())
}
}
}
}
pub struct UpdatedComponent {
name: String,
from_version: Option<String>,
to_version: Option<String>,
}
impl UpdatedComponent {
pub fn new(name: String, from_version: Option<String>, to_version: Option<String>) -> Self {
Self {
name,
from_version,
to_version,
}
}
}
impl Display for UpdatedComponent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match (&self.from_version, &self.to_version) {
(None, None) => write!(f, "{}", self.name),
(None, Some(to_version)) => write!(f, "{} to {}", self.name, to_version),
(Some(from_version), None) => write!(f, "{} from {}", self.name, from_version),
(Some(from_version), Some(to_version)) => {
write!(f, "{} from {} to {}", self.name, from_version, to_version)
}
}
}
}
pub struct Runner<'a> {
ctx: &'a ExecutionContext<'a>,
report: Report<'a>,
}
impl<'a> Runner<'a> {
pub fn new(ctx: &'a ExecutionContext) -> Runner<'a> {
Runner {
ctx,
report: Vec::new(),
}
}
fn push_result(&mut self, key: Cow<'a, str>, result: StepResult) {
debug_assert!(!self.report.iter().any(|(k, _)| k == &key), "{key} already reported");
self.report.push((key, result));
}
pub fn execute<K, F>(&mut self, step: Step, key: K, func: F) -> Result<()>
where
K: Into<Cow<'a, str>> + Debug,
F: Fn() -> Result<()>,
{
self._execute(step, key, || func().map(|()| None))
}
pub fn execute_with_updated<K, F>(&mut self, step: Step, key: K, func: F) -> Result<()>
where
K: Into<Cow<'a, str>> + Debug,
F: Fn() -> Result<Vec<UpdatedComponent>>,
{
self._execute(step, key, || func().map(Some))
}
fn _execute<K, F>(&mut self, step: Step, key: K, func: F) -> Result<()>
where
K: Into<Cow<'a, str>> + Debug,
F: Fn() -> Result<Option<Vec<UpdatedComponent>>>,
{
if !self.ctx.config().should_run(step) {
return Ok(());
}
let key: Cow<'a, str> = key.into();
debug!("Step {:?}", key);
// alter the `func` to put it in a span
let func = || {
let span =
tracing::span!(parent: tracing::Span::none(), tracing::Level::TRACE, "step", step = ?step, key = %key);
let _guard = span.enter();
func()
};
loop {
match func() {
Ok(updated) => {
self.push_result(key, StepResult::Success(updated.map(UpdatedComponents::new)));
break;
}
Err(e) if e.downcast_ref::<DryRun>().is_some() => break,
Err(e) if e.downcast_ref::<MissingSudo>().is_some() => {
print_warning(t!("Skipping step, sudo is required"));
self.push_result(key, StepResult::SkippedMissingSudo);
break;
}
Err(e) if e.downcast_ref::<SkipStep>().is_some() => {
if self.ctx.config().verbose() || self.ctx.config().show_skipped() {
self.push_result(key, StepResult::Skipped(e.to_string()));
}
break;
}
Err(e) => {
debug!("Step {:?} failed: {:?}", key, e);
let interrupted = ctrlc::interrupted();
if interrupted {
ctrlc::unset_interrupted();
}
let ignore_failure = self.ctx.config().ignore_failure(step);
let should_ask = interrupted || !(self.ctx.config().no_retry() || ignore_failure);
let should_retry = if should_ask {
print_error(&key, format!("{e:?}"));
should_retry(key.as_ref())?
} else {
ShouldRetry::No
};
match should_retry {
ShouldRetry::No | ShouldRetry::Quit => {
self.push_result(
key,
if ignore_failure {
StepResult::Ignored
} else {
StepResult::Failure
},
);
if let ShouldRetry::Quit = should_retry {
return Err(io::Error::from(io::ErrorKind::Interrupted))
.context("Quit from user input");
}
break;
}
ShouldRetry::Yes => (),
}
}
}
}
Ok(())
}
pub fn report(&self) -> &Report<'_> {
&self.report
}
}