From e0a0a7e16ad94c42e50ce5af4f198c9367a489b8 Mon Sep 17 00:00:00 2001 From: Alvaro <89421445+alvaro17f@users.noreply.github.com> Date: Thu, 29 Feb 2024 13:10:53 +0100 Subject: [PATCH] remove: completions module in favour of stdout add: completions subcommand --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 42 +++++--------- src/utils/completions.rs | 115 --------------------------------------- src/utils/get_user.rs | 1 + src/utils/mod.rs | 1 - 6 files changed, 18 insertions(+), 145 deletions(-) delete mode 100644 src/utils/completions.rs diff --git a/Cargo.lock b/Cargo.lock index 099ccba..f29210f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1220,7 +1220,7 @@ checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" [[package]] name = "wrestic" -version = "1.5.1" +version = "1.6.0" dependencies = [ "anyhow", "clap", diff --git a/Cargo.toml b/Cargo.toml index 89e85ac..f35e1d7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "wrestic" -version = "1.5.1" +version = "1.6.0" authors = ["alvaro17f"] description = "Restic wrapper built in Rust" homepage = "https://wrestic.com/" diff --git a/src/main.rs b/src/main.rs index 997e968..02b39f3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,7 @@ mod modules; mod utils; -use crate::utils::{ - completions::set_completions, set_environment_variables::set_environment_variables, - tools::clear, -}; +use crate::utils::{set_environment_variables::set_environment_variables, tools::clear}; use anyhow::Result; use clap::{CommandFactory, Parser, Subcommand}; use clap_complete::Shell; @@ -15,17 +12,11 @@ use modules::{ initialize::initialize, repair::repair, restore::restore, selector::selector, snapshots::snapshots, update::update, }; -use std::process::exit; -use utils::{ - completions::print_completions, get_config::get_config, restic_checker::restic_checker, -}; +use utils::{get_config::get_config, restic_checker::restic_checker}; #[derive(Parser, Debug, PartialEq)] #[command(author, version, about, long_about = None)] struct Cli { - // If provided, generate completions for given shell - #[arg(long = "generate", value_enum)] - generator: Option, /// List of available commands #[command(subcommand)] commands: Option, @@ -61,21 +52,11 @@ enum Commands { #[clap(short_flag = 'c', allow_hyphen_values = true)] #[command(arg_required_else_help = true)] Custom { args: Vec }, -} - -fn handle_completions(cli: &Cli) -> Result<()> { - if let Some(generator) = cli.generator.as_ref() { - let mut cmd = Cli::command(); - if generator == &Shell::Zsh || generator == &Shell::Bash || generator == &Shell::Fish { - set_completions(*generator, &mut cmd)?; - cprintln!("{} completions are set", generator); - exit(0) - } else { - print_completions(*generator, &mut cmd); - exit(0) - } - } - Ok(()) + /// Generate tab-completion scripts for your shell + Completions { + #[clap(value_enum)] + shell: Shell, + }, } fn handle_commands(cli: &Cli) -> Result<()> { @@ -110,6 +91,14 @@ fn handle_commands(cli: &Cli) -> Result<()> { Some(Commands::Custom { args }) => { custom(args)?; } + Some(Commands::Completions { shell }) => { + clap_complete::generate( + *shell, + &mut Cli::command(), + "wrestic", + &mut std::io::stdout().lock(), + ); + } None => { selector()?; } @@ -149,7 +138,6 @@ fn main() -> Result<()> { restic_checker()?; let cli = Cli::parse(); - handle_completions(&cli)?; handle_commands(&cli)?; Ok(()) diff --git a/src/utils/completions.rs b/src/utils/completions.rs deleted file mode 100644 index b8cde10..0000000 --- a/src/utils/completions.rs +++ /dev/null @@ -1,115 +0,0 @@ -#![allow(clippy::single_char_pattern)] -use crate::utils::get_user::get_user; -use crate::utils::macros::error; -use anyhow::Result; -use clap_complete::{generate, Generator}; -use std::io::ErrorKind; -use std::{ - fs::{self, File}, - io::{self, Write}, - path::Path, - process::{Command, Stdio}, -}; - -fn create_completions_file(file_path: &str, output: &[u8]) -> Result<()> { - let path = Path::new(file_path); - - if !path.exists() { - if let Some(parent) = path.parent() { - match fs::create_dir_all(parent) { - Ok(_) => {} - Err(e) if e.kind() == ErrorKind::PermissionDenied => { - let status = Command::new("sudo") - .arg("mkdir") - .arg("-p") - .arg(parent.to_str().unwrap()) - .status()?; - - if !status.success() { - return Err(error!( - "Failed to create directory. Please run this program with 'sudo'." - )); - } - } - Err(_) => return Err(error!("Failed to create directory.")), - } - } - - match File::create(path) { - Ok(_) => {} - Err(e) if e.kind() == ErrorKind::PermissionDenied => { - let status = Command::new("sudo") - .arg("touch") - .arg(path.to_str().unwrap()) - .status()?; - - if !status.success() { - return Err(error!( - "Failed to create file. Please run this program with 'sudo'." - )); - } - } - Err(_) => return Err(error!("Failed to create file.")), - } - } - - match fs::write(path, output) { - Ok(_) => {} - Err(e) if e.kind() == ErrorKind::PermissionDenied => { - let mut child = Command::new("sudo") - .arg("sh") - .arg("-c") - .arg(format!( - "echo '{}' > {}", - String::from_utf8_lossy(output).replace("'", "'\\''"), - path.to_str().unwrap() - )) - .stdin(Stdio::piped()) - .spawn()?; - - child.stdin.as_mut().unwrap().write_all(output)?; - - let status = child.wait()?; - - if !status.success() { - return Err(error!( - "Failed to write to file. Please run this program with 'sudo'." - )); - } - } - Err(_) => return Err(error!("Failed to write to file.")), - } - - Ok(()) -} - -pub fn print_completions(gen: G, cmd: &mut clap::Command) { - generate(gen, cmd, cmd.get_name().to_string(), &mut io::stdout()); -} - -pub fn set_completions( - gen: G, - cmd: &mut clap::Command, -) -> Result<()> { - let mut output = Vec::new(); - - generate(gen, cmd, cmd.get_name().to_string(), &mut output); - - let shell = format!("{:#?}", gen); - let user = get_user; - - let file_path = if shell.to_lowercase().contains("bash") { - format!( - "/home/{}/.local/share/bash-completion/completions/wrestic", - user()? - ) - } else if shell.to_lowercase().contains("zsh") { - "/usr/local/share/zsh/site-functions/_wrestic".to_string() - } else if shell.to_lowercase().contains("fish") { - format!("/home/{}/.config/fish/completions/wrestic.fish", user()?) - } else { - panic!("{:?}", "Shell not supported") - }; - - create_completions_file(&file_path, &output) -} diff --git a/src/utils/get_user.rs b/src/utils/get_user.rs index 95beca4..5ec48eb 100644 --- a/src/utils/get_user.rs +++ b/src/utils/get_user.rs @@ -1,3 +1,4 @@ +#![allow(dead_code)] use crate::utils::macros::error; use anyhow::Result; use color_print::cformat; diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 926371a..e11cbaa 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,4 +1,3 @@ -pub mod completions; pub mod get_config; pub mod get_current_shell; pub mod get_user;