Skip to content

Commit

Permalink
feat: fvm version command (#3626)
Browse files Browse the repository at this point in the history
Provides a `fvm version` command to print CLI version details.

---

## Demo

![CleanShot 2023-10-25 at 20 08 41](https://github.com/infinyon/fluvio/assets/34756077/1d297464-5b29-4ae6-a2e7-f2b5b81cd92e)
  • Loading branch information
EstebanBorai committed Oct 26, 2023
1 parent 4c0f1e8 commit 744ee4e
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion crates/fluvio-version-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,23 @@ 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 }
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"
1 change: 1 addition & 0 deletions crates/fluvio-version-manager/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ pub mod install;
pub mod itself;
pub mod show;
pub mod switch;
pub mod version;
54 changes: 54 additions & 0 deletions crates/fluvio-version-manager/src/command/version.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
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<String> {
let sys = sysinfo::System::new_all();
let info = format!(
"{} {} (kernel {})",
sys.name()?,
sys.os_version()?,
sys.kernel_version()?,
);

Some(info)
}
17 changes: 13 additions & 4 deletions crates/fluvio-version-manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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")]
Expand All @@ -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 {
Expand All @@ -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(),
}
}
}
27 changes: 27 additions & 0 deletions tests/cli/fvm_smoke_tests/fvm_basic.bats
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down Expand Up @@ -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
}

0 comments on commit 744ee4e

Please sign in to comment.