Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/commands/deployment.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::*;
use crate::client::post_graphql;
use crate::controllers::environment::get_matched_environment;
use crate::controllers::project::{ensure_project_and_environment_exist, get_project};
use crate::controllers::project::{
ensure_project_and_environment_exist, get_project, select_service_fallback,
};
use crate::gql::queries::deployments::{DeploymentStatus, ResponseData, Variables};
use chrono::{DateTime, Local, Utc};
use serde::Serialize;
Expand Down Expand Up @@ -128,9 +130,9 @@ async fn list_deployments(
} else if let Some(linked_service_id) = linked_project.service {
linked_service_id
} else {
bail!(
"No service specified and no service linked. Use 'railway link' to link a service or specify one with the service argument."
);
select_service_fallback(&project.services.edges, true)?
.id
.clone()
};

let variables = Variables {
Expand Down
10 changes: 8 additions & 2 deletions src/commands/domain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use serde_json::json;

use crate::{
consts::TICK_STRING,
controllers::project::{ensure_project_and_environment_exist, get_project},
controllers::project::{
ensure_project_and_environment_exist, get_project, select_service_fallback,
},
errors::RailwayError,
};

Expand Down Expand Up @@ -175,6 +177,10 @@ pub fn get_service<'a>(
}

if project.services.edges.len() == 1 {
eprintln!(
"No service linked — auto-selecting \"{}\"",
project.services.edges[0].node.name
);
return Ok(&project.services.edges[0].node);
}

Expand Down Expand Up @@ -203,7 +209,7 @@ pub fn get_service<'a>(
}
}

bail!(RailwayError::NoServices);
select_service_fallback(&project.services.edges, true)
}

