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

⚗️ Add --generate option by clap_complete #373

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 10 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ ansi_term = "0.12.1"
bytesize = "1.3.0"
chrono = "0.4.31"
clap = { version = "4.4.11", features = ["derive"] }
clap_complete = { version = "4.4.6", optional = true }
glob = "0.3.1"
indicatif = { version = "0.17.7", features = ["improved_unicode"] }
pna = { version = "0.5.0", path = "../pna" }
Expand All @@ -25,6 +26,7 @@ nix = { version = "0.27.1", features = ["user", "fs"] }

[features]
experimental = []
generate = ["experimental", "clap_complete"]

[[bin]]
name = "pna"
Expand Down
4 changes: 2 additions & 2 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use bytesize::ByteSize;
use clap::{value_parser, ArgGroup, Parser, Subcommand, ValueEnum};
use std::path::PathBuf;

#[derive(Parser, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[derive(Parser, Clone, Eq, PartialEq, Hash, Debug)]
#[command(
name = env!("CARGO_PKG_NAME"),
version,
Expand Down Expand Up @@ -47,7 +47,7 @@ pub(crate) enum Verbosity {
Verbose,
}

#[derive(Subcommand, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[derive(Subcommand, Clone, Eq, PartialEq, Hash, Debug)]
pub(crate) enum Commands {
#[command(visible_alias = "c", about = "Create archive")]
Create(CreateArgs),
Expand Down
6 changes: 1 addition & 5 deletions cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ pub(super) mod experimental;
pub mod extract;
pub mod list;

#[cfg(feature = "experimental")]
use self::experimental::ExperimentalCommands;
use crate::cli::{CipherAlgorithmArgs, Cli, Commands, PasswordArgs, Verbosity};
use std::io;

Expand All @@ -18,9 +16,7 @@ pub fn entry(cli: Cli) -> io::Result<()> {
Commands::Extract(args) => args.execute(cli.verbosity.verbosity()),
Commands::List(args) => args.execute(cli.verbosity.verbosity()),
#[cfg(feature = "experimental")]
Commands::Experimental(cmd) => match cmd.command {
ExperimentalCommands::Split(cmd) => cmd.execute(cli.verbosity.verbosity()),
},
Commands::Experimental(cmd) => cmd.execute(cli.verbosity.verbosity()),
}
}

Expand Down
43 changes: 40 additions & 3 deletions cli/src/command/experimental.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
#[cfg(feature = "generate")]
use crate::cli::Cli;
use crate::{
cli::Verbosity,
command::{commons::split_to_parts, Command},
utils::part_name,
};
use bytesize::ByteSize;
#[cfg(feature = "generate")]
use clap::CommandFactory;
use clap::{Args, Parser, Subcommand};
#[cfg(feature = "generate")]
use clap_complete::{generate, Generator, Shell};
use pna::{Archive, EntryPart, MIN_CHUNK_BYTES_SIZE, PNA_HEADER};
#[cfg(feature = "generate")]
use std::env;
use std::{fs::File, io, path::PathBuf};

#[derive(Args, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[command(args_conflicts_with_subcommands = true)]
#[derive(Args, Clone, Eq, PartialEq, Hash, Debug)]
#[command(args_conflicts_with_subcommands = true, arg_required_else_help = true)]
pub(crate) struct ExperimentalArgs {
#[command(subcommand)]
pub(crate) command: ExperimentalCommands,
pub(crate) command: Option<ExperimentalCommands>,
#[cfg(feature = "generate")]
#[arg(long, help = "Generate shell auto complete")]
pub(crate) generate: Option<Shell>,
}

impl Command for ExperimentalArgs {
fn execute(self, verbosity: Verbosity) -> io::Result<()> {
#[cfg(feature = "generate")]
if let Some(shell) = self.generate {
let cmd = &mut Cli::command();
print_completions(shell, cmd);
return Ok(());
};
match self.command {
Some(ExperimentalCommands::Split(cmd)) => cmd.execute(verbosity),
None => unreachable!(),
}
}
}

#[derive(Subcommand, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
Expand Down Expand Up @@ -91,3 +117,14 @@ fn split_archive(args: SplitCommand, verbosity: Verbosity) -> io::Result<()> {
writer.finalize()?;
Ok(())
}

#[cfg(feature = "generate")]
fn print_completions<G: Generator>(gen: G, cmd: &mut clap::Command) {
let name = env::args().next().map(PathBuf::from).unwrap();
generate(
gen,
cmd,
name.file_name().unwrap().to_string_lossy(),
&mut io::stdout(),
);
}