Skip to content

Commit

Permalink
feat: update command for fvm
Browse files Browse the repository at this point in the history
  • Loading branch information
EstebanBorai committed Oct 31, 2023
1 parent 0a8b819 commit 59663a2
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
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,4 +3,5 @@ pub mod install;
pub mod itself;
pub mod show;
pub mod switch;
pub mod update;
pub mod version;
105 changes: 105 additions & 0 deletions crates/fluvio-version-manager/src/command/update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
//! Updates version of the current channel to the most recent one
use anyhow::{Result, Error};
use clap::Args;
use colored::Colorize;
use semver::Version;
use url::Url;

use fluvio_hub_util::HUB_REMOTE;
use fluvio_hub_util::fvm::{Client, DEFAULT_PKGSET, Channel, PackageSet};

use crate::common::TARGET;
use crate::common::notify::Notify;
use crate::common::settings::Settings;
use crate::common::version_installer::VersionInstaller;

#[derive(Debug, Args)]
pub struct UpdateOpt {
/// Registry used to fetch Fluvio Versions
#[arg(long, env = "HUB_REGISTRY_URL", default_value = HUB_REMOTE)]
registry: Url,
}

impl UpdateOpt {
pub async fn process(self, notify: Notify) -> Result<()> {
let settings = Settings::open()?;
let Some(channel) = settings.channel else {
notify.info("No channel set, please set a channel first using `fvm switch`");
return Ok(());
};

if channel.is_version_tag() {
// Abort early if the user is not using a Channel and instead has
// a static tag set as active
notify.info("Cannot update a static version tag. You must use a Channel.");
return Ok(());
}

let latest_pkgset = self.fetch_latest_version(&channel).await?;
let Some(version) = settings.version else {
notify.info(
"No installed version detected, please install a version first using `fvm install`",
);
return Ok(());
};

match channel {
Channel::Stable => {
if latest_pkgset.version > version {
notify.info(format!(
"Updating fluvio {} to version {}. Current version is {}.",
channel.to_string().bold(),
latest_pkgset.version,
version
));

return VersionInstaller::new(channel, latest_pkgset, notify)
.install()
.await;
}

notify.done("You are already up to date");
}
Channel::Latest => {
// The latest tag can be very dynamic, so we just check for this
// tag to be different than the current version assuming
// upstream is always up to date
if latest_pkgset.version != version {
notify.info(format!(
"Updating fluvio {} to version {}. Current version is {}.",
channel.to_string().bold(),
latest_pkgset.version,
version
));

return VersionInstaller::new(channel, latest_pkgset, notify)
.install()
.await;
}

notify.done("You are already up to date");
}
Channel::Tag(_) => {
notify.warn("Static tags cannot be updated. No changes made.");
}
}

Ok(())
}

async fn fetch_latest_version(&self, channel: &Channel) -> Result<PackageSet> {
if channel.is_version_tag() {
return Err(Error::msg(
"Cannot update a static version tag. You must use a Channel.",
));
}

let client = Client::new(self.registry.as_str())?;
let pkgset = client
.fetch_package_set(DEFAULT_PKGSET, channel, TARGET)
.await?;

Ok(pkgset)
}
}
5 changes: 5 additions & 0 deletions crates/fluvio-version-manager/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use self::command::install::InstallOpt;
use self::command::itself::SelfOpt;
use self::command::show::ShowOpt;
use self::command::switch::SwitchOpt;
use self::command::update::UpdateOpt;
use self::command::version::VersionOpt;
use self::common::notify::Notify;

Expand Down Expand Up @@ -59,6 +60,9 @@ pub enum Command {
/// Set a installed Fluvio Version as active
#[command(name = "switch")]
Switch(SwitchOpt),
/// Updates the current channel version to the most recent
#[command(name = "update")]
Update(UpdateOpt),
/// Prints version information
Version(VersionOpt),
}
Expand All @@ -75,6 +79,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::Update(cmd) => cmd.process(notify).await,
Command::Version(cmd) => cmd.process(),
}
}
Expand Down

0 comments on commit 59663a2

Please sign in to comment.