Skip to content

Commit

Permalink
refactor: check updates macro -> function
Browse files Browse the repository at this point in the history
  • Loading branch information
alexng353 committed Dec 18, 2024
1 parent bf9d609 commit ff13a84
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/commands/check_updates.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::check_update;
use crate::util::check_update::check_update;

use super::*;
use serde_json::json;
Expand All @@ -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{}",
Expand Down
4 changes: 2 additions & 2 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -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, *};

Expand All @@ -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)?;

Expand Down
8 changes: 5 additions & 3 deletions src/commands/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down Expand Up @@ -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::<queries::UserProjects, _>(
Expand Down
8 changes: 5 additions & 3 deletions src/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, *};
Expand Down Expand Up @@ -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)?;
Expand Down
22 changes: 0 additions & 22 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}};
}
12 changes: 4 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clap::{Parser, Subcommand};

mod commands;
use commands::*;
use util::check_update::check_update_command;

mod client;
mod config;
Expand Down Expand Up @@ -68,17 +69,12 @@ async fn main() -> Result<()> {
// intercept the args
{
let args: Vec<String> = std::env::args().collect();

let flags: Vec<String> = 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?;
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/util/check_update.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use colored::Colorize;

pub async fn check_update(configs: &mut crate::Configs, force: bool) -> anyhow::Result<bool> {
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(())
}
1 change: 1 addition & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod logs;
pub mod prompt;
pub mod check_update;

0 comments on commit ff13a84

Please sign in to comment.