Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add shell completion to cli-next

Manav Rathie9a1219738d0
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index b947531e56..fe6ffd0cff 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -1354,6 +1354,15 @@ dependencies = [
"strsim",
]
+[[package]]
+name = "clap_complete"
+version = "4.6.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "037e2a1a92236d0aff7e845093f64661d6df4c02c9fcc61a60e9e1d736fa392f"
+dependencies = [
+ "clap",
+]
+
[[package]]
name = "clap_derive"
version = "4.6.7"
@@ -2537,6 +2546,7 @@ dependencies = [
"anyhow",
"chrono",
"clap",
+ "clap_complete",
"dialoguer",
"dirs",
"ente-accounts",
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index b7e7c781dc..923c901395 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -54,6 +54,7 @@ edition = "2024"
anyhow = "1.0.104"
chrono = { version = "0.4.45", default-features = false }
clap = "4.6.7"
+clap_complete = "4.6.11"
dialoguer = { version = "0.12.0", default-features = false }
dirs = "7.0.0"
ente-accounts = { path = "crates/accounts" }
diff --git a/rust/apps/cli-next/Cargo.toml b/rust/apps/cli-next/Cargo.toml
index 18ec8c5b11..b285027972 100644
--- a/rust/apps/cli-next/Cargo.toml
+++ b/rust/apps/cli-next/Cargo.toml
@@ -11,6 +11,7 @@ museum = []
anyhow.workspace = true
chrono = { workspace = true, features = ["std", "clock"] }
clap = { workspace = true, features = ["derive"] }
+clap_complete.workspace = true
dialoguer = { workspace = true, features = ["password"] }
dirs.workspace = true
ente-accounts.workspace = true
diff --git a/rust/apps/cli-next/src/args.rs b/rust/apps/cli-next/src/args.rs
index a3aef029c1..8f8b7ebbed 100644
--- a/rust/apps/cli-next/src/args.rs
+++ b/rust/apps/cli-next/src/args.rs
@@ -1,6 +1,7 @@
use std::{io::IsTerminal, num::NonZeroUsize, path::PathBuf};
-use clap::{Args, Parser, Subcommand, ValueEnum};
+use clap::{Arg, ArgAction, Args, CommandFactory, Parser, Subcommand, ValueEnum};
+use clap_complete::Shell;
use serde::{Deserialize, Serialize};
#[derive(Parser)]
@@ -27,6 +28,15 @@ pub struct Cli {
pub command: Command,
}
+pub fn command() -> clap::Command {
+ Cli::command().subcommand_required(false).arg(
+ Arg::new("help-all")
+ .long("help-all")
+ .action(ArgAction::SetTrue)
+ .help("Print help for all commands"),
+ )
+}
+
#[derive(Args)]
pub struct Options {
#[arg(long, global = true, help = "Print the result as JSON")]
@@ -122,6 +132,8 @@ Keep the same home and key across unattended invocations."
#[command(subcommand)]
command: VaultCommand,
},
+ #[command(about = "Print a shell completion script")]
+ Completion { shell: Shell },
}
#[derive(Subcommand)]
diff --git a/rust/apps/cli-next/src/main.rs b/rust/apps/cli-next/src/main.rs
index 16f5565f4f..8110492cb1 100644
--- a/rust/apps/cli-next/src/main.rs
+++ b/rust/apps/cli-next/src/main.rs
@@ -17,7 +17,7 @@ use std::{
};
use anyhow::{Context, Result, bail, ensure};
-use clap::{Arg, ArgAction, CommandFactory, FromArgMatches};
+use clap::FromArgMatches;
use ente_core::{b64, crypto::Key};
use serde::de::DeserializeOwned;
use serde_json::json;
@@ -29,12 +29,7 @@ use vault::{Account, State, Vault};
#[tokio::main]
async fn main() {
- let mut command = Cli::command().subcommand_required(false).arg(
- Arg::new("help-all")
- .long("help-all")
- .action(ArgAction::SetTrue)
- .help("Print help for all commands"),
- );
+ let mut command = args::command();
let matches = command.get_matches_mut();
let result = if matches.get_flag("help-all") {
command.build();
@@ -91,6 +86,12 @@ async fn run(cli: Cli) -> Result<()> {
command: KeyCommand::Generate,
},
} => vault_key(options.json),
+ Command::Completion { shell } => {
+ let mut command = args::command();
+ let name = command.get_name().to_owned();
+ clap_complete::generate(shell, &mut command, name, &mut std::io::stdout().lock());
+ Ok(())
+ }
}
}
diff --git a/rust/apps/cli-next/tests/cli.rs b/rust/apps/cli-next/tests/cli.rs
index b09d75a939..7874728f87 100644
--- a/rust/apps/cli-next/tests/cli.rs
+++ b/rust/apps/cli-next/tests/cli.rs
@@ -13,6 +13,28 @@ use ente_core::{
use serde_json::{Value, json};
use tempfile::TempDir;
+#[test]
+fn completion_prints_scripts_without_storage() {
+ let home = TestHome::new();
+ let path = home.dir.path().join("unused");
+ for shell in ["bash", "zsh", "fish", "powershell", "elvish"] {
+ let output = success(
+ home.command(&["completion", shell, "--json", "--offline", "--no-input"])
+ .env("ENTE_CLI_HOME", &path)
+ .env("ENTE_CLI_VAULT_KEY", "invalid")
+ .output()
+ .unwrap(),
+ );
+ assert!(output.stderr.is_empty(), "{shell}");
+ let script = String::from_utf8(output.stdout).unwrap();
+ assert!(serde_json::from_str::<Value>(&script).is_err(), "{shell}");
+ for item in ["help-all", "account", "paste", "hardlinks", "completion"] {
+ assert!(script.contains(item), "{shell}: {item}");
+ }
+ assert!(!path.exists(), "{shell} created the CLI home");
+ }
+}
+
#[test]
fn reads_do_not_initialize_storage() {
let binary = format!("ente-cli-next{}", std::env::consts::EXE_SUFFIX);