diff --git a/Cargo.lock b/Cargo.lock index b566097b59..21519e8d0e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2697,7 +2697,7 @@ dependencies = [ [[package]] name = "fluvio-compression" -version = "0.3.1" +version = "0.3.2" dependencies = [ "bytes 1.5.0", "flate2", @@ -3413,6 +3413,7 @@ dependencies = [ "clap", "colored", "comfy-table", + "current_platform", "dialoguer 0.11.0", "dirs 5.0.1", "fluvio-future", @@ -3423,6 +3424,7 @@ dependencies = [ "serde", "serde_json", "sha2 0.10.8", + "sysinfo", "tempfile", "toml 0.8.2", "tracing", diff --git a/crates/fluvio-version-manager/Cargo.toml b/crates/fluvio-version-manager/Cargo.toml index 2e09f3658a..297cf71f5c 100644 --- a/crates/fluvio-version-manager/Cargo.toml +++ b/crates/fluvio-version-manager/Cargo.toml @@ -20,6 +20,7 @@ async-std = { workspace = true, features = ["attributes"] } clap = { workspace = true, features = ["std", "color", "derive", "env", "help"] } colored = { workspace = true } comfy-table = { workspace = true } +current_platform = { workspace = true } dialoguer = { workspace = true } dirs = { workspace = true } hex = { workspace = true } @@ -27,14 +28,15 @@ semver = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } sha2 = { workspace = true } +sysinfo = { workspace = true } tempfile = { workspace = true } tracing = { workspace = true } toml = { workspace = true } url = { workspace = true } # Workspace Crates -fluvio-hub-util = { workspace = true } fluvio-future = { workspace = true, features = ["subscriber"] } +fluvio-hub-util = { workspace = true } [dev-dependencies] fs_extra = "1.3.0" diff --git a/crates/fluvio-version-manager/src/command/mod.rs b/crates/fluvio-version-manager/src/command/mod.rs index 71be487648..95627405f1 100644 --- a/crates/fluvio-version-manager/src/command/mod.rs +++ b/crates/fluvio-version-manager/src/command/mod.rs @@ -3,3 +3,4 @@ pub mod install; pub mod itself; pub mod show; pub mod switch; +pub mod version; diff --git a/crates/fluvio-version-manager/src/command/version.rs b/crates/fluvio-version-manager/src/command/version.rs new file mode 100644 index 0000000000..8d958cfa55 --- /dev/null +++ b/crates/fluvio-version-manager/src/command/version.rs @@ -0,0 +1,54 @@ +//! Prints version information to stdout + +use anyhow::Result; +use clap::Args; +use current_platform::CURRENT_PLATFORM; +use sha2::{Digest, Sha256}; +use sysinfo::SystemExt; + +use crate::{BINARY_NAME, BINARY_VERSION}; + +#[derive(Debug, Args)] +pub struct VersionOpt; + +impl VersionOpt { + pub fn process(self) -> Result<()> { + println!("{BINARY_NAME} CLI: {BINARY_VERSION}"); + println!("{BINARY_NAME} CLI Arch: {CURRENT_PLATFORM}"); + + if let Some(sha) = self.format_cli_sha() { + println!("{BINARY_NAME} CLI SHA256: {}", sha); + } + + if let Some(info) = os_info() { + println!("OS Details: {info}"); + } + + Ok(()) + } + + /// Read CLI and compute its sha256 + fn format_cli_sha(&self) -> Option { + let path = std::env::current_exe().ok()?; + let bin = std::fs::read(path).ok()?; + let mut hasher = Sha256::new(); + + hasher.update(bin); + + let bin_sha256 = hasher.finalize(); + + Some(format!("{:x}", &bin_sha256)) + } +} + +fn os_info() -> Option { + let sys = sysinfo::System::new_all(); + let info = format!( + "{} {} (kernel {})", + sys.name()?, + sys.os_version()?, + sys.kernel_version()?, + ); + + Some(info) +} diff --git a/crates/fluvio-version-manager/src/main.rs b/crates/fluvio-version-manager/src/main.rs index 274ced6df1..4678123445 100644 --- a/crates/fluvio-version-manager/src/main.rs +++ b/crates/fluvio-version-manager/src/main.rs @@ -3,14 +3,21 @@ mod common; use anyhow::Result; use clap::Parser; -use command::current::CurrentOpt; -use command::show::ShowOpt; +use self::command::current::CurrentOpt; use self::command::install::InstallOpt; use self::command::itself::SelfOpt; +use self::command::show::ShowOpt; use self::command::switch::SwitchOpt; +use self::command::version::VersionOpt; use self::common::notify::Notify; +/// Binary name is read from `Cargo.toml` `[[bin]]` section +pub const BINARY_NAME: &str = env!("CARGO_BIN_NAME"); + +/// Binary version is read from `Cargo.toml` `version` field +pub const BINARY_VERSION: &str = env!("CARGO_PKG_VERSION"); + #[async_std::main] async fn main() -> Result<()> { fluvio_future::subscriber::init_tracer(None); @@ -23,10 +30,9 @@ async fn main() -> Result<()> { #[derive(Debug, Parser)] #[command( + name = BINARY_NAME, about = "Fluvio Version Manager (FVM)", - name = "fvm", max_term_width = 100, - version = env!("CARGO_PKG_VERSION") )] pub struct Cli { #[clap(long, short = 'q', help = "Suppress all output")] @@ -52,6 +58,8 @@ pub enum Command { /// Set a installed Fluvio Version as active #[command(name = "switch")] Switch(SwitchOpt), + /// Prints version information + Version(VersionOpt), } impl Cli { @@ -66,6 +74,7 @@ impl Cli { Command::Install(cmd) => cmd.process(notify).await, Command::Show(cmd) => cmd.process(notify).await, Command::Switch(cmd) => cmd.process(notify).await, + Command::Version(cmd) => cmd.process(), } } } diff --git a/tests/cli/fvm_smoke_tests/fvm_basic.bats b/tests/cli/fvm_smoke_tests/fvm_basic.bats index e193d267d1..22b780c4fe 100644 --- a/tests/cli/fvm_smoke_tests/fvm_basic.bats +++ b/tests/cli/fvm_smoke_tests/fvm_basic.bats @@ -47,6 +47,10 @@ setup_file() { FLUVIO_BINARIES_DIR="$FLUVIO_HOME_DIR/bin" export FLUVIO_BINARIES_DIR debug_msg "Fluvio Binaries Directory: $FLUVIO_BINARIES_DIR" + + FVM_CARGO_TOML_VERSION="$(yq -oy '.package.version' ./crates/fluvio-version-manager/Cargo.toml)" + export FVM_CARGO_TOML_VERSION + debug_msg "Version File Value: $FVM_CARGO_TOML_VERSION" } @test "Install fvm and setup a settings.toml file" { @@ -643,3 +647,26 @@ setup_file() { rm -rf $FLUVIO_HOME_DIR assert_success } + +@test "Prints version with details on fvm version" { + run bash -c '$FVM_BIN self install' + assert_success + + # Sets `fvm` in the PATH using the "env" file included in the installation + source ~/.fvm/env + + run bash -c 'fvm version' + assert_line --index 0 "fvm CLI: $FVM_CARGO_TOML_VERSION" + assert_line --index 1 --partial "fvm CLI Arch: " + assert_line --index 2 --partial "fvm CLI SHA256: " + assert_line --index 3 --partial "OS Details: " + assert_success + + # Removes FVM + run bash -c 'fvm self uninstall --yes' + assert_success + + # Removes Fluvio + rm -rf $FLUVIO_HOME_DIR + assert_success +}