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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
use std::borrow::Cow;
use std::ffi::{OsStr, OsString};
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
use super::CrateInfo;
use super::Result;
use crate::helper::StringExt;
use crate::report::{Code, ReportEntry};
mod directory_entries;
pub use directory_entries::DirectoryEntries;
#[derive(Debug)]
pub enum CollectionError {
CouldNotRetrieve(String),
}
impl CollectionError {
pub(crate) fn to_entry(&self) -> ReportEntry {
use CollectionError::*;
match self {
CouldNotRetrieve(reason) => ReportEntry::Text(reason.clone()),
}
}
}
pub trait Collector {
fn description(&self) -> &str;
fn collect(&mut self, crate_info: &CrateInfo) -> Result<ReportEntry>;
}
#[derive(Default)]
pub struct SoftwareVersion {
version: Option<String>,
}
impl SoftwareVersion {
pub fn custom<S: AsRef<str>>(version: S) -> Self {
Self {
version: Some(version.as_ref().into()),
}
}
}
impl Collector for SoftwareVersion {
fn description(&self) -> &str {
"Software version"
}
fn collect(&mut self, crate_info: &CrateInfo) -> Result<ReportEntry> {
let git_hash_suffix = match crate_info.git_hash {
Some(git_hash) => format!(" ({})", git_hash),
None => String::new(),
};
Ok(ReportEntry::Text(format!(
"{} {}{}",
crate_info.pkg_name,
self.version.as_deref().unwrap_or(crate_info.pkg_version),
git_hash_suffix,
)))
}
}
#[derive(Default)]
pub struct CompileTimeInformation {}
impl Collector for CompileTimeInformation {
fn description(&self) -> &str {
"Compile time information"
}
fn collect(&mut self, _: &CrateInfo) -> Result<ReportEntry> {
Ok(ReportEntry::List(vec![
ReportEntry::Text(format!("Profile: {}", env!("BUGREPORT_PROFILE"))),
ReportEntry::Text(format!("Target triple: {}", env!("BUGREPORT_TARGET"))),
ReportEntry::Text(format!(
"Family: {}",
env!("BUGREPORT_CARGO_CFG_TARGET_FAMILY")
)),
ReportEntry::Text(format!("OS: {}", env!("BUGREPORT_CARGO_CFG_TARGET_OS"))),
ReportEntry::Text(format!(
"Architecture: {}",
env!("BUGREPORT_CARGO_CFG_TARGET_ARCH")
)),
ReportEntry::Text(format!(
"Pointer width: {}",
env!("BUGREPORT_CARGO_CFG_TARGET_POINTER_WIDTH")
)),
ReportEntry::Text(format!(
"Endian: {}",
env!("BUGREPORT_CARGO_CFG_TARGET_ENDIAN")
)),
ReportEntry::Text(format!(
"CPU features: {}",
env!("BUGREPORT_CARGO_CFG_TARGET_FEATURE")
)),
ReportEntry::Text(format!("Host: {}", env!("BUGREPORT_HOST"))),
]))
}
}
#[derive(Default)]
pub struct CommandLine {}
impl Collector for CommandLine {
fn description(&self) -> &str {
"Command-line"
}
fn collect(&mut self, _: &CrateInfo) -> Result<ReportEntry> {
let mut result = String::new();
for arg in std::env::args_os() {
result += &shell_escape::escape(arg.to_string_lossy());
result += " ";
}
Ok(ReportEntry::Code(Code {
language: Some("bash".into()),
code: result,
}))
}
}
#[cfg(feature = "collector_operating_system")]
#[derive(Default)]
pub struct OperatingSystem {}
#[cfg(feature = "collector_operating_system")]
impl Collector for OperatingSystem {
fn description(&self) -> &str {
"Operating system"
}
fn collect(&mut self, _: &CrateInfo) -> Result<ReportEntry> {
use std::ops::Deref;
use sys_info::{os_release, os_type};
let os_type = os_type()
.map_err(|_| CollectionError::CouldNotRetrieve("Operating system type".into()))?;
let os_release = os_release();
let os_release = os_release
.as_ref()
.map(|t| t.deref())
.unwrap_or("(unknown version)");
#[cfg(target_os = "macos")]
return Ok(ReportEntry::Text(format!(
"{} ({} {})",
macos_info_string(),
os_type,
os_release
)));
#[cfg(not(target_os = "macos"))]
Ok(ReportEntry::Text(format!("{} {}", os_type, os_release)))
}
}
#[cfg(all(feature = "collector_operating_system", target_os = "macos"))]
fn macos_info() -> Result<(String, String)> {
fn sw_vers(arg: &str) -> Result<String> {
let stdout = Command::new("sw_vers")
.arg(arg)
.output()
.map_err(|err| CollectionError::CouldNotRetrieve(err.to_string()))?
.stdout;
Ok(String::from_utf8_lossy(&stdout).trim().to_owned())
}
let macos_name = sw_vers("-productName")?;
let macos_version = sw_vers("-productVersion")?;
Ok((macos_name, macos_version))
}
#[cfg(all(feature = "collector_operating_system", target_os = "macos"))]
fn macos_info_string() -> String {
if let Ok((name, version)) = macos_info() {
format!("{} {}", name, version)
} else {
"Unknown".to_owned()
}
}
pub struct EnvironmentVariables {
list: Vec<OsString>,
}
impl EnvironmentVariables {
pub fn list<S: AsRef<OsStr>>(list: &[S]) -> Self {
Self {
list: list.iter().map(|s| s.as_ref().to_os_string()).collect(),
}
}
}
impl Collector for EnvironmentVariables {
fn description(&self) -> &str {
"Environment variables"
}
fn collect(&mut self, _: &CrateInfo) -> Result<ReportEntry> {
let mut result = String::new();
for var in &self.list {
let value = std::env::var_os(&var).map(|value| value.to_string_lossy().into_owned());
let value: Option<String> =
value.map(|v| shell_escape::escape(Cow::Borrowed(&v)).into());
result += &format!(
"{}={}\n",
var.to_string_lossy(),
value.unwrap_or_else(|| "<not set>".into())
);
}
result.pop();
Ok(ReportEntry::Code(Code {
language: Some("bash".into()),
code: result,
}))
}
}
pub struct CommandOutput<'a> {
title: &'a str,
cmd: OsString,
cmd_args: Vec<OsString>,
}
impl<'a> CommandOutput<'a> {
pub fn new<S, T>(title: &'a str, cmd: T, args: &[S]) -> Self
where
T: AsRef<OsStr>,
S: AsRef<OsStr>,
{
let mut cmd_args: Vec<OsString> = Vec::new();
for a in args {
cmd_args.push(a.into());
}
CommandOutput {
title,
cmd: cmd.as_ref().to_owned(),
cmd_args,
}
}
}
impl<'a> Collector for CommandOutput<'a> {
fn description(&self) -> &str {
self.title
}
fn collect(&mut self, _: &CrateInfo) -> Result<ReportEntry> {
let mut result = String::new();
result += "> ";
result += &self.cmd.to_string_lossy();
result += " ";
for arg in &self.cmd_args {
result += &shell_escape::escape(arg.to_string_lossy());
result += " ";
}
result += "\n";
let output = Command::new(&self.cmd)
.args(&self.cmd_args)
.output()
.map_err(|e| {
CollectionError::CouldNotRetrieve(format!(
"Could not run command '{}': {}",
self.cmd.to_string_lossy(),
e
))
})?;
let utf8_decoding_error = |_| {
CollectionError::CouldNotRetrieve(format!(
"Error while running command '{}': output is not valid UTF-8.",
self.cmd.to_string_lossy()
))
};
let stdout = String::from_utf8(output.stdout).map_err(utf8_decoding_error)?;
let stderr = String::from_utf8(output.stderr).map_err(utf8_decoding_error)?;
result += &stdout;
result += &stderr;
result.trim_end_inplace();
let mut concat = vec![ReportEntry::Code(Code {
language: None,
code: result,
})];
if !output.status.success() {
concat.push(ReportEntry::Text(format!(
"Command failed{}.",
output
.status
.code()
.map_or("".into(), |c| format!(" with exit code {}", c))
)));
}
Ok(ReportEntry::Concat(concat))
}
}
pub struct FileContent<'a> {
title: &'a str,
path: PathBuf,
}
impl<'a> FileContent<'a> {
pub fn new<P: AsRef<Path>>(title: &'a str, path: P) -> Self {
Self {
title,
path: path.as_ref().to_path_buf(),
}
}
}
impl<'a> Collector for FileContent<'a> {
fn description(&self) -> &str {
self.title
}
fn collect(&mut self, _: &CrateInfo) -> Result<ReportEntry> {
let mut result = fs::read_to_string(&self.path).map_err(|e| {
CollectionError::CouldNotRetrieve(format!(
"Could not read contents of '{}': {}.",
self.path.to_string_lossy(),
e
))
})?;
result.trim_end_inplace();
Ok(ReportEntry::Code(Code {
language: None,
code: result,
}))
}
}