Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(console): add subcommand for gen completions #336

Merged
merged 14 commits into from
May 23, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tokio-console/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ humantime = "2.1.0"
serde = { version = "1", features = ["derive"] }
toml = "0.5"
dirs = "4"
clap_complete = "3"
2 changes: 2 additions & 0 deletions tokio-console/args.example
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ OPTIONS:
Print version information

SUBCOMMANDS:
gen-completion
Generate shell completions
gen-config
Generate a `console.toml` config file with the default configuration values, overridden
by any provided command-line arguments
Expand Down
30 changes: 29 additions & 1 deletion tokio-console/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::view::Palette;
use clap::{ArgGroup, Parser as Clap, Subcommand, ValueHint};
use clap::{ArgGroup, IntoApp, Parser as Clap, Subcommand, ValueHint};
use clap_complete::Shell;
use color_eyre::eyre::WrapErr;
use serde::{Deserialize, Serialize};
use std::fmt;
Expand Down Expand Up @@ -95,6 +96,18 @@ pub enum OptionalCmd {
/// $ tokio-console gen-config > console.toml
///
GenConfig,

/// Generate shell completions
hawkw marked this conversation as resolved.
Show resolved Hide resolved
///
/// The completion script will be written to stdout.
/// The completion script should be saved in the shell's completion directory.
/// This depends on which shell is in use.
GenCompletion {
#[clap(name = "install", long = "install")]
install: bool,
#[clap(arg_enum)]
shell: Shell,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i wonder if it's worthwhile to also try to read this from the value of the $SHELL environment variable, if it's not provided by the user? we could add that in a follow-up.

},
}

#[derive(Debug, Clone, Copy, Deserialize)]
Expand Down Expand Up @@ -577,6 +590,21 @@ impl ConfigPath {
}
}

/// Generete completion scripts for each specified shell.
pub fn gen_completion(install: bool, shell: Shell) -> color_eyre::Result<()> {
let mut app = Config::command();
let mut buf: Box<dyn std::io::Write> = if install {
color_eyre::eyre::bail!(
"Automatically installing completion scripts is not currently supported on {}",
shell
)
} else {
Box::new(std::io::stdout())
};
clap_complete::generate(shell, &mut app, "tokio-console", &mut buf);
Ok(())
}

#[cfg(test)]
mod tests {
use std::{
Expand Down
16 changes: 11 additions & 5 deletions tokio-console/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ mod warnings;
async fn main() -> color_eyre::Result<()> {
let mut args = config::Config::parse()?;

if args.subcmd == Some(config::OptionalCmd::GenConfig) {
// Generate a default config file and exit.
let toml = args.gen_config_file()?;
println!("{}", toml);
return Ok(());
match args.subcmd {
Some(config::OptionalCmd::GenConfig) => {
// Generate a default config file and exit.
let toml = args.gen_config_file()?;
println!("{}", toml);
return Ok(());
}
Some(config::OptionalCmd::GenCompletion { install, shell }) => {
return config::gen_completion(install, shell);
}
None => {}
}

let retain_for = args.retain_for();
Expand Down