Skip to content

Commit 886cb3b

Browse files
committed
cli: simplify main module layout
1 parent f330901 commit 886cb3b

3 files changed

Lines changed: 116 additions & 127 deletions

File tree

crates/cli/src/command.rs

Lines changed: 0 additions & 122 deletions
This file was deleted.

crates/cli/src/main.rs

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,124 @@
1+
use std::io::stderr;
12
use std::process::ExitCode;
23

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+
412
mod service;
513
mod subcmds;
614
mod utils;
715

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+
859
#[tokio::main]
960
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+
}
11124
}

crates/cli/src/subcmds/completion.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use camino::Utf8PathBuf;
55
use clap::{Args, CommandFactory, ValueEnum};
66
use clap_complete::{generate, generate_to, Shell};
77

8-
use crate::command;
9-
108
#[derive(Debug, Args)]
119
#[clap(next_help_heading = "Completion options")]
1210
pub(crate) struct Command {
@@ -21,7 +19,7 @@ pub(crate) struct Command {
2119

2220
impl Command {
2321
pub(super) fn run(&self) -> anyhow::Result<ExitCode> {
24-
let mut cmd = command::Command::command();
22+
let mut cmd = crate::Command::command();
2523
let name = cmd.get_name().to_string();
2624

2725
if let Some(dir) = &self.dir {

0 commit comments

Comments
 (0)