From 431538e9c90090c8802460f3a625a0cd38a5e72a Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Fri, 14 Feb 2025 16:21:39 +0100 Subject: feat(version): Include `yt-dlp` and `python` version in `--version` Both are run-time dependencies and need to be up-to-date (especially, `yt-dlp`) --- yt/src/cli.rs | 6 +++++- yt/src/main.rs | 6 ++++++ yt/src/version/mod.rs | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 yt/src/version/mod.rs diff --git a/yt/src/cli.rs b/yt/src/cli.rs index d7e084a..5e7af5b 100644 --- a/yt/src/cli.rs +++ b/yt/src/cli.rs @@ -22,7 +22,7 @@ use crate::{ }; #[derive(Parser, Debug)] -#[clap(author, version, about, long_about = None)] +#[clap(author, about, long_about = None)] #[allow(clippy::module_name_repetitions)] /// An command line interface to select, download and watch videos pub struct CliArgs { @@ -30,6 +30,10 @@ pub struct CliArgs { /// The subcommand to execute [default: select] pub command: Option, + /// Show the version and exit + #[arg(long, short = 'V', action= ArgAction::SetTrue)] + pub version: bool, + /// Increase message verbosity #[arg(long="verbose", short = 'v', action = ArgAction::Count)] pub verbosity: u8, diff --git a/yt/src/main.rs b/yt/src/main.rs index fbfdac0..3097eea 100644 --- a/yt/src/main.rs +++ b/yt/src/main.rs @@ -50,6 +50,7 @@ pub mod status; pub mod storage; pub mod subscribe; pub mod update; +pub mod version; pub mod videos; pub mod watch; @@ -59,6 +60,11 @@ pub mod watch; async fn main() -> Result<()> { let args = cli::CliArgs::parse(); + if args.version { + version::show()?; + return Ok(()); + } + // The default verbosity is 1 (Warn) let verbosity: u8 = args.verbosity + 1; diff --git a/yt/src/version/mod.rs b/yt/src/version/mod.rs new file mode 100644 index 0000000..369541e --- /dev/null +++ b/yt/src/version/mod.rs @@ -0,0 +1,32 @@ +use std::process::Command; + +use anyhow::{Context, Result}; + +fn get_cmd_version(cmd: &str) -> Result { + let out = String::from_utf8( + Command::new(cmd) + .arg("--version") + .output() + .with_context(|| format!("Failed to run `{cmd} --version`"))? + .stdout, + ) + .context("Failed to interpret output as utf8")?; + + Ok(out.trim().to_owned()) +} + +pub fn show() -> Result<()> { + let python_version = get_cmd_version("python")?; + let yt_dlp_version = get_cmd_version("yt-dlp")?; + + println!( + "{}: {} + +python: {python_version} +yt-dlp: {yt_dlp_version}", + env!("CARGO_PKG_NAME"), + env!("CARGO_PKG_VERSION"), + ); + + Ok(()) +} -- cgit 1.4.1