Skip to content

Commit cb72bec

Browse files
committed
wip, review
1 parent 8a3e56b commit cb72bec

File tree

5 files changed

+30
-37
lines changed

5 files changed

+30
-37
lines changed

cmd/soroban-cli/src/cli.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ use dotenvy::dotenv;
33
use std::thread;
44
use tracing_subscriber::{fmt, EnvFilter};
55

6-
use crate::self_outdated_check::print_upgrade_prompt;
6+
use crate::upgrade_check::upgrade_check;
77
use crate::{commands, Root};
88

99
#[tokio::main]
1010
pub async fn main() {
1111
// Spawn a thread to print the upgrade prompt in the background
12-
thread::spawn(print_upgrade_prompt);
12+
thread::spawn(upgrade_check);
1313

1414
let _ = dotenv().unwrap_or_default();
1515

cmd/soroban-cli/src/config/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub mod data;
1818
pub mod locator;
1919
pub mod network;
2020
pub mod secret;
21-
pub mod self_outdated_check;
21+
pub mod upgrade_check;
2222

2323
#[derive(thiserror::Error, Debug)]
2424
pub enum Error {

cmd/soroban-cli/src/config/self_outdated_check.rs cmd/soroban-cli/src/config/upgrade_check.rs

+13-12
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ use crate::config::locator;
22
use jsonrpsee_core::Serialize;
33
use semver::Version;
44
use serde::Deserialize;
5+
use serde_json;
56
use std::fs;
67

7-
const FILE_NAME: &str = "self_outdated_check.toml";
8+
const FILE_NAME: &str = "upgrade_check.json";
89

910
/// The `SelfOutdatedCheck` struct represents the state of the self-outdated check.
10-
/// This state is global and stored in the `self_outdated_check.toml` file in
11+
/// This state is global and stored in the `self_outdated_check.json` file in
1112
/// the global configuration directory.
1213
#[derive(Serialize, Deserialize, Debug, PartialEq)]
13-
pub struct SelfOutdatedCheck {
14+
pub struct UpgradeCheck {
1415
/// The timestamp of the latest check for a new version of the CLI.
1516
pub latest_check_time: u64,
1617
/// The latest stable version of the CLI available on crates.io.
@@ -19,7 +20,7 @@ pub struct SelfOutdatedCheck {
1920
pub max_version: Version,
2021
}
2122

22-
impl Default for SelfOutdatedCheck {
23+
impl Default for UpgradeCheck {
2324
fn default() -> Self {
2425
Self {
2526
latest_check_time: 0,
@@ -29,7 +30,7 @@ impl Default for SelfOutdatedCheck {
2930
}
3031
}
3132

32-
impl SelfOutdatedCheck {
33+
impl UpgradeCheck {
3334
/// Loads the state of the self-outdated check from the global configuration directory.
3435
/// If the file doesn't exist, returns a default instance of `SelfOutdatedCheck`.
3536
pub fn load() -> Result<Self, locator::Error> {
@@ -39,14 +40,14 @@ impl SelfOutdatedCheck {
3940
}
4041
let data = fs::read(&path)
4142
.map_err(|error| locator::Error::SelfOutdatedCheckReadFailed { path, error })?;
42-
Ok(toml::from_slice(data.as_slice())?)
43+
Ok(serde_json::from_slice(data.as_slice())?)
4344
}
4445

45-
/// Saves the state of the self-outdated check to the `self_outdated_check.toml` file in the global configuration directory.
46+
/// Saves the state of the self-outdated check to the `self_outdated_check.json` file in the global configuration directory.
4647
pub fn save(&self) -> Result<(), locator::Error> {
4748
let path = locator::global_config_path()?.join(FILE_NAME);
4849
let path = locator::ensure_directory(path)?;
49-
let data = toml::to_string(self).map_err(|_| locator::Error::ConfigSerialization)?;
50+
let data = serde_json::to_string(self).map_err(|_| locator::Error::ConfigSerialization)?;
5051
fs::write(&path, data)
5152
.map_err(|error| locator::Error::SelfOutdatedCheckWriteFailed { path, error })
5253
}
@@ -64,19 +65,19 @@ mod tests {
6465
env::set_var("XDG_CONFIG_HOME", temp_dir.path());
6566

6667
// Test default loading
67-
let default_check = SelfOutdatedCheck::load().unwrap();
68-
assert_eq!(default_check, SelfOutdatedCheck::default());
68+
let default_check = UpgradeCheck::load().unwrap();
69+
assert_eq!(default_check, UpgradeCheck::default());
6970
assert_eq!(default_check.latest_check_time, 0);
7071
assert_eq!(default_check.max_stable_version, Version::new(0, 0, 0));
7172

7273
// Test saving and loading
73-
let saved_check = SelfOutdatedCheck {
74+
let saved_check = UpgradeCheck {
7475
latest_check_time: 1_234_567_890,
7576
max_stable_version: Version::new(1, 2, 3),
7677
max_version: Version::parse("1.2.4-rc.1").unwrap(),
7778
};
7879
saved_check.save().unwrap();
79-
let loaded_check = SelfOutdatedCheck::load().unwrap();
80+
let loaded_check = UpgradeCheck::load().unwrap();
8081
assert_eq!(loaded_check, saved_check);
8182
}
8283
}

cmd/soroban-cli/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ pub mod get_spec;
1818
pub mod key;
1919
pub mod log;
2020
pub mod print;
21-
pub mod self_outdated_check;
2221
pub mod signer;
2322
pub mod toid;
23+
pub mod upgrade_check;
2424
pub mod utils;
2525
pub mod wasm;
2626

cmd/soroban-cli/src/self_outdated_check.rs cmd/soroban-cli/src/upgrade_check.rs

+13-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::config::self_outdated_check::SelfOutdatedCheck;
2-
use crate::print::Print;
1+
use crate::config::upgrade_check::UpgradeCheck;
32
use semver::Version;
43
use serde::Deserialize;
54
use std::error::Error;
@@ -35,7 +34,7 @@ fn fetch_latest_crate_info() -> Result<Crate, Box<dyn Error>> {
3534
}
3635

3736
/// Print a warning if a new version of the CLI is available
38-
pub fn print_upgrade_prompt() {
37+
pub fn upgrade_check() {
3938
// We should skip the upgrade check if we're not in a tty environment.
4039
if !std::io::stderr().is_terminal() {
4140
return;
@@ -48,15 +47,11 @@ pub fn print_upgrade_prompt() {
4847
}
4948

5049
let current_version = crate::commands::version::pkg();
51-
let print = Print::new(false);
5250

53-
let mut stats = match SelfOutdatedCheck::load() {
54-
Ok(stats) => stats,
55-
Err(e) => {
56-
print.warnln(format!("Failed to load self outdated check data: {}", e));
57-
SelfOutdatedCheck::default()
58-
}
59-
};
51+
let mut stats = UpgradeCheck::load().unwrap_or_else(|e| {
52+
println!("Failed to load self outdated check data: {e}");
53+
UpgradeCheck::default()
54+
});
6055

6156
#[allow(clippy::cast_sign_loss)]
6257
let now = chrono::Utc::now().timestamp() as u64;
@@ -65,39 +60,36 @@ pub fn print_upgrade_prompt() {
6560
if now - stats.latest_check_time >= MINIMUM_CHECK_INTERVAL.as_secs() {
6661
match fetch_latest_crate_info() {
6762
Ok(c) => {
68-
stats = SelfOutdatedCheck {
63+
stats = UpgradeCheck {
6964
latest_check_time: now,
7065
max_stable_version: c.max_stable_version,
7166
max_version: c.max_version,
7267
};
7368
}
7469
Err(e) => {
75-
print.warnln(format!(
76-
"Failed to fetch stellar-cli info from crates.io: {}",
77-
e
78-
));
70+
println!("Failed to fetch stellar-cli info from crates.io: {e}");
7971
// Only update the latest check time if the fetch failed
8072
// This way we don't spam the user with errors
8173
stats.latest_check_time = now;
8274
}
8375
}
8476

8577
if let Err(e) = stats.save() {
86-
print.warnln(format!("Failed to save self outdated check data: {}", e));
78+
println!("Failed to save self outdated check data: {e}");
8779
}
8880
}
8981

9082
let current_version = Version::parse(current_version).unwrap();
9183
let latest_version = get_latest_version(&current_version, &stats);
9284

9385
if *latest_version > current_version {
94-
print.warnln(format!(
86+
println!(
9587
"A new release of stellar-cli is available: {current_version} -> {latest_version}",
96-
));
88+
);
9789
}
9890
}
9991

100-
fn get_latest_version<'a>(current_version: &Version, stats: &'a SelfOutdatedCheck) -> &'a Version {
92+
fn get_latest_version<'a>(current_version: &Version, stats: &'a UpgradeCheck) -> &'a Version {
10193
if current_version.pre.is_empty() {
10294
// If we are currently using a non-preview version
10395
&stats.max_stable_version
@@ -123,7 +115,7 @@ mod tests {
123115

124116
#[test]
125117
fn test_get_latest_version() {
126-
let stats = SelfOutdatedCheck {
118+
let stats = UpgradeCheck {
127119
latest_check_time: 0,
128120
max_stable_version: Version::parse("1.0.0").unwrap(),
129121
max_version: Version::parse("1.1.0-rc.1").unwrap(),

0 commit comments

Comments
 (0)