Skip to content

Commit

Permalink
remove: completions module in favour of stdout
Browse files Browse the repository at this point in the history
add: completions subcommand
  • Loading branch information
alvaro17f committed Feb 29, 2024
1 parent 4496d06 commit e0a0a7e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 145 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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/"
Expand Down
42 changes: 15 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Shell>,
/// List of available commands
#[command(subcommand)]
commands: Option<Commands>,
Expand Down Expand Up @@ -61,21 +52,11 @@ enum Commands {
#[clap(short_flag = 'c', allow_hyphen_values = true)]
#[command(arg_required_else_help = true)]
Custom { args: Vec<String> },
}

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!("<c>{}</c> <y>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<()> {
Expand Down Expand Up @@ -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()?;
}
Expand Down Expand Up @@ -149,7 +138,6 @@ fn main() -> Result<()> {
restic_checker()?;

let cli = Cli::parse();
handle_completions(&cli)?;
handle_commands(&cli)?;

Ok(())
Expand Down
115 changes: 0 additions & 115 deletions src/utils/completions.rs

This file was deleted.

1 change: 1 addition & 0 deletions src/utils/get_user.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#![allow(dead_code)]
use crate::utils::macros::error;
use anyhow::Result;
use color_print::cformat;
Expand Down
1 change: 0 additions & 1 deletion src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod completions;
pub mod get_config;
pub mod get_current_shell;
pub mod get_user;
Expand Down

0 comments on commit e0a0a7e

Please sign in to comment.