From f83af8e0760b99d515539df10e856a1bf0508bdb Mon Sep 17 00:00:00 2001 From: Marc Hartmayer Date: Fri, 24 May 2024 09:40:22 +0000 Subject: [PATCH] rust: Generate shell (e.g. bash) completion scripts via build.rs for all tools It might be handy to have shell completion support for the Rust PV tools. Reviewed-by: Steffen Eiden Signed-off-by: Marc Hartmayer Signed-off-by: Steffen Eiden --- rust/Cargo.lock | 12 ++++++++++++ rust/pvapconfig/Cargo.toml | 5 +++++ rust/pvapconfig/build.rs | 24 ++++++++++++++++++++++++ rust/pvattest/Cargo.toml | 7 +++++++ rust/pvattest/build.rs | 25 +++++++++++++++++++++++++ rust/pvsecret/Cargo.toml | 7 +++++++ rust/pvsecret/build.rs | 24 ++++++++++++++++++++++++ 7 files changed, 104 insertions(+) create mode 100644 rust/pvapconfig/build.rs create mode 100644 rust/pvattest/build.rs create mode 100644 rust/pvsecret/build.rs diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 1e48424b..8248ecab 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -127,6 +127,15 @@ dependencies = [ "terminal_size", ] +[[package]] +name = "clap_complete" +version = "4.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f6b5c519bab3ea61843a7923d074b04245624bb84a64a8c150f5deb014e388b" +dependencies = [ + "clap", +] + [[package]] name = "clap_derive" version = "4.3.2" @@ -403,6 +412,7 @@ name = "pvapconfig" version = "0.10.0" dependencies = [ "clap", + "clap_complete", "lazy_static", "openssl", "rand", @@ -420,6 +430,7 @@ dependencies = [ "anyhow", "byteorder", "clap", + "clap_complete", "log", "s390_pv", "serde", @@ -434,6 +445,7 @@ version = "0.10.0" dependencies = [ "anyhow", "clap", + "clap_complete", "log", "s390_pv", "serde_yaml", diff --git a/rust/pvapconfig/Cargo.toml b/rust/pvapconfig/Cargo.toml index 345f4059..bf642659 100644 --- a/rust/pvapconfig/Cargo.toml +++ b/rust/pvapconfig/Cargo.toml @@ -16,3 +16,8 @@ regex = "1.7" serde = { version = "1.0.139", features = ["derive"] } serde_yaml = "0.9" utils = { path = "../utils" } + +[build-dependencies] +clap = { version ="4.1", features = ["derive", "wrap_help"]} +clap_complete = "4.1" +lazy_static = "1.1" diff --git a/rust/pvapconfig/build.rs b/rust/pvapconfig/build.rs new file mode 100644 index 00000000..b99f184b --- /dev/null +++ b/rust/pvapconfig/build.rs @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: MIT +// +// Copyright IBM Corp. 2024 +// it under the terms of the MIT license. See LICENSE for details. + +use clap::{CommandFactory, ValueEnum}; +use clap_complete::{generate_to, Shell}; +use std::env; +use std::io::Error; + +include!("src/cli.rs"); + +fn main() -> Result<(), Error> { + let outdir = env::var_os("OUT_DIR").unwrap(); + let crate_name = env!("CARGO_PKG_NAME"); + let mut cmd = Cli::command(); + for &shell in Shell::value_variants() { + generate_to(shell, &mut cmd, crate_name, &outdir)?; + } + + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=src/cli.rs"); + Ok(()) +} diff --git a/rust/pvattest/Cargo.toml b/rust/pvattest/Cargo.toml index 3ad2a019..15d8c4ef 100644 --- a/rust/pvattest/Cargo.toml +++ b/rust/pvattest/Cargo.toml @@ -15,3 +15,10 @@ zerocopy = { version="0.7", features = ["derive"] } pv = { path = "../pv", package = "s390_pv" } utils = { path = "../utils" } + +[build-dependencies] +clap = { version ="4.1", features = ["derive", "wrap_help"]} +clap_complete = "4.1" +log = { version = "0.4", features = ["std", "release_max_level_debug"] } + +utils = { path = "../utils" } diff --git a/rust/pvattest/build.rs b/rust/pvattest/build.rs new file mode 100644 index 00000000..4c79d3f9 --- /dev/null +++ b/rust/pvattest/build.rs @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: MIT +// +// Copyright IBM Corp. 2024 +// it under the terms of the MIT license. See LICENSE for details. + +use clap::CommandFactory; +use clap_complete::{generate_to, Shell}; +use std::env; +use std::io::Error; + +include!("src/cli.rs"); + +fn main() -> Result<(), Error> { + let outdir = env::var_os("OUT_DIR").unwrap(); + let crate_name = env!("CARGO_PKG_NAME"); + let mut cmd = CliOptions::command(); + for &shell in Shell::value_variants() { + generate_to(shell, &mut cmd, crate_name, &outdir)?; + } + + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=src/cli.rs"); + println!("cargo:rerun-if-changed=../utils/src/cli.rs"); + Ok(()) +} diff --git a/rust/pvsecret/Cargo.toml b/rust/pvsecret/Cargo.toml index d1093147..c5d1129b 100644 --- a/rust/pvsecret/Cargo.toml +++ b/rust/pvsecret/Cargo.toml @@ -12,3 +12,10 @@ serde_yaml = "0.9" pv = { path = "../pv" , package = "s390_pv" } utils = { path = "../utils"} + +[build-dependencies] +clap = { version ="4.1", features = ["derive", "wrap_help"]} +clap_complete = "4.1" +log = { version = "0.4", features = ["std", "release_max_level_debug"] } + +utils = { path = "../utils" } diff --git a/rust/pvsecret/build.rs b/rust/pvsecret/build.rs new file mode 100644 index 00000000..7c9bb779 --- /dev/null +++ b/rust/pvsecret/build.rs @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: MIT +// +// Copyright IBM Corp. 2024 +// it under the terms of the MIT license. See LICENSE for details. + +use clap_complete::{generate_to, Shell}; +use std::env; +use std::io::Error; + +include!("src/cli.rs"); + +fn main() -> Result<(), Error> { + let outdir = env::var_os("OUT_DIR").unwrap(); + let crate_name = env!("CARGO_PKG_NAME"); + let mut cmd = CliOptions::command(); + for &shell in Shell::value_variants() { + generate_to(shell, &mut cmd, crate_name, &outdir)?; + } + + println!("cargo:rerun-if-changed=build.rs"); + println!("cargo:rerun-if-changed=src/cli.rs"); + println!("cargo:rerun-if-changed=../utils/src/cli.rs"); + Ok(()) +}