Skip to content

Commit

Permalink
refactor: use version type rather than strings
Browse files Browse the repository at this point in the history
Upgrade to `sn-releases` version 0.2.0, which forces the use of the `semver::Version` type rather
than `String` for dealing with versions.

It made sense to do the same thing with string-based version fields in this crate. All of the fields
on the `Settings` type were also turned into an `Option`, which makes sense because it's possible
that we don't have one of the asset types installed. I'm not sure why this wasn't done in the first
place. It made the code a bit simpler.

BREAKING CHANGE: settings files from previous versions will not deserialize properly and will have
to be deleted.
  • Loading branch information
jacderida authored and joshuef committed Mar 21, 2024
1 parent f13c139 commit ff7c965
Show file tree
Hide file tree
Showing 4 changed files with 326 additions and 277 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ semver = "1.0.4"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
sn-releases = "0.1.7"
sn-releases = "0.2.0"
tempfile = "3.8.1"
textwrap = "0.16.0"
tokio = { version = "1.26", features = ["full"] }
Expand Down
63 changes: 32 additions & 31 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use crate::update::{perform_update_assessment, UpdateAssessmentResult};
use color_eyre::{eyre::eyre, Result};
use lazy_static::lazy_static;
use prettytable::{Cell, Row, Table};
use sn_releases::SafeReleaseRepositoryInterface;
use semver::Version;
use sn_releases::SafeReleaseRepoActions;
use std::collections::HashMap;
use std::env::consts::{ARCH, OS};
use std::path::PathBuf;
Expand Down Expand Up @@ -50,6 +51,11 @@ pub(crate) async fn process_install_cmd(
get_default_install_path()?
};

let version = if let Some(version) = version {
Some(Version::parse(&version)?)
} else {
None
};
do_install_binary(&asset_type, dest_dir_path.clone(), version).await?;

if !no_modify_shell_profile {
Expand All @@ -68,26 +74,19 @@ pub(crate) async fn process_update_cmd() -> Result<()> {
let safe_config_dir_path = get_safe_config_dir_path()?;
let settings_file_path = safe_config_dir_path.join("safeup.json");
let settings = Settings::read(&settings_file_path)?;
let release_repo = <dyn SafeReleaseRepositoryInterface>::default_config();
let release_repo = <dyn SafeReleaseRepoActions>::default_config();

for asset_type in AssetType::variants() {
println!("Retrieving latest version for {asset_type}...");
let latest_version = release_repo
.get_latest_version(&asset_type.get_release_type())
.await?;
println!("Latest version of {asset_type} is {latest_version}");
if settings.is_installed(&asset_type) {
println!(
"Current version of {asset_type} is {}",
settings.get_installed_version(&asset_type)
);
}

let decision = perform_update_assessment(&asset_type, &latest_version, &settings)?;
match decision {
UpdateAssessmentResult::PerformUpdate => {
UpdateAssessmentResult::PerformUpdate(installed_path) => {
println!("Updating {asset_type} to {latest_version}...");
let installed_path = settings.get_install_path(&asset_type).clone();
let installed_dir_path = installed_path
.parent()
.ok_or_else(|| eyre!("could not retrieve parent directory"))?;
Expand Down Expand Up @@ -126,19 +125,21 @@ pub(crate) fn process_ls_command() -> Result<()> {
Cell::new("Path"),
]));
for asset_type in AssetType::variants() {
let installed_path = settings.get_install_path(&asset_type);
let wrapped_install_path = textwrap::wrap(
installed_path
.to_str()
.ok_or_else(|| eyre!("could not obtain install path"))?,
WRAP_LENGTH,
)
.join("\n");
table.add_row(Row::new(vec![
Cell::new(&asset_type.to_string()),
Cell::new(&settings.get_installed_version(&asset_type)),
Cell::new(&wrapped_install_path),
]));
if let Some((installed_path, installed_version)) = settings.get_install_details(&asset_type)
{
let wrapped_install_path = textwrap::wrap(
installed_path
.to_str()
.ok_or_else(|| eyre!("could not obtain install path"))?,
WRAP_LENGTH,
)
.join("\n");
table.add_row(Row::new(vec![
Cell::new(&asset_type.to_string()),
Cell::new(&installed_version.to_string()),
Cell::new(&wrapped_install_path),
]));
}
}
table.printstd();
Ok(())
Expand All @@ -147,10 +148,10 @@ pub(crate) fn process_ls_command() -> Result<()> {
async fn do_install_binary(
asset_type: &AssetType,
dest_dir_path: PathBuf,
version: Option<String>,
version: Option<Version>,
) -> Result<()> {
let platform = get_platform()?;
let release_repo = <dyn SafeReleaseRepositoryInterface>::default_config();
let release_repo = <dyn SafeReleaseRepoActions>::default_config();
let (installed_version, bin_path) = crate::install::install_bin(
asset_type.clone(),
release_repo,
Expand All @@ -165,16 +166,16 @@ async fn do_install_binary(
let mut settings = Settings::read(&settings_file_path)?;
match asset_type {
AssetType::Client => {
settings.safe_path = bin_path;
settings.safe_version = installed_version;
settings.safe_path = Some(bin_path);
settings.safe_version = Some(installed_version);
}
AssetType::Node => {
settings.safenode_path = bin_path;
settings.safenode_version = installed_version;
settings.safenode_path = Some(bin_path);
settings.safenode_version = Some(installed_version);
}
AssetType::NodeManager => {
settings.safenode_manager_path = bin_path;
settings.safenode_manager_version = installed_version;
settings.safenode_manager_path = Some(bin_path);
settings.safenode_manager_version = Some(installed_version);
}
}
settings.save(&settings_file_path)?;
Expand Down

0 comments on commit ff7c965

Please sign in to comment.