diff --git a/src/commands/check_updates.rs b/src/commands/check_updates.rs index 704376821..bed1f503d 100644 --- a/src/commands/check_updates.rs +++ b/src/commands/check_updates.rs @@ -1,4 +1,4 @@ -use crate::check_update; +use crate::util::check_update::check_update; use super::*; use serde_json::json; @@ -23,7 +23,7 @@ pub async fn command(_args: Args, json: bool) -> Result<()> { return Ok(()); } - let is_latest = check_update!(configs, true); + let is_latest = check_update(&mut configs, true).await?; if is_latest { println!( "You are on the latest version of the CLI, v{}", diff --git a/src/commands/init.rs b/src/commands/init.rs index 10e05f003..0b1686a2f 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -1,6 +1,6 @@ use std::fmt::Display; -use crate::{check_update, util::prompt::prompt_select}; +use crate::util::{check_update::check_update_command, prompt::prompt_select}; use super::{queries::user_projects::UserProjectsMeTeamsEdgesNode, *}; @@ -16,7 +16,7 @@ pub struct Args { pub async fn command(args: Args, _json: bool) -> Result<()> { let mut configs = Configs::new()?; - check_update!(configs); + check_update_command(&mut configs).await?; let client = GQLClient::new_authorized(&configs)?; diff --git a/src/commands/link.rs b/src/commands/link.rs index 051fed9bf..5c2ec265b 100644 --- a/src/commands/link.rs +++ b/src/commands/link.rs @@ -2,9 +2,11 @@ use colored::*; use std::fmt::Display; use crate::{ - check_update, errors::RailwayError, - util::prompt::{fake_select, prompt_options, prompt_options_skippable}, + util::{ + check_update::check_update_command, + prompt::{fake_select, prompt_options, prompt_options_skippable}, + }, }; use super::{ @@ -39,7 +41,7 @@ pub struct Args { pub async fn command(args: Args, _json: bool) -> Result<()> { let mut configs = Configs::new()?; - check_update!(configs); + check_update_command(&mut configs).await?; let client = GQLClient::new_authorized(&configs)?; let me = post_graphql::( diff --git a/src/commands/run.rs b/src/commands/run.rs index 230d203d6..852d3f8a3 100644 --- a/src/commands/run.rs +++ b/src/commands/run.rs @@ -2,14 +2,16 @@ use anyhow::bail; use is_terminal::IsTerminal; use crate::{ - check_update, controllers::{ environment::get_matched_environment, project::{ensure_project_and_environment_exist, get_project}, variables::get_service_variables, }, errors::RailwayError, - util::prompt::{prompt_select, PromptService}, + util::{ + check_update::check_update_command, + prompt::{prompt_select, PromptService}, + }, }; use super::{queries::project::ProjectProject, *}; @@ -76,7 +78,7 @@ async fn get_service( pub async fn command(args: Args, _json: bool) -> Result<()> { // only needs to be mutable for the update check let mut configs = Configs::new()?; - check_update!(configs); + check_update_command(&mut configs).await?; let configs = configs; // so we make it immutable again let client = GQLClient::new_authorized(&configs)?; diff --git a/src/macros.rs b/src/macros.rs index 2e0075163..164d492a7 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -42,25 +42,3 @@ macro_rules! interact_or { } }; } - -#[macro_export] -macro_rules! check_update { - ($obj:expr, $force:expr) => {{ - let result = $obj.check_update($force).await; - - if let Ok(Some(latest_version)) = result { - println!( - "{} v{} visit {} for more info", - "New version available:".green().bold(), - latest_version.yellow(), - "https://docs.railway.com/guides/cli".purple(), - ); - false - } else { - true - } - }}; - ($configs:expr) => {{ - check_update!($configs, false); - }}; -} diff --git a/src/main.rs b/src/main.rs index 146f629cf..118401b0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ use clap::{Parser, Subcommand}; mod commands; use commands::*; +use util::check_update::check_update_command; mod client; mod config; @@ -68,17 +69,12 @@ async fn main() -> Result<()> { // intercept the args { let args: Vec = std::env::args().collect(); - - let flags: Vec = vec!["--version", "-V", "-h", "--help", "help"] - .into_iter() - .map(|s| s.to_string()) - .collect(); - - let check_version = args.into_iter().any(|arg| flags.contains(&arg)); + let flags = ["--version", "-V", "-h", "--help", "help"]; + let check_version = args.into_iter().any(|arg| flags.contains(&arg.as_str())); if check_version { let mut configs = Configs::new()?; - check_update!(configs, false); + check_update_command(&mut configs).await?; } } diff --git a/src/util/check_update.rs b/src/util/check_update.rs new file mode 100644 index 000000000..da1bd95e8 --- /dev/null +++ b/src/util/check_update.rs @@ -0,0 +1,21 @@ +use colored::Colorize; + +pub async fn check_update(configs: &mut crate::Configs, force: bool) -> anyhow::Result { + let result = configs.check_update(force).await; + if let Ok(Some(latest_version)) = result { + println!( + "{} v{} visit {} for more info", + "New version available:".green().bold(), + latest_version.yellow(), + "https://docs.railway.com/guides/cli".purple(), + ); + Ok(false) + } else { + Ok(true) + } +} + +pub async fn check_update_command(configs: &mut crate::Configs) -> anyhow::Result<()> { + check_update(configs, false).await?; + Ok(()) +} diff --git a/src/util/mod.rs b/src/util/mod.rs index 4c1a3567f..487cc046b 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,2 +1,3 @@ pub mod logs; pub mod prompt; +pub mod check_update;