Skip to content

Commit

Permalink
Add mode option to download
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsanchezq committed Jun 8, 2021
1 parent 80d9d7c commit 6ac7ae0
Showing 1 changed file with 45 additions and 5 deletions.
50 changes: 45 additions & 5 deletions src/bin/cli/logs/sentry.rs
@@ -1,9 +1,10 @@
use super::Error;
use catalyst_toolbox::logs::sentry::SentryLogClient;
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;

Expand All @@ -14,6 +15,11 @@ pub enum SentryLogs {
Download(Download),
}

pub enum Mode {
Full,
Latest,
}

#[derive(StructOpt)]
#[structopt(rename_all = "kebab-case")]
pub struct Download {
Expand All @@ -23,6 +29,23 @@ pub struct Download {
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 {
Expand All @@ -35,19 +58,36 @@ impl SentryLogs {

impl Download {
pub fn exec(self) -> Result<(), Error> {
let Self { url, token, out } = self;
request_sentry_logs_and_dump_to_file(url, token, out)
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 = client.get_json_logs()?;
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)?;
serde_json::to_writer_pretty(file, logs)?;
Ok(())
}

0 comments on commit 6ac7ae0

Please sign in to comment.