diff --git a/config/config_remote.toml b/config/config_remote.toml index fb5c05c..58d0e40 100644 --- a/config/config_remote.toml +++ b/config/config_remote.toml @@ -2,7 +2,6 @@ version = "0.0.1" [update] -last_cvm_version = "0.0.1" version_pattern = "v{version}" name_pattern = "cvm-{arch}.tar.gz" file_name = "cvm.tar.gz" diff --git a/src/config/remote_config.rs b/src/config/remote_config.rs index 401f072..c779492 100644 --- a/src/config/remote_config.rs +++ b/src/config/remote_config.rs @@ -17,7 +17,7 @@ pub fn get_remote_config() -> Result { Ok(parse_file) } -#[derive(Deserialize, Debug, Clone)] +#[derive(Deserialize, Debug, Clone, Default)] pub struct RemoteConfig { pub general: General, pub update: Update, @@ -30,18 +30,18 @@ pub struct RemoteConfig { pub services_item: Vec, } -#[derive(Deserialize, Debug, Clone)] +#[derive(Deserialize, Debug, Clone, Default)] pub struct Binaries { pub required_files: Vec, pub others_files: Vec, } -#[derive(Deserialize, Debug, Clone)] +#[derive(Deserialize, Debug, Clone, Default)] pub struct General { pub version: String, } -#[derive(Deserialize, Debug, Clone)] +#[derive(Deserialize, Debug, Clone, Default)] pub struct Init { pub git_assets: String, pub ghcup_url: String, @@ -62,7 +62,7 @@ pub struct Init { pub haskell_cabal_version: String, } -#[derive(Deserialize, Debug, Clone)] +#[derive(Deserialize, Debug, Clone, Default)] pub struct ConfigFileItem { pub url: String, pub name: String, @@ -70,7 +70,7 @@ pub struct ConfigFileItem { pub pattern_sed: String, } -#[derive(Deserialize, Debug, Clone)] +#[derive(Deserialize, Debug, Clone, Default)] pub struct Dependencies { pub macos: Vec, pub debian: Vec, @@ -84,7 +84,7 @@ pub struct Dependencies { pub rhel_8: Vec, } -#[derive(Deserialize, Debug, Clone)] +#[derive(Deserialize, Debug, Clone, Default)] pub struct BuildCardanoNode { pub cnode_repository: String, pub cnode_release: String, @@ -93,22 +93,21 @@ pub struct BuildCardanoNode { pub cnode_ported_libsodium_data: String, } -#[derive(Deserialize, Debug, Clone)] +#[derive(Deserialize, Debug, Clone, Default)] pub struct StructureFolderItem { pub key: String, pub name: String, pub parent: String, } -#[derive(Deserialize, Debug, Clone)] +#[derive(Deserialize, Debug, Clone, Default)] pub struct Update { - pub last_cvm_version: String, pub version_pattern: String, pub name_pattern: String, pub file_name: String, } -#[derive(Deserialize, Debug, Clone)] +#[derive(Deserialize, Debug, Clone, Default)] pub struct Services { pub url: String, pub file: String, diff --git a/src/main.rs b/src/main.rs index e44f1fd..fb6a43f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,15 +1,14 @@ extern crate core; -use std::io::stdout; +use crossterm::style::Stylize; use message::message::{MessageData, Message}; use subcommands::subcommands_impl; -use crate::subcommands::subcommand::Command; +use crate::subcommands::subcommand_strategy::CommandStrategy; use subcommands::subcommands_impl::init::Init; use subcommands::subcommands_impl::install::Install; use subcommands::subcommands_impl::list::List; use subcommands::subcommands_impl::start::Start; use subcommands::subcommands_impl::stop::Stop; -use crate::message::message::MessageKind; use crate::subcommands_impl::clean::Clean; use crate::subcommands_impl::config::Config; use crate::subcommands_impl::r#use::Use; @@ -18,6 +17,7 @@ use crate::subcommands_impl::update::Update; use crate::task::task::Success; use crate::term::term::Term; use crate::utils::url_build::url_build; +use crate::utils::version_utils::get_last_cvm_version; mod task; mod config; @@ -31,50 +31,39 @@ mod term; const VERSION: &str = env!("CARGO_PKG_VERSION"); fn main() { - let config = config::remote_config::get_remote_config(); - - if let Err(error) = &config { - error.print(); - return; - } - let config = config.unwrap(); - - let mut term = Term { stdout: stdout() }; - - let _ =show_update_alert(&config.update.last_cvm_version, &VERSION.to_string()); + let _ = show_update_alert(VERSION.to_string()); let args = subcommands::commands_config::command_config(); - let result = match args.subcommand() { Some(("init", matches)) => { - Init::start(matches, &config, &mut term) + Init::start(matches) } Some(("install", matches)) => { - Install::start(matches, &config, &mut term) + Install::start(matches) } Some(("use", matches)) => { - Use::start(matches, &config, &mut term) + Use::start(matches) } Some(("remove", matches)) => { - Remove::start(matches, &config, &mut term) + Remove::start(matches) } Some(("clean", matches)) => { - Clean::start(matches, &config, &mut term) + Clean::start(matches) } Some(("list", matches)) => { - List::start(matches, &config, &mut term) + List::start(matches) } Some(("update", matches)) => { - Update::start(matches, &config, &mut term) + Update::start(matches) } Some(("start", matches)) => { - Start::start(matches, &config, &mut term) + Start::start(matches) } Some(("stop", matches)) => { - Stop::start(matches, &config, &mut term) + Stop::start(matches) } Some(("config", matches)) => { - Config::start(matches, &config, &mut term) + Config::start(matches) } _ => { error_not_found() } }; @@ -96,13 +85,11 @@ fn error_not_found() -> Result { })); } -fn show_update_alert(last_version: &String, current_version: &String) -> Result { +fn show_update_alert(current_version: String) { + let last_version = get_last_cvm_version().unwrap_or("".to_string()); + let last_version = last_version.replace("v", ""); if &last_version > ¤t_version { - return Err(Message::CommandNotFound(MessageData { - message: format!("{} {} => {}", "New update available", ¤t_version, &last_version), - kind: MessageKind::Info, - ..Default::default() - })); + print!("{}\n {}\n", format!("{} {} => {}", "New update available".yellow(), ¤t_version.blue(), + &last_version.yellow()), "To actualize run [cvm update]".green()); }; - Ok(Success{}) } \ No newline at end of file diff --git a/src/subcommands/mod.rs b/src/subcommands/mod.rs index 13351b0..3e425fb 100644 --- a/src/subcommands/mod.rs +++ b/src/subcommands/mod.rs @@ -1,3 +1,3 @@ pub mod commands_config; -pub mod subcommand; +pub mod subcommand_strategy; pub mod subcommands_impl; \ No newline at end of file diff --git a/src/subcommands/subcommand.rs b/src/subcommands/subcommand.rs deleted file mode 100644 index 4c84bb1..0000000 --- a/src/subcommands/subcommand.rs +++ /dev/null @@ -1,9 +0,0 @@ -#![allow(dead_code, unused_variables)] - -use clap::ArgMatches; -use crate::config::remote_config::RemoteConfig; -use crate::{Message, Success, Term}; - -pub trait Command { - fn start(command: &ArgMatches, config: &RemoteConfig, term: &mut Term) -> Result; -} \ No newline at end of file diff --git a/src/subcommands/subcommand_strategy.rs b/src/subcommands/subcommand_strategy.rs new file mode 100644 index 0000000..a8e1703 --- /dev/null +++ b/src/subcommands/subcommand_strategy.rs @@ -0,0 +1,8 @@ +#![allow(dead_code, unused_variables)] + +use clap::ArgMatches; +use crate::{Message, Success}; + +pub trait CommandStrategy { + fn start(command: &ArgMatches) -> Result; +} \ No newline at end of file diff --git a/src/subcommands/subcommands_impl/clean.rs b/src/subcommands/subcommands_impl/clean.rs index 6fc0268..e0bd947 100644 --- a/src/subcommands/subcommands_impl/clean.rs +++ b/src/subcommands/subcommands_impl/clean.rs @@ -1,8 +1,8 @@ #![allow(dead_code, unused_variables)] +use std::io::stdout; use clap::{ArgMatches}; -use crate::{Command, Message, Success, Term}; -use crate::config::remote_config::RemoteConfig; +use crate::{CommandStrategy, config, Message, Success, Term}; use crate::task::task_impl::commons::folder_manager_task::{FolderManagerAction, FolderManagerTask}; use crate::task::task_impl::commons::run_command_task::{Cmd, RunCommandInputData, RunCommandTask}; use crate::task_manager::task_manager::TaskManager; @@ -11,8 +11,11 @@ use crate::utils::folders::Folder; pub struct Clean{} -impl Command for Clean{ - fn start(command: &ArgMatches, config: &RemoteConfig, term: &mut Term) -> Result { +impl CommandStrategy for Clean{ + fn start(command: &ArgMatches) -> Result { + + let config = config::remote_config::get_remote_config()?; + let mut term = Term { stdout: stdout() }; let home_dir = Folder::get_home_dir()?; let mut ghcup_dir = String::from(home_dir); @@ -23,7 +26,7 @@ impl Command for Clean{ TaskManager::default().start(vec![ Box::new(RunCommandTask { input_data: build_cabal_clean_command(ghcup_dir), command_description: "Cleaning cabal cache, package and build temporal files".to_string() }), Box::new(FolderManagerTask { input_data: FolderManagerAction::Clean(vec![git_folder]) }), - ], config, term, L1) + ], &config, &mut term, L1) } } diff --git a/src/subcommands/subcommands_impl/config.rs b/src/subcommands/subcommands_impl/config.rs index 49a4620..ad4a933 100644 --- a/src/subcommands/subcommands_impl/config.rs +++ b/src/subcommands/subcommands_impl/config.rs @@ -1,27 +1,32 @@ #![allow(dead_code, unused_variables)] +use std::io::stdout; use clap::ArgMatches; -use crate::subcommands::subcommand::Command; +use crate::subcommands::subcommand_strategy::CommandStrategy; use crate::subcommands::commands_config::Args; use crate::config::remote_config::RemoteConfig; use crate::message::message::Message; use crate::task::task::Success; use crate::task_manager::task_manager::TaskManager; -use crate::Term; +use crate::{config, Term}; use crate::task::task_impl::config::update_config_files_task::UpdateConfigFilesTask; use crate::term::log_level::LogLevel::L1; pub struct Config {} -impl Command for Config { - fn start<'a>(command: &'a ArgMatches, config: &RemoteConfig, term: &mut Term) -> Result { +impl CommandStrategy for Config { + fn start(command: &ArgMatches) -> Result { + + let config = config::remote_config::get_remote_config()?; + let mut term = Term { stdout: stdout() }; + let default_value = "".to_string(); match command.subcommand() { Some(("update", matches)) => { let force_arg = matches.contains_id(Args::FORCE._to_string()); - update_config(force_arg, config, term) + update_config(force_arg, &config, &mut term) } _ => { Ok(Success {}) } } diff --git a/src/subcommands/subcommands_impl/init.rs b/src/subcommands/subcommands_impl/init.rs index 19efc14..e75157d 100644 --- a/src/subcommands/subcommands_impl/init.rs +++ b/src/subcommands/subcommands_impl/init.rs @@ -1,9 +1,9 @@ #![allow(dead_code, unused_variables)] +use std::io::stdout; use clap::ArgMatches; -use crate::subcommands::subcommand::Command; +use crate::subcommands::subcommand_strategy::CommandStrategy; use crate::subcommands::commands_config::Args; -use crate::config::remote_config::RemoteConfig; use crate::config::state_config::{reset_init, set_init_network, set_init_success}; use crate::message::message::Message; use crate::task::task::Success; @@ -14,7 +14,7 @@ use crate::task::task_impl::init::install_dependences_task::InstallDependenciesT use crate::task::task_impl::init::install_ghcup_task::InstallHanskellGhcTask; use crate::task::task_impl::init::install_libsecp256k1_task::Installlibsecp256k1Task; use crate::task_manager::task_manager::TaskManager; -use crate::Term; +use crate::{config, Term}; use crate::term::log_level::LogLevel::L1; use crate::utils::folders::Folder; @@ -23,8 +23,11 @@ const TESTNET: &str = "testnet"; pub struct Init{} -impl Command for Init { - fn start<'a>(command: &'a ArgMatches, config: &RemoteConfig, term: &mut Term) -> Result { +impl CommandStrategy for Init { + fn start(command: &ArgMatches) -> Result { + + let config = config::remote_config::get_remote_config()?; + let mut term = Term { stdout: stdout() }; let mut network = MAINNET; @@ -45,7 +48,7 @@ impl Command for Init { Box::new(CreateFolderStructure::default()), Box::new(Installlibsecp256k1Task::default()), Box::new(DownloadConfigFilesTask { network: network.to_string() }), - ], config, term, L1)?; + ], &config, &mut term, L1)?; set_init_network(network.to_string())?; set_init_success(true) diff --git a/src/subcommands/subcommands_impl/install.rs b/src/subcommands/subcommands_impl/install.rs index 2cc19a6..73fdb6d 100644 --- a/src/subcommands/subcommands_impl/install.rs +++ b/src/subcommands/subcommands_impl/install.rs @@ -1,12 +1,12 @@ #![allow(dead_code, unused_variables)] +use std::io::stdout; use std::path::Path; use clap::ArgMatches; use crate::subcommands::commands_config::{Args}; use crate::task::task::Success; use crate::utils::version_utils::{get_last_tag, LATEST, verify_version}; -use crate::config::remote_config::RemoteConfig; -use crate::{Message, Command, Term, MessageData, url_build}; +use crate::{Message, CommandStrategy, Term, MessageData, url_build, config}; use crate::config::state_config::get_state; use crate::message::message::MessageKind; use crate::task::task_impl::commons::folder_manager_task::{FolderManagerAction, FolderManagerTask}; @@ -18,8 +18,12 @@ use crate::utils::folders::Folder; pub struct Install {} -impl Command for Install { - fn start(command: &ArgMatches, config: &RemoteConfig, term: &mut Term) -> Result { +impl CommandStrategy for Install { + fn start(command: &ArgMatches) -> Result { + + let config = config::remote_config::get_remote_config()?; + let mut term = Term { stdout: stdout() }; + let version_arg = command.get_one::(Args::VERSION._to_string()).unwrap(); let mut version = verify_version(version_arg.as_str())?.to_string(); @@ -61,6 +65,6 @@ impl Command for Install { Box::new(FolderManagerTask { input_data: FolderManagerAction::Create(vec![(bin_folder.clone(), version.clone())]) }), Box::new(CopyBinTask { input_data: CopyBinInputData { files_names: config.binaries.required_files.clone(), origin_path: cardano_folder.clone(), version: version.clone(), bin_folder: bin_folder.clone(), version_folder: version_folder.clone() } }), - ], config, term, L1) + ], &config, &mut term, L1) } } \ No newline at end of file diff --git a/src/subcommands/subcommands_impl/list.rs b/src/subcommands/subcommands_impl/list.rs index 9ae7791..450b1a3 100644 --- a/src/subcommands/subcommands_impl/list.rs +++ b/src/subcommands/subcommands_impl/list.rs @@ -4,16 +4,17 @@ use clap::ArgMatches; use crossterm::style::Stylize; use fs_extra::dir::get_size; use walkdir::WalkDir; -use crate::{Command, Message, Success, Term}; +use crate::{CommandStrategy, config, Message, Success}; use crate::utils::version_utils::verify_version; -use crate::config::remote_config::RemoteConfig; use crate::config::state_config::get_state; use crate::utils::folders::Folder; pub struct List{} -impl Command for List{ - fn start(command: &ArgMatches, config: &RemoteConfig, term: &mut Term) -> Result { +impl CommandStrategy for List{ + fn start(command: &ArgMatches) -> Result { + + let config = config::remote_config::get_remote_config()?; let bin_folder = Folder::get_path(Folder::BIN, &config); let current_folder = Folder::get_path(Folder::CURRENT, &config); diff --git a/src/subcommands/subcommands_impl/remove.rs b/src/subcommands/subcommands_impl/remove.rs index 9b96527..88a7256 100644 --- a/src/subcommands/subcommands_impl/remove.rs +++ b/src/subcommands/subcommands_impl/remove.rs @@ -1,11 +1,11 @@ #![allow(dead_code, unused_variables)] +use std::io::stdout; use std::path::Path; -use clap::{ArgMatches}; -use crate::{Command, Message, Success, Term, url_build}; +use clap::ArgMatches; +use crate::{CommandStrategy, config, Message, Success, Term, url_build}; use crate::subcommands::commands_config::Args; use crate::utils::version_utils::{get_last_tag, LATEST, read_version, verify_version}; -use crate::config::remote_config::RemoteConfig; use crate::task::task::Task; use crate::task::task_impl::commons::folder_manager_task::{FolderManagerAction, FolderManagerTask}; use crate::task::task_impl::r#use::service_manager_task::{ServicesAction, ServicesManagerTask}; @@ -15,8 +15,11 @@ use crate::utils::folders::Folder; pub struct Remove {} -impl Command for Remove { - fn start(command: &ArgMatches, config: &RemoteConfig, term: &mut Term) -> Result { +impl CommandStrategy for Remove { + fn start(command: &ArgMatches) -> Result { + + let config = config::remote_config::get_remote_config()?; + let mut term = Term { stdout: stdout() }; let version_arg = command.get_one::(Args::VERSION._to_string()).unwrap(); let mut version = verify_version(version_arg.as_str())?.to_string(); @@ -46,6 +49,6 @@ impl Command for Remove { } task_queue.reverse(); - TaskManager::default().start(task_queue, config, term, L1) + TaskManager::default().start(task_queue, &config, &mut term, L1) } } \ No newline at end of file diff --git a/src/subcommands/subcommands_impl/start.rs b/src/subcommands/subcommands_impl/start.rs index 9bd0032..69c17fc 100644 --- a/src/subcommands/subcommands_impl/start.rs +++ b/src/subcommands/subcommands_impl/start.rs @@ -1,20 +1,23 @@ #![allow(dead_code, unused_variables)] +use std::io::stdout; use clap::{ArgMatches}; -use crate::{Command, Message, Success, Term}; -use crate::config::remote_config::RemoteConfig; +use crate::{CommandStrategy, config, Message, Success, Term}; use crate::task::task_impl::r#use::service_manager_task::{ServicesAction, ServicesManagerTask}; use crate::task_manager::task_manager::TaskManager; use crate::term::log_level::LogLevel::L1; pub struct Start{} -impl Command for Start{ - fn start(command: &ArgMatches, config: &RemoteConfig, term: &mut Term) -> Result { +impl CommandStrategy for Start{ + fn start(command: &ArgMatches) -> Result { + + let config = config::remote_config::get_remote_config()?; + let mut term = Term { stdout: stdout() }; sudo::escalate_if_needed().expect("Super user permissions are required"); TaskManager::default().start(vec![ Box::new(ServicesManagerTask { input_data: ServicesAction::START }), - ], config, term, L1) + ], &config, &mut term, L1) } } \ No newline at end of file diff --git a/src/subcommands/subcommands_impl/stop.rs b/src/subcommands/subcommands_impl/stop.rs index 50ad7b6..4e1b2b7 100644 --- a/src/subcommands/subcommands_impl/stop.rs +++ b/src/subcommands/subcommands_impl/stop.rs @@ -1,21 +1,24 @@ #![allow(dead_code, unused_variables)] +use std::io::stdout; use clap::{ArgMatches}; -use crate::{Command, Message, Success, Term}; -use crate::config::remote_config::RemoteConfig; +use crate::{CommandStrategy, config, Message, Success, Term}; use crate::task::task_impl::r#use::service_manager_task::{ServicesAction, ServicesManagerTask}; use crate::task_manager::task_manager::TaskManager; use crate::term::log_level::LogLevel::L1; pub struct Stop{} -impl Command for Stop{ - fn start(command: &ArgMatches, config: &RemoteConfig, term: &mut Term) -> Result { +impl CommandStrategy for Stop{ + fn start(command: &ArgMatches) -> Result { + + let config = config::remote_config::get_remote_config()?; + let mut term = Term { stdout: stdout() }; sudo::escalate_if_needed().expect("Super user permissions are required"); TaskManager::default().start(vec![ Box::new(ServicesManagerTask { input_data: ServicesAction::STOP }), - ], config, term, L1) + ], &config, &mut term, L1) } } \ No newline at end of file diff --git a/src/subcommands/subcommands_impl/update.rs b/src/subcommands/subcommands_impl/update.rs index 9da6c76..79c96b3 100644 --- a/src/subcommands/subcommands_impl/update.rs +++ b/src/subcommands/subcommands_impl/update.rs @@ -1,8 +1,9 @@ #![allow(dead_code, unused_variables)] +use std::io::stdout; use clap::ArgMatches; -use crate::{Command, Message, Success, Term}; -use crate::config::remote_config::{RemoteConfig}; +use crate::{CommandStrategy, Message, Success, Term}; +use crate::config::remote_config::RemoteConfig; use crate::task::task_impl::update::check_update_task::{CheckUpdateData, CheckUpdateTask}; use crate::task_manager::task_manager::TaskManager; use crate::term::log_level::LogLevel::L1; @@ -11,11 +12,13 @@ const VERSION: &str = env!("CARGO_PKG_VERSION"); pub struct Update{} -impl Command for Update{ - fn start(command: &ArgMatches, config: &RemoteConfig, term: &mut Term) -> Result { +impl CommandStrategy for Update{ + fn start(command: &ArgMatches) -> Result { + + let mut term = Term { stdout: stdout() }; TaskManager::default().start(vec![ Box::new(CheckUpdateTask { input_data: CheckUpdateData { version: VERSION.to_string() } }), - ], config, term, L1) + ], &RemoteConfig::default(), &mut term, L1) } } \ No newline at end of file diff --git a/src/subcommands/subcommands_impl/use.rs b/src/subcommands/subcommands_impl/use.rs index a45bcbf..20f4fce 100644 --- a/src/subcommands/subcommands_impl/use.rs +++ b/src/subcommands/subcommands_impl/use.rs @@ -1,10 +1,10 @@ #![allow(dead_code, unused_variables)] +use std::io::stdout; use clap::{ArgMatches}; -use crate::{Command, Message, Success, Term}; +use crate::{CommandStrategy, config, Message, Success, Term}; use crate::subcommands::commands_config::Args; use crate::utils::version_utils::{get_last_tag, LATEST, verify_version}; -use crate::config::remote_config::RemoteConfig; use crate::task::task_impl::r#use::deploy_system_task::DeploySystemTask; use crate::task::task_impl::r#use::service_manager_task::{ServicesAction, ServicesManagerTask}; use crate::task::task_impl::r#use::use_version_task::{UserVersionData, UserVersionTask}; @@ -13,8 +13,11 @@ use crate::term::log_level::LogLevel::L1; pub struct Use{} -impl Command for Use{ - fn start(command: &ArgMatches, config: &RemoteConfig, term: &mut Term) -> Result { +impl CommandStrategy for Use{ + fn start(command: &ArgMatches) -> Result { + + let config = config::remote_config::get_remote_config()?; + let mut term = Term { stdout: stdout() }; let version_arg = command.get_one::(Args::VERSION._to_string()).unwrap(); let mut version = verify_version(version_arg.as_str())?.to_string(); @@ -31,6 +34,6 @@ impl Command for Use{ Box::new(ServicesManagerTask { input_data: ServicesAction::STOP }), Box::new(UserVersionTask { input_data: UserVersionData { version }}), Box::new(DeploySystemTask { }), - ], config, term, L1) + ], &config, &mut term, L1) } } \ No newline at end of file diff --git a/src/task/task_impl/update/check_update_task.rs b/src/task/task_impl/update/check_update_task.rs index db68994..fa9a781 100644 --- a/src/task/task_impl/update/check_update_task.rs +++ b/src/task/task_impl/update/check_update_task.rs @@ -8,11 +8,12 @@ use crate::env::Env; use strfmt::strfmt; use crate::{Success, Term}; use crate::config::remote_config::{RemoteConfig, Update}; -use crate::message::message::{Message, MessageData}; +use crate::message::message::{Message, MessageData, MessageKind}; use crate::task::task::Task; use crate::task::task_type::TaskType; use crate::utils::download_manager::download; use crate::utils::folders::Folder; +use crate::utils::version_utils::get_last_cvm_version; pub struct CheckUpdateTask { pub input_data: CheckUpdateData, @@ -30,15 +31,18 @@ impl Task for CheckUpdateTask { } fn run(self: &Self, _env: &mut Env, config: &RemoteConfig, term: &mut Term) -> Result { - if &config.update.last_cvm_version <= &self.input_data.version { + let last_version = get_last_cvm_version()?; + let last_version = last_version.replace("v", ""); + if &last_version <= &self.input_data.version { return Err(Message::AlreadyLastUpdate(MessageData { message: "You already have the latest version".to_string(), task: self.get_type(), + kind: MessageKind::Info, ..Default::default() })); }; - download_and_copy_version(&config.update.last_cvm_version, &config.init.git_assets, &config.update) + download_and_copy_version(&last_version, &config.init.git_assets, &config.update) } fn check(self: &Self, _env: &mut Env, config: &RemoteConfig, term: &mut Term) -> Result { diff --git a/src/utils/version_utils.rs b/src/utils/version_utils.rs index 55e3a31..057b7a8 100644 --- a/src/utils/version_utils.rs +++ b/src/utils/version_utils.rs @@ -11,6 +11,7 @@ use crate::url_build; pub const LATEST: &str = "latest"; const USER_AGENT: &str = "cvm"; const VERSION_FILE: &str = "version"; +const CVM_RELEASES: &str = "https://api.github.com/repos/orelvis15/cvm/releases/latest"; pub fn verify_version(version: &str) -> Result<&str, Message> { let regex = Regex::new(r"^(\d+\.)?(\d+\.)?(\*|\d+)$").unwrap(); @@ -29,14 +30,30 @@ pub fn get_last_tag(url: &String) -> Result { let client = reqwest::blocking::Client::builder().user_agent(USER_AGENT).build(); if let Ok(client) = client { if let Ok(response) = client.get(url).send() { - if let Ok(test) = response.text() { - let tag: Tag = serde_json::from_str(test.as_str()).unwrap(); + if let Ok(text) = response.text() { + let tag: Tag = serde_json::from_str(text.as_str()).unwrap(); return Ok(tag.tag_name); } } } return Err(Message::CheckCardanoVersion(MessageData { - message: "Error checking latest cardano node tag".to_string(), + message: "Error checking latest cardano node release".to_string(), + ..Default::default() + })); +} + +pub fn get_last_cvm_version() -> Result { + let client = reqwest::blocking::Client::builder().user_agent(USER_AGENT).build(); + if let Ok(client) = client { + if let Ok(response) = client.get(CVM_RELEASES).send() { + if let Ok(text) = response.text() { + let tag: Tag = serde_json::from_str(text.as_str()).unwrap(); + return Ok(tag.tag_name); + } + } + } + return Err(Message::CheckCardanoVersion(MessageData { + message: "Error checking latest cvm release".to_string(), ..Default::default() })); }