Signed-off-by: NotAShelf <raf@notashelf.dev> Change-Id: I6a6a6964dcd3beb752ae2d3a90ab6e545815c692
This commit is contained in:
parent
d86d81f3c1
commit
0174e390f3
4 changed files with 71 additions and 1 deletions
11
Cargo.lock
generated
11
Cargo.lock
generated
|
@ -43,6 +43,15 @@ dependencies = [
|
|||
"clap_lex",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_complete"
|
||||
version = "4.5.55"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a5abde44486daf70c5be8b8f8f1b66c49f86236edf6fa2abadb4d961c4c6229a"
|
||||
dependencies = [
|
||||
"clap",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "clap_derive"
|
||||
version = "4.5.41"
|
||||
|
@ -302,6 +311,8 @@ name = "xtask"
|
|||
version = "0.1.1"
|
||||
dependencies = [
|
||||
"clap",
|
||||
"clap_complete",
|
||||
"eh",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -13,6 +13,7 @@ version = "0.1.1"
|
|||
|
||||
[workspace.dependencies]
|
||||
clap = { default-features = false, features = [ "std", "help", "derive" ], version = "4.5" }
|
||||
clap_complete = "4.5"
|
||||
regex = "1.11"
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = "0.3"
|
||||
|
|
|
@ -10,3 +10,5 @@ publish = false
|
|||
|
||||
[dependencies]
|
||||
clap.workspace = true
|
||||
clap_complete.workspace = true
|
||||
eh = { path = "../eh" }
|
||||
|
|
|
@ -4,7 +4,8 @@ use std::{
|
|||
process,
|
||||
};
|
||||
|
||||
use clap::Parser;
|
||||
use clap::{CommandFactory, Parser};
|
||||
use clap_complete::{Shell, generate};
|
||||
|
||||
#[derive(clap::Parser)]
|
||||
struct Cli {
|
||||
|
@ -24,6 +25,15 @@ enum Command {
|
|||
#[arg(long, default_value = "target/release/eh")]
|
||||
main_binary: PathBuf,
|
||||
},
|
||||
/// Generate shell completion scripts
|
||||
Completions {
|
||||
/// Shell to generate completions for
|
||||
#[arg(value_enum)]
|
||||
shell: Shell,
|
||||
/// Directory to output completion files
|
||||
#[arg(long, default_value = "completions")]
|
||||
output_dir: PathBuf,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
|
@ -56,6 +66,12 @@ fn main() {
|
|||
process::exit(1);
|
||||
}
|
||||
}
|
||||
Command::Completions { shell, output_dir } => {
|
||||
if let Err(error) = generate_completions(shell, &output_dir) {
|
||||
eprintln!("error generating completions: {error}");
|
||||
process::exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,3 +133,43 @@ fn create_multicall_binaries(
|
|||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn generate_completions(shell: Shell, output_dir: &Path) -> Result<(), Box<dyn error::Error>> {
|
||||
println!("generating {} completions...", shell);
|
||||
|
||||
fs::create_dir_all(output_dir)?;
|
||||
|
||||
let mut cmd = eh::Cli::command();
|
||||
let bin_name = "eh";
|
||||
|
||||
let completion_file = output_dir.join(format!("{}.{}", bin_name, shell));
|
||||
let mut file = fs::File::create(&completion_file)?;
|
||||
|
||||
generate(shell, &mut cmd, bin_name, &mut file);
|
||||
|
||||
println!("completion file generated: {}", completion_file.display());
|
||||
|
||||
// Create symlinks for multicall binaries
|
||||
let multicall_names = ["nb", "nr", "ns"];
|
||||
for name in &multicall_names {
|
||||
let symlink_path = output_dir.join(format!("{}.{}", name, shell));
|
||||
if symlink_path.exists() {
|
||||
fs::remove_file(&symlink_path)?;
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
{
|
||||
std::os::unix::fs::symlink(&completion_file, &symlink_path)?;
|
||||
println!("completion symlink created: {}", symlink_path.display());
|
||||
}
|
||||
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
fs::copy(&completion_file, &symlink_path)?;
|
||||
println!("completion copy created: {}", symlink_path.display());
|
||||
}
|
||||
}
|
||||
|
||||
println!("completions generated successfully!");
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue