From 091f7be227f6e5b8beeea8612a2fa8dff45ceca4 Mon Sep 17 00:00:00 2001 From: Esteban Borai Date: Wed, 25 Oct 2023 14:30:46 -0300 Subject: [PATCH 1/3] feat: fvm version command --- Cargo.lock | 4 +- crates/fluvio-version-manager/.gitignore | 1 - crates/fluvio-version-manager/Cargo.toml | 4 +- .../fluvio-version-manager/src/command/mod.rs | 1 + .../src/command/version.rs | 60 +++++++++++++++++++ crates/fluvio-version-manager/src/main.rs | 17 ++++-- tests/cli/fvm_smoke_tests/fvm_basic.bats | 31 ++++++++++ 7 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 crates/fluvio-version-manager/src/command/version.rs 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/.gitignore b/crates/fluvio-version-manager/.gitignore index d5f3b82b77..e69de29bb2 100644 --- a/crates/fluvio-version-manager/.gitignore +++ b/crates/fluvio-version-manager/.gitignore @@ -1 +0,0 @@ -!/fixtures/* 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..4f4e9a4dc2 --- /dev/null +++ b/crates/fluvio-version-manager/src/command/version.rs @@ -0,0 +1,60 @@ +//! 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 { + #[clap(long, help = "Print extra binary information")] + verbose: bool, +} + +impl VersionOpt { + pub fn process(self) -> Result<()> { + println!("{BINARY_NAME} {BINARY_VERSION}"); + + if self.verbose { + println!("arch: {CURRENT_PLATFORM}"); + + if let Some(sha) = self.format_cli_sha() { + println!("checksum (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..46505a2ef0 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,30 @@ setup_file() { rm -rf $FLUVIO_HOME_DIR assert_success } + +@test "Prints version with and without 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 $FVM_CARGO_TOML_VERSION" + assert_success + + run bash -c 'fvm version --verbose' + assert_line --index 0 "fvm $FVM_CARGO_TOML_VERSION" + assert_line --index 1 --partial "arch: " + assert_line --index 2 --partial "checksum (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 +} From 66ad61dcf3627a34a8601a6f9412efaf30695e11 Mon Sep 17 00:00:00 2001 From: Esteban Borai Date: Wed, 25 Oct 2023 17:56:33 -0300 Subject: [PATCH 2/3] try: use relative path from root --- crates/fluvio-version-manager/.gitignore | 1 + tests/cli/fvm_smoke_tests/fvm_basic.bats | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/crates/fluvio-version-manager/.gitignore b/crates/fluvio-version-manager/.gitignore index e69de29bb2..d5f3b82b77 100644 --- a/crates/fluvio-version-manager/.gitignore +++ b/crates/fluvio-version-manager/.gitignore @@ -0,0 +1 @@ +!/fixtures/* diff --git a/tests/cli/fvm_smoke_tests/fvm_basic.bats b/tests/cli/fvm_smoke_tests/fvm_basic.bats index 46505a2ef0..60cc707a49 100644 --- a/tests/cli/fvm_smoke_tests/fvm_basic.bats +++ b/tests/cli/fvm_smoke_tests/fvm_basic.bats @@ -48,7 +48,7 @@ setup_file() { 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)" + 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" } From 6e7a9fa61c0d3a8cba98650d347a2ec5bd572c00 Mon Sep 17 00:00:00 2001 From: Esteban Borai Date: Wed, 25 Oct 2023 19:38:18 -0300 Subject: [PATCH 3/3] feat: do not use verbose --- .../src/command/version.rs | 22 +++++++------------ tests/cli/fvm_smoke_tests/fvm_basic.bats | 12 ++++------ 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/crates/fluvio-version-manager/src/command/version.rs b/crates/fluvio-version-manager/src/command/version.rs index 4f4e9a4dc2..8d958cfa55 100644 --- a/crates/fluvio-version-manager/src/command/version.rs +++ b/crates/fluvio-version-manager/src/command/version.rs @@ -9,25 +9,19 @@ use sysinfo::SystemExt; use crate::{BINARY_NAME, BINARY_VERSION}; #[derive(Debug, Args)] -pub struct VersionOpt { - #[clap(long, help = "Print extra binary information")] - verbose: bool, -} +pub struct VersionOpt; impl VersionOpt { pub fn process(self) -> Result<()> { - println!("{BINARY_NAME} {BINARY_VERSION}"); - - if self.verbose { - println!("arch: {CURRENT_PLATFORM}"); + println!("{BINARY_NAME} CLI: {BINARY_VERSION}"); + println!("{BINARY_NAME} CLI Arch: {CURRENT_PLATFORM}"); - if let Some(sha) = self.format_cli_sha() { - println!("checksum (sha256): {}", sha); - } + if let Some(sha) = self.format_cli_sha() { + println!("{BINARY_NAME} CLI SHA256: {}", sha); + } - if let Some(info) = os_info() { - println!("OS Details: {info}"); - } + if let Some(info) = os_info() { + println!("OS Details: {info}"); } Ok(()) diff --git a/tests/cli/fvm_smoke_tests/fvm_basic.bats b/tests/cli/fvm_smoke_tests/fvm_basic.bats index 60cc707a49..22b780c4fe 100644 --- a/tests/cli/fvm_smoke_tests/fvm_basic.bats +++ b/tests/cli/fvm_smoke_tests/fvm_basic.bats @@ -648,7 +648,7 @@ setup_file() { assert_success } -@test "Prints version with and without details on fvm version" { +@test "Prints version with details on fvm version" { run bash -c '$FVM_BIN self install' assert_success @@ -656,13 +656,9 @@ setup_file() { source ~/.fvm/env run bash -c 'fvm version' - assert_line --index 0 "fvm $FVM_CARGO_TOML_VERSION" - assert_success - - run bash -c 'fvm version --verbose' - assert_line --index 0 "fvm $FVM_CARGO_TOML_VERSION" - assert_line --index 1 --partial "arch: " - assert_line --index 2 --partial "checksum (sha256): " + 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