/
sentry.rs
93 lines (81 loc) · 2.1 KB
/
sentry.rs
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
use super::Error;
use catalyst_toolbox::logs::sentry::{LazySentryLogs, RawLog, SentryLogClient};
use jcli_lib::utils::io::open_file_write;
use std::path::PathBuf;
use std::str::FromStr;
use structopt::StructOpt;
use url::Url;
#[derive(StructOpt)]
#[structopt(rename_all = "kebab-case")]
pub enum SentryLogs {
/// Download logs from sentry
Download(Download),
}
pub enum Mode {
Full,
Latest,
}
#[derive(StructOpt)]
#[structopt(rename_all = "kebab-case")]
pub struct Download {
#[structopt(long)]
url: Url,
#[structopt(long)]
token: String,
#[structopt(long)]
out: PathBuf,
#[structopt(long, default_value = "latest")]
mode: Mode,
}
impl FromStr for Mode {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"f" | "full" => Ok(Self::Full),
"l" | "latest" => Ok(Self::Latest),
_ => Err(format!(
"Could not parse Mode {}. Any of 'f', 'full', 'l' or 'latest' is required",
s
)),
}
}
}
impl SentryLogs {
pub fn exec(self) -> Result<(), Error> {
match self {
SentryLogs::Download(download) => download.exec(),
}
}
}
impl Download {
pub fn exec(self) -> Result<(), Error> {
let Self {
url,
token,
out,
mode,
} = self;
request_sentry_logs_and_dump_to_file(url, token, mode, out)
}
}
fn request_sentry_logs_and_dump_to_file(
url: Url,
token: String,
mode: Mode,
out: PathBuf,
) -> Result<(), Error> {
let client = SentryLogClient::new(url, token);
let logs: Vec<RawLog> = match mode {
Mode::Full => {
let sentry_logs = LazySentryLogs::new(client, 1000);
sentry_logs.into_iter().collect()
}
Mode::Latest => client.get_json_logs()?,
};
dump_logs_to_json(&logs, out)
}
fn dump_logs_to_json(logs: &[RawLog], out: PathBuf) -> Result<(), Error> {
let file = open_file_write(&Some(out))?;
serde_json::to_writer_pretty(file, logs)?;
Ok(())
}