pub fn creating_domain_spiner(message: Option<String>) -> anyhow::Result<indicatif::ProgressBar> {
Expand Down
12 changes: 7 additions & 5 deletions src/commands/down.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use std::time::Duration;

use anyhow::bail;

use super::{
queries::{deployments::DeploymentListInput, deployments::DeploymentStatus},
*,
};
use crate::{
consts::TICK_STRING,
controllers::{environment::get_matched_environment, project::get_project},
errors::RailwayError,
controllers::{
environment::get_matched_environment,
project::{get_project, select_service_fallback},
},
util::prompt::prompt_confirm_with_default,
};

Expand Down Expand Up @@ -56,7 +56,9 @@ pub async fn command(args: Args) -> Result<()> {
// Otherwise if we have a linked service, use that
(_, Some(linked_service)) => linked_service,
// Otherwise it's a user error
_ => bail!(RailwayError::NoServiceLinked),
_ => select_service_fallback(&project.services.edges, false)?
.id
.to_owned(),
};

let vars = queries::deployments::Variables {
Expand Down
9 changes: 4 additions & 5 deletions src/commands/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
stream_build_logs, stream_deploy_logs, stream_http_logs,
},
environment::get_matched_environment,
project::{ensure_project_and_environment_exist, get_project},
project::{ensure_project_and_environment_exist, get_project, select_service_fallback},
},
util::{
logs::{LogFormat, print_http_log, print_log},
Expand Down Expand Up @@ -313,10 +313,9 @@ pub async fn command(args: Args) -> Result<()> {
.to_owned(),
// Otherwise if we have a linked service, use that
(_, Some(linked_service)) => linked_service,
// Otherwise it's a user error
_ => bail!(
"No service could be found. Please either link one with `railway service` or specify one via the `--service` flag."
),
_ => select_service_fallback(&project.services.edges, true)?
.id
.to_owned(),
};

// Fetch all deployments so we can find a sensible default deployment id if
Expand Down
33 changes: 19 additions & 14 deletions src/commands/redeploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use is_terminal::IsTerminal;
use crate::{
controllers::project::{
ensure_project_and_environment_exist, find_service_instance, get_project,
select_service_fallback,
},
errors::RailwayError,
util::{progress::create_spinner_if, prompt::prompt_confirm_with_default},
Expand Down Expand Up @@ -38,18 +39,22 @@ pub async fn command(args: Args) -> Result<()> {
let project = get_project(&client, &configs, linked_project.project.clone()).await?;
let is_terminal = std::io::stdout().is_terminal();

let service_id = args.service.or_else(|| linked_project.service.clone()).ok_or_else(|| anyhow!("No service found. Please link one via `railway link` or specify one via the `--service` flag."))?;
let service = project
.services
.edges
.iter()
.find(|s| {
s.node.id == service_id || s.node.name.to_lowercase() == service_id.to_lowercase()
})
.ok_or_else(|| anyhow!(RailwayError::ServiceNotFound(service_id)))?;
let service_node = match args.service.or_else(|| linked_project.service.clone()) {
Some(service_input) => project
.services
.edges
.iter()
.find(|s| {
s.node.id == service_input
|| s.node.name.to_lowercase() == service_input.to_lowercase()
})
.map(|s| &s.node)
.ok_or_else(|| anyhow!(RailwayError::ServiceNotFound(service_input)))?,
None => select_service_fallback(&project.services.edges, false)?,
};

let service_in_env =
find_service_instance(&project, linked_project.environment_id()?, &service.node.id)
find_service_instance(&project, linked_project.environment_id()?, &service_node.id)
.ok_or_else(|| {
anyhow!("The service specified doesn't exist in the current environment")
})?;
Expand All @@ -62,7 +67,7 @@ pub async fn command(args: Args) -> Result<()> {
bail!(
"The latest deployment for service {} cannot be redeployed. \
This may be because it's currently building, deploying, or was removed.",
service.node.name
service_node.name
);
}

Expand All @@ -72,7 +77,7 @@ pub async fn command(args: Args) -> Result<()> {
prompt_confirm_with_default(
format!(
"Redeploy the latest deployment from service {} in environment {}?",
service.node.name,
service_node.name,
linked_project
.environment_name
.clone()
Expand All @@ -95,7 +100,7 @@ pub async fn command(args: Args) -> Result<()> {
!args.json,
format!(
"Redeploying the latest deployment from service {}...",
service.node.name
service_node.name
),
);

Expand All @@ -116,7 +121,7 @@ pub async fn command(args: Args) -> Result<()> {
} else if let Some(spinner) = spinner {
spinner.finish_with_message(format!(
"The latest deployment from service {} has been redeployed",
service.node.name.green()
service_node.name.green()
));
}

Expand Down
46 changes: 22 additions & 24 deletions src/commands/restart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use is_terminal::IsTerminal;
use crate::{
controllers::project::{
ensure_project_and_environment_exist, find_service_instance, get_project,
select_service_fallback,
},
errors::RailwayError,
subscription::subscribe_graphql,
Expand Down Expand Up @@ -41,25 +42,22 @@ pub async fn command(args: Args) -> Result<()> {
let project = get_project(&client, &configs, linked_project.project.clone()).await?;
let is_terminal = std::io::stdout().is_terminal();

let service_id = args
.service
.or_else(|| linked_project.service.clone())
.ok_or_else(|| {
anyhow!(
"No service found. Please link one via `railway link` or specify one via the `--service` flag."
)
})?;
let service = project
.services
.edges
.iter()
.find(|s| {
s.node.id == service_id || s.node.name.to_lowercase() == service_id.to_lowercase()
})
.ok_or_else(|| anyhow!(RailwayError::ServiceNotFound(service_id)))?;
let service_node = match args.service.or_else(|| linked_project.service.clone()) {
Some(service_input) => project
.services
.edges
.iter()
.find(|s| {
s.node.id == service_input
|| s.node.name.to_lowercase() == service_input.to_lowercase()
})
.map(|s| &s.node)
.ok_or_else(|| anyhow!(RailwayError::ServiceNotFound(service_input)))?,
None => select_service_fallback(&project.services.edges, false)?,
};

let service_in_env =
find_service_instance(&project, linked_project.environment_id()?, &service.node.id)
find_service_instance(&project, linked_project.environment_id()?, &service_node.id)
.ok_or_else(|| {
anyhow!("The service specified doesn't exist in the current environment")
})?;
Expand All @@ -74,7 +72,7 @@ pub async fn command(args: Args) -> Result<()> {
prompt_confirm_with_default(
format!(
"Restart the latest deployment from service {} in environment {}?",
service.node.name,
service_node.name,
linked_project
.environment_name
.clone()
Expand All @@ -97,7 +95,7 @@ pub async fn command(args: Args) -> Result<()> {
!args.json,
format!(
"Restarting the latest deployment from service {}...",
service.node.name
service_node.name
),
);

Expand All @@ -118,7 +116,7 @@ pub async fn command(args: Args) -> Result<()> {
let spinner = spinner.unwrap();
spinner.set_message(format!(
"Waiting for deployment from service {} to be healthy...",
service.node.name
service_node.name
));

let mut stream =
Expand All @@ -144,21 +142,21 @@ pub async fn command(args: Args) -> Result<()> {
DeploymentStatus::SUCCESS => {
spinner.finish_with_message(format!(
"The latest deployment from service {} has been restarted and is healthy",
service.node.name.green()
service_node.name.green()
));
return Ok(());
}
DeploymentStatus::FAILED => {
spinner.finish_with_message(format!(
"Deployment from service {} failed",
service.node.name.red()
service_node.name.red()
));
bail!("Deployment failed");
}
DeploymentStatus::CRASHED => {
spinner.finish_with_message(format!(
"Deployment from service {} crashed",
service.node.name.red()
service_node.name.red()
));
bail!("Deployment crashed");
}
Expand All @@ -169,7 +167,7 @@ pub async fn command(args: Args) -> Result<()> {

spinner.finish_with_message(format!(
"The latest deployment from service {} has been restarted",
service.node.name.green()
service_node.name.green()
));

Ok(())
Expand Down
38 changes: 21 additions & 17 deletions src/commands/scale.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
controllers::{
environment::get_matched_environment,
project::find_service_instance,
project::{find_service_instance, select_service_fallback},
regions::{convert_hashmap_to_map, merge_config, prompt_for_regions},
},
util::progress::create_spinner_if,
Expand Down Expand Up @@ -139,24 +139,28 @@ fn get_existing_config(
environment: &str,
) -> Result<(Value, String)> {
let environment_id = get_matched_environment(project, environment.to_string())?.id;
let service_input = match args.service.as_ref() {
Some(s) => s,
None => linked_project.service.as_ref().ok_or_else(|| {
anyhow::anyhow!("No service linked. Please either specify a service with the --service flag or link one with `railway service`")
})?,
};

let service = project.services.edges.iter().find(|p| {
(p.node.id == *service_input)
|| (p.node.name.to_lowercase() == service_input.to_lowercase())
});

let Some(service) = service else {
bail!("Service '{}' not found in project", service_input);
let service_id = match args
.service
.clone()
.or_else(|| linked_project.service.clone())
{
Some(service_input) => project
.services
.edges
.iter()
.find(|p| {
p.node.id == service_input
|| p.node.name.to_lowercase() == service_input.to_lowercase()
})
.ok_or_else(|| anyhow::anyhow!("Service '{}' not found in project", service_input))?
.node
.id
.clone(),
None => select_service_fallback(&project.services.edges, false)?
.id
.clone(),
};

let service_id = service.node.id.clone();

// check that service exists in that environment
let instance = find_service_instance(project, &environment_id, &service_id);
let service_meta = if let Some(instance) = instance {
Expand Down
Loading
Loading