-
-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathbash_hook.rs
More file actions
279 lines (250 loc) · 8.61 KB
/
bash_hook.rs
File metadata and controls
279 lines (250 loc) · 8.61 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#![expect(clippy::unwrap_used, reason = "deprecated command")]
use std::cmp::min;
use std::collections::HashMap;
use std::env;
use std::fs;
use std::io::{BufRead as _, BufReader};
use std::path::Path;
use anyhow::{format_err, Result};
use clap::{builder::ArgPredicate, Arg, ArgAction, ArgMatches, Command};
use lazy_static::lazy_static;
use regex::Regex;
use sentry::protocol::{Event, Exception, Frame, Stacktrace, User, Value};
use uuid::Uuid;
use crate::commands::send_event;
use crate::config::Config;
use crate::utils::event::{attach_logfile, get_sdk_info};
use crate::utils::releases::detect_release_name;
const BASH_SCRIPT: &str = include_str!("../bashsupport.sh");
lazy_static! {
static ref FRAME_RE: Regex = Regex::new(r"^(.*?):(.*):(\d+)$").unwrap();
}
pub fn make_command(command: Command) -> Command {
command
.about("Prints out a bash script that does error handling.")
// Legacy command, left hidden for backward compatibility
.hide(true)
.arg(
Arg::new("no_exit")
.long("no-exit")
.action(ArgAction::SetTrue)
.help("Do not turn on -e (exit immediately) flag automatically"),
)
.arg(
Arg::new("no_environ")
.long("no-environ")
.action(ArgAction::SetTrue)
.help("Do not send environment variables along"),
)
.arg(
Arg::new("cli")
.long("cli")
.value_name("CMD")
.help("Explicitly set/override the sentry-cli command"),
)
.arg(
Arg::new("send_event")
.long("send-event")
.action(ArgAction::SetTrue)
.requires_ifs([
(ArgPredicate::IsPresent, "traceback"),
(ArgPredicate::IsPresent, "log"),
])
.hide(true),
)
.arg(
Arg::new("traceback")
.long("traceback")
.value_name("PATH")
.hide(true),
)
.arg(
Arg::new("tags")
.value_name("KEY:VALUE")
.long("tag")
.action(ArgAction::Append)
.help("Add tags (key:value) to the event."),
)
.arg(
Arg::new("release")
.value_name("RELEASE")
.long("release")
.action(ArgAction::Set)
.help("Define release version for the event."),
)
.arg(Arg::new("log").long("log").value_name("PATH").hide(true))
}
fn send_event(
traceback: &str,
logfile: &str,
tags: &[&String],
release: Option<String>,
environ: bool,
) -> Result<()> {
let config = Config::current();
let mut event = Event {
environment: config.get_environment().map(Into::into),
release: release.or(detect_release_name().ok()).map(Into::into),
sdk: Some(get_sdk_info()),
user: whoami::fallible::username().ok().map(|n| User {
username: Some(n),
ip_address: Some(Default::default()),
..Default::default()
}),
..Event::default()
};
for tag in tags {
let mut split = tag.splitn(2, ':');
let key = split.next().ok_or_else(|| format_err!("missing tag key"))?;
let value = split
.next()
.ok_or_else(|| format_err!("missing tag value"))?;
event.tags.insert(key.into(), value.into());
}
if environ {
event.extra.insert(
"environ".into(),
Value::Object(env::vars().map(|(k, v)| (k, Value::String(v))).collect()),
);
}
let mut cmd = "unknown".to_owned();
let mut exit_code = 1;
let mut frames = vec![];
if let Ok(f) = fs::File::open(traceback) {
let f = BufReader::new(f);
for line in f.lines() {
let line = line?;
// meta info
if line.starts_with('@') {
if let Some(rest) = line.strip_prefix("@command:") {
cmd = rest.to_owned();
} else if let Some(rest) = line.strip_prefix("@exit_code:") {
exit_code = rest.parse().unwrap_or(exit_code);
} else {
continue;
}
}
if let Some(cap) = FRAME_RE.captures(&line) {
match &cap[1] {
"_sentry_err_trap" | "_sentry_exit_trap" | "_sentry_traceback" => continue,
_ => {}
}
frames.push(Frame {
filename: Some(cap[2].to_string()),
abs_path: Path::new(&cap[2])
.canonicalize()
.map(|x| x.display().to_string())
.ok(),
lineno: cap[3].parse().ok(),
function: Some(cap[1].to_string()),
..Default::default()
});
}
}
}
{
let mut source_caches = HashMap::new();
for frame in &mut frames {
let lineno = match frame.lineno {
Some(line) => line as usize,
None => continue,
};
let filename = frame.filename.as_deref().expect("frame without location");
if !source_caches.contains_key(filename) {
if let Ok(f) = fs::File::open(filename) {
let lines: Vec<_> = BufReader::new(f)
.lines()
.map(|x| x.unwrap_or_else(|_| "".to_owned()))
.collect();
source_caches.insert(filename, lines);
} else {
source_caches.insert(filename, vec![]);
}
}
let source = &source_caches[filename];
frame.context_line = source.get(lineno.saturating_sub(1)).cloned();
if let Some(slice) = source.get(lineno.saturating_sub(5)..lineno.saturating_sub(1)) {
frame.pre_context = slice.to_vec();
};
if let Some(slice) = source.get(lineno..min(lineno + 5, source.len())) {
frame.post_context = slice.to_vec();
};
}
}
attach_logfile(&mut event, logfile, true)?;
event.exception.values.push(Exception {
ty: "BashError".into(),
value: Some(format!("command {cmd} exited with status {exit_code}")),
stacktrace: Some(Stacktrace {
frames,
..Default::default()
}),
..Default::default()
});
let id = send_event::send_raw_event(event)?;
println!("{id}");
Ok(())
}
pub fn execute(matches: &ArgMatches) -> Result<()> {
let release = Config::current().get_release(matches).ok();
let tags: Vec<_> = matches
.get_many::<String>("tags")
.map(|v| v.collect())
.unwrap_or_default();
if matches.get_flag("send_event") {
return send_event(
matches.get_one::<String>("traceback").unwrap(),
matches.get_one::<String>("log").unwrap(),
&tags,
release,
!matches.get_flag("no_environ"),
);
}
let path = env::temp_dir();
let log = path.join(format!(".sentry-{}.out", Uuid::new_v4().as_hyphenated()));
let traceback = path.join(format!(
".sentry-{}.traceback",
Uuid::new_v4().as_hyphenated()
));
let mut script = BASH_SCRIPT
.replace(
"___SENTRY_TRACEBACK_FILE___",
&traceback.display().to_string(),
)
.replace("___SENTRY_LOG_FILE___", &log.display().to_string());
script = script.replace(
" ___SENTRY_TAGS___",
&tags
.iter()
.map(|tag| format!(" --tag \"{tag}\""))
.collect::<Vec<_>>()
.join(""),
);
script = match release {
Some(release) => script.replace(
" ___SENTRY_RELEASE___",
format!(" --release \"{release}\"").as_str(),
),
None => script.replace(" ___SENTRY_RELEASE___", ""),
};
script = script.replace(
"___SENTRY_CLI___",
matches
.get_one::<String>("cli")
.map_or_else(
|| env::current_exe().unwrap().display().to_string(),
String::clone,
)
.as_str(),
);
if matches.get_flag("no_environ") {
script = script.replace("___SENTRY_NO_ENVIRON___", "--no-environ");
} else {
script = script.replace("___SENTRY_NO_ENVIRON___", "");
}
if !matches.get_flag("no_exit") {
script.insert_str(0, "set -e\n\n");
}
println!("{script}");
Ok(())
}