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
|
@ -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