Skip to content

Commit

Permalink
feat: add rage command for bug reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
suo committed May 10, 2022
1 parent 0a0f0ec commit bb80fef
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 32 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ directories = "4.0.1"
blake3 = "1.3.1"
fern = { version = "0.6.1", features = ["colored"] }
chrono = "0.4.19"
dialoguer = "0.10.1"

[dev-dependencies]
assert_cmd = "2.0.4"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub mod linter;
pub mod log_utils;
pub mod path;
pub mod persistent_data;
pub mod rage;
pub mod render;

use git::get_changed_files;
Expand Down
22 changes: 21 additions & 1 deletion src/log_utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use anyhow::{bail, Result};
use console::{style, Term};
use fern::colors::{Color, ColoredLevelConfig};
use std::path::Path;
use std::process::Output;

use log::Level::Trace;
Expand Down Expand Up @@ -32,7 +33,7 @@ pub fn ensure_output(program_name: &str, output: &Output) -> Result<()> {
Ok(())
}

pub fn setup_logger(log_level: LevelFilter, force_color: bool) -> Result<()> {
pub fn setup_logger(log_level: LevelFilter, log_file: &Path, force_color: bool) -> Result<()> {
let builder = fern::Dispatch::new();

let isatty = Term::stderr().features().is_attended();
Expand Down Expand Up @@ -61,6 +62,20 @@ pub fn setup_logger(log_level: LevelFilter, force_color: bool) -> Result<()> {
.level(log_level)
.chain(std::io::stderr()),
)
.chain(
fern::Dispatch::new()
.format(move |out, message, record| {
out.finish(format_args!(
"[{} {} {}] {}",
chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true),
record.level(),
record.target(),
message
))
})
.level(LevelFilter::Trace)
.chain(fern::log_file(log_file)?),
)
.apply()?;
} else {
builder
Expand All @@ -78,6 +93,11 @@ pub fn setup_logger(log_level: LevelFilter, force_color: bool) -> Result<()> {
.level(log_level)
.chain(std::io::stderr()),
)
.chain(
fern::Dispatch::new()
.level(LevelFilter::Trace)
.chain(fern::log_file(log_file)?),
)
.apply()?;
}
Ok(())
Expand Down
44 changes: 39 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{collections::HashSet, convert::TryFrom, io::Write};

use anyhow::{Context, Result};
use chrono::SecondsFormat;
use clap::Parser;

use lintrunner::{
Expand All @@ -9,7 +10,8 @@ use lintrunner::{
lint_config::{get_linters_from_config, LintRunnerConfig},
log_utils::setup_logger,
path::AbsPath,
persistent_data::{PersistentDataStore},
persistent_data::{ExitInfo, PersistentDataStore, RunInfo},
rage::do_rage,
render::print_error,
PathsOpt, RenderOpt, RevisionOpt,
};
Expand Down Expand Up @@ -102,6 +104,13 @@ enum SubCommand {

/// Run linters. This is the default if no subcommand is provided.
Lint,

/// Create a bug report for a past invocation of lintrunner.
Rage {
/// Choose a specific invocation to report on. 0 is the most recent run.
#[clap(long, short)]
invocation: Option<usize>,
},
}

fn do_main() -> Result<i32> {
Expand All @@ -128,9 +137,17 @@ fn do_main() -> Result<i32> {
(_, _) => log::LevelFilter::Trace,
};

let persistent_data_store = PersistentDataStore::new(&config_path)?;
let run_info = RunInfo {
args: std::env::args().collect(),
timestamp: chrono::Local::now().to_rfc3339_opts(SecondsFormat::Secs, true),
};
let persistent_data_store = PersistentDataStore::new(&config_path, run_info)?;

setup_logger(log_level, args.force_color)?;
setup_logger(
log_level,
&persistent_data_store.log_file(),
args.force_color,
)?;

let cmd = args.cmd.unwrap_or(SubCommand::Lint);
let lint_runner_config = LintRunnerConfig::new(&config_path)?;
Expand Down Expand Up @@ -194,7 +211,7 @@ fn do_main() -> Result<i32> {
PathsOpt::Auto
};

match cmd {
let res = match cmd {
SubCommand::Init { dry_run } => {
// Just run initialization commands, don't actually lint.
do_init(linters, dry_run, &persistent_data_store, &config_path)
Expand Down Expand Up @@ -222,7 +239,24 @@ fn do_main() -> Result<i32> {
revision_opt,
)
}
}
SubCommand::Rage { invocation } => do_rage(&persistent_data_store, invocation),
};

let exit_info = match &res {
Ok(code) => ExitInfo {
code: *code,
err: None,
},
Err(err) => ExitInfo {
code: 1,
err: Some(err.to_string()),
},
};

// Write data related to this run out to the persistent data store.
persistent_data_store.write_run_info(exit_info)?;

res
}

fn main() {
Expand Down
Loading

0 comments on commit bb80fef

Please sign in to comment.