|
| 1 | +use std::io::stderr; |
1 | 2 | use std::process::ExitCode; |
2 | 3 |
|
3 | | -mod command; |
| 4 | +use bugbite::output::verbose; |
| 5 | +use clap::Parser; |
| 6 | +use clap_verbosity_flag::{log::LevelFilter, Verbosity, WarnLevel}; |
| 7 | +use tracing_log::AsTrace; |
| 8 | + |
| 9 | +use crate::subcmds::Subcommand; |
| 10 | +use crate::utils::wrapped_doc; |
| 11 | + |
4 | 12 | mod service; |
5 | 13 | mod subcmds; |
6 | 14 | mod utils; |
7 | 15 |
|
| 16 | +fn enable_logging(verbosity: LevelFilter) { |
| 17 | + // enable verbose output |
| 18 | + if verbosity >= LevelFilter::Info { |
| 19 | + verbose!(true); |
| 20 | + }; |
| 21 | + |
| 22 | + let format = tracing_subscriber::fmt::format() |
| 23 | + .with_level(true) |
| 24 | + .with_target(true) |
| 25 | + .without_time() |
| 26 | + .compact(); |
| 27 | + |
| 28 | + tracing_subscriber::fmt() |
| 29 | + .event_format(format) |
| 30 | + .with_max_level(verbosity.as_trace()) |
| 31 | + .with_writer(stderr) |
| 32 | + .init(); |
| 33 | +} |
| 34 | + |
| 35 | +#[derive(Parser, Debug)] |
| 36 | +#[command( |
| 37 | + name = env!("CARGO_BIN_NAME"), |
| 38 | + version, |
| 39 | + about = "command line tool for mangling bugs, issues, and tickets", |
| 40 | + disable_help_subcommand = true, |
| 41 | + help_template = wrapped_doc!(" |
| 42 | + {before-help}{name} {version} |
| 43 | +
|
| 44 | + {about} |
| 45 | +
|
| 46 | + {usage-heading} {usage} |
| 47 | +
|
| 48 | + {all-args}{after-help} |
| 49 | + ") |
| 50 | +)] |
| 51 | +pub(crate) struct Command { |
| 52 | + #[clap(flatten)] |
| 53 | + verbosity: Verbosity<WarnLevel>, |
| 54 | + |
| 55 | + #[command(subcommand)] |
| 56 | + subcmd: Subcommand, |
| 57 | +} |
| 58 | + |
8 | 59 | #[tokio::main] |
9 | 60 | async fn main() -> anyhow::Result<ExitCode> { |
10 | | - command::Command::run().await |
| 61 | + let cmd = Command::parse(); |
| 62 | + enable_logging(cmd.verbosity.log_level_filter()); |
| 63 | + |
| 64 | + // TODO: drop this once stable rust supports `unix_sigpipe`, |
| 65 | + // see https://github.com/rust-lang/rust/issues/97889. |
| 66 | + // |
| 67 | + // Reset SIGPIPE to the default behavior since rust ignores it by default. |
| 68 | + unsafe { |
| 69 | + libc::signal(libc::SIGPIPE, libc::SIG_DFL); |
| 70 | + } |
| 71 | + |
| 72 | + cmd.subcmd.run().await |
| 73 | +} |
| 74 | + |
| 75 | +#[cfg(test)] |
| 76 | +mod tests { |
| 77 | + use std::{env, fs}; |
| 78 | + |
| 79 | + use bugbite::test::{build_path, reset_stdin}; |
| 80 | + |
| 81 | + use super::*; |
| 82 | + |
| 83 | + #[tokio::test] |
| 84 | + async fn doc() { |
| 85 | + unsafe { |
| 86 | + // wipe bugbite-related environment variables |
| 87 | + for (key, _value) in env::vars() { |
| 88 | + if key.starts_with("BUGBITE_") { |
| 89 | + env::remove_var(key); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + env::set_var("BUGBITE_CONNECTION", "doc-test"); |
| 94 | + } |
| 95 | + |
| 96 | + let doc_dir = build_path!(env!("CARGO_MANIFEST_DIR"), "doc"); |
| 97 | + for entry in doc_dir.read_dir_utf8().unwrap() { |
| 98 | + let entry = entry.unwrap(); |
| 99 | + let path = entry.path(); |
| 100 | + if path.extension().map(|x| x == "adoc").unwrap_or_default() { |
| 101 | + let name = entry.file_name(); |
| 102 | + let doc = fs::read_to_string(path).unwrap(); |
| 103 | + for (lineno, line) in doc.lines().enumerate().filter(|(_, x)| x.starts_with(' ')) { |
| 104 | + for cmd in line.trim().split(" | ").filter(|x| x.starts_with("bite ")) { |
| 105 | + let args = shlex::split(cmd).unwrap(); |
| 106 | + match Command::try_parse_from(args) { |
| 107 | + Err(e) => { |
| 108 | + panic!( |
| 109 | + "failed parsing: {cmd}\nfile: {name}, line {}\n{e}", |
| 110 | + lineno + 1 |
| 111 | + ); |
| 112 | + } |
| 113 | + Ok(cmd) => { |
| 114 | + // verify Debug is derived for all commands |
| 115 | + let _ = format!("{cmd:?}"); |
| 116 | + } |
| 117 | + } |
| 118 | + reset_stdin(); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + } |
11 | 124 | } |
0 commit comments