diff --git a/clients/client-core/src/init/mod.rs b/clients/client-core/src/init/mod.rs index 6e1700d7ae..facb54bf73 100644 --- a/clients/client-core/src/init/mod.rs +++ b/clients/client-core/src/init/mod.rs @@ -112,14 +112,14 @@ where { let id = config.get_id(); - // If we are not going to register gateway, and an explicitly chosed gateway is not passed in, + // If we are not going to register gateway, and an explicitly chosen gateway is not passed in, // load the existing configuration file if !register_gateway && user_chosen_gateway_id.is_none() { println!("Not registering gateway, will reuse existing config and keys"); return load_existing_gateway_config::(&id); } - // Else, we preceed by querying the nym-api + // Else, we proceed by querying the nym-api let gateway = helpers::query_gateway_details( config.get_nym_api_endpoints(), user_chosen_gateway_id, diff --git a/clients/native/src/commands/init.rs b/clients/native/src/commands/init.rs index c41c4796a0..95ba859f80 100644 --- a/clients/native/src/commands/init.rs +++ b/clients/native/src/commands/init.rs @@ -1,6 +1,7 @@ // Copyright 2021 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +use crate::commands::try_upgrade_v1_1_13_config; use crate::{ client::config::Config, commands::{override_config, OverrideConfig}, @@ -121,6 +122,9 @@ pub(crate) async fn execute(args: &Init) -> Result<(), ClientError> { let already_init = Config::default_config_file_path(id).exists(); if already_init { + // in case we're using old config, try to upgrade it + // (if we're using the current version, it's a no-op) + try_upgrade_v1_1_13_config(id)?; println!("Client \"{id}\" was already initialised before"); } diff --git a/clients/native/src/commands/mod.rs b/clients/native/src/commands/mod.rs index b8c05c4cc7..b2227a2795 100644 --- a/clients/native/src/commands/mod.rs +++ b/clients/native/src/commands/mod.rs @@ -1,13 +1,15 @@ // Copyright 2021-2023 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +use crate::client::config::old_config_v1_1_13::OldConfigV1_1_13; use crate::client::config::{BaseConfig, Config}; use clap::CommandFactory; use clap::{Parser, Subcommand}; use lazy_static::lazy_static; +use log::info; use nym_bin_common::build_information::BinaryBuildInformation; use nym_bin_common::completions::{fig_generate, ArgShell}; -use nym_config::OptionalSet; +use nym_config::{NymConfig, OptionalSet}; use std::error::Error; use std::net::IpAddr; @@ -102,6 +104,20 @@ pub(crate) fn override_config(config: Config, args: OverrideConfig) -> Config { ) } +fn try_upgrade_v1_1_13_config(id: &str) -> std::io::Result<()> { + // explicitly load it as v1.1.13 (which is incompatible with the current, i.e. 1.1.14+) + let Ok(old_config) = OldConfigV1_1_13::load_from_file(id) else { + // if we failed to load it, there might have been nothing to upgrade + // or maybe it was an even older file. in either way. just ignore it and carry on with our day + return Ok(()); + }; + info!("It seems the client is using <= v1.1.13 config template."); + info!("It is going to get updated to the current specification."); + + let updated: Config = old_config.into(); + updated.save_to_file(None) +} + #[cfg(test)] mod tests { use super::*; diff --git a/clients/native/src/commands/run.rs b/clients/native/src/commands/run.rs index 631131f66a..ef7a488289 100644 --- a/clients/native/src/commands/run.rs +++ b/clients/native/src/commands/run.rs @@ -4,13 +4,12 @@ use std::error::Error; use std::net::IpAddr; +use crate::commands::try_upgrade_v1_1_13_config; use crate::{ client::{config::Config, SocketClient}, commands::{override_config, OverrideConfig}, error::ClientError, }; - -use crate::client::config::old_config_v1_1_13::OldConfigV1_1_13; use clap::Args; use log::*; use nym_bin_common::version_checker::is_minor_version_compatible; @@ -98,38 +97,20 @@ fn version_check(cfg: &Config) -> bool { } } -fn load_config(id: &str) -> Result> { - // try to load the current config - match Config::load_from_file(id) { - Ok(config) => Ok(config), - Err(err) => { - warn!("Failed to load config for {id} - {err}...\nAttempting to use the old config template..."); - - // if this failed, try to use the old template - let old_config = match OldConfigV1_1_13::load_from_file(id) { - Ok(cfg) => { - info!("Managed to load config for {id} using old template"); - cfg - } - Err(err) => { - error!("Failed to load config for {id} with the old template. Are you sure you have run `init` before? (Error was: {err})"); - return Err(Box::new(ClientError::FailedToLoadConfig(id.to_string()))); - } - }; - - info!("Updating the client config template..."); - let updated: Config = old_config.into(); - updated.save_to_file(None)?; - - Ok(updated) - } - } -} - pub(crate) async fn execute(args: &Run) -> Result<(), Box> { let id = &args.id; - let mut config = load_config(id)?; + // in case we're using old config, try to upgrade it + // (if we're using the current version, it's a no-op) + try_upgrade_v1_1_13_config(id)?; + + let mut config = match Config::load_from_file(id) { + Ok(cfg) => cfg, + Err(err) => { + error!("Failed to load config for {}. Are you sure you have run `init` before? (Error was: {err})", id); + return Err(Box::new(ClientError::FailedToLoadConfig(id.to_string()))); + } + }; let override_config_fields = OverrideConfig::from(args.clone()); config = override_config(config, override_config_fields); diff --git a/clients/socks5/src/commands/init.rs b/clients/socks5/src/commands/init.rs index c921834ec3..59f87afe95 100644 --- a/clients/socks5/src/commands/init.rs +++ b/clients/socks5/src/commands/init.rs @@ -1,6 +1,7 @@ // Copyright 2021 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +use crate::commands::try_upgrade_v1_1_13_config; use crate::{ client::config::Config, commands::{override_config, OverrideConfig}, @@ -124,6 +125,9 @@ pub(crate) async fn execute(args: &Init) -> Result<(), Socks5ClientError> { let already_init = Config::default_config_file_path(id).exists(); if already_init { + // in case we're using old config, try to upgrade it + // (if we're using the current version, it's a no-op) + try_upgrade_v1_1_13_config(id)?; println!("SOCKS5 client \"{id}\" was already initialised before"); } diff --git a/clients/socks5/src/commands/mod.rs b/clients/socks5/src/commands/mod.rs index 9e5b903de6..4de1925400 100644 --- a/clients/socks5/src/commands/mod.rs +++ b/clients/socks5/src/commands/mod.rs @@ -1,13 +1,15 @@ // Copyright 2021-2023 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +use crate::client::config::old_config_v1_1_13::OldConfigV1_1_13; use crate::client::config::{BaseConfig, Config}; use clap::CommandFactory; use clap::{Parser, Subcommand}; use lazy_static::lazy_static; +use log::info; use nym_bin_common::build_information::BinaryBuildInformation; use nym_bin_common::completions::{fig_generate, ArgShell}; -use nym_config::OptionalSet; +use nym_config::{NymConfig, OptionalSet}; use std::error::Error; pub mod init; @@ -101,6 +103,20 @@ pub(crate) fn override_config(config: Config, args: OverrideConfig) -> Config { ) } +fn try_upgrade_v1_1_13_config(id: &str) -> std::io::Result<()> { + // explicitly load it as v1.1.13 (which is incompatible with the current, i.e. 1.1.14+) + let Ok(old_config) = OldConfigV1_1_13::load_from_file(id) else { + // if we failed to load it, there might have been nothing to upgrade + // or maybe it was an even older file. in either way. just ignore it and carry on with our day + return Ok(()); + }; + info!("It seems the client is using <= v1.1.13 config template."); + info!("It is going to get updated to the current specification."); + + let updated: Config = old_config.into(); + updated.save_to_file(None) +} + #[cfg(test)] mod tests { use super::*; diff --git a/clients/socks5/src/commands/run.rs b/clients/socks5/src/commands/run.rs index a995fe6f9c..227154badc 100644 --- a/clients/socks5/src/commands/run.rs +++ b/clients/socks5/src/commands/run.rs @@ -1,13 +1,12 @@ // Copyright 2021 - Nym Technologies SA // SPDX-License-Identifier: Apache-2.0 +use crate::commands::try_upgrade_v1_1_13_config; use crate::{ client::{config::Config, NymClient}, commands::{override_config, OverrideConfig}, error::Socks5ClientError, }; - -use crate::client::config::old_config_v1_1_13::OldConfigV1_1_13; use clap::Args; use log::*; use nym_bin_common::version_checker::is_minor_version_compatible; @@ -106,40 +105,22 @@ fn version_check(cfg: &Config) -> bool { } } -fn load_config(id: &str) -> Result> { - // try to load the current config - match Config::load_from_file(id) { - Ok(config) => Ok(config), - Err(err) => { - warn!("Failed to load config for {id} - {err}...\nAttempting to use the old config template..."); - - // if this failed, try to use the old template - let old_config = match OldConfigV1_1_13::load_from_file(id) { - Ok(cfg) => { - info!("Managed to load config for {id} using old template"); - cfg - } - Err(err) => { - error!("Failed to load config for {id} with the old template. Are you sure you have run `init` before? (Error was: {err})"); - return Err(Box::new(Socks5ClientError::FailedToLoadConfig( - id.to_string(), - ))); - } - }; - - info!("Updating the client config template..."); - let updated: Config = old_config.into(); - updated.save_to_file(None)?; - - Ok(updated) - } - } -} - pub(crate) async fn execute(args: &Run) -> Result<(), Box> { let id = &args.id; - let mut config = load_config(id)?; + // in case we're using old config, try to upgrade it + // (if we're using the current version, it's a no-op) + try_upgrade_v1_1_13_config(id)?; + + let mut config = match Config::load_from_file(id) { + Ok(cfg) => cfg, + Err(err) => { + error!("Failed to load config for {}. Are you sure you have run `init` before? (Error was: {err})", id); + return Err(Box::new(Socks5ClientError::FailedToLoadConfig( + id.to_string(), + ))); + } + }; let override_config_fields = OverrideConfig::from(args.clone()); config = override_config(config, override_config_fields);