Skip to content

Commit

Permalink
Fix service command showing services that aren't in current environme…
Browse files Browse the repository at this point in the history
…nt (#508)
  • Loading branch information
Milo123459 authored Jun 6, 2024
1 parent 52b007f commit 829259c
Showing 1 changed file with 34 additions and 5 deletions.
39 changes: 34 additions & 5 deletions src/commands/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use anyhow::bail;
use crate::{
controllers::project::get_project,
errors::RailwayError,
util::prompt::{prompt_select, PromptService},
util::prompt::{fake_select, prompt_options, PromptService},
};

use super::*;

/// Link a service to the current project
#[derive(Parser)]
pub struct Args {
/// The service to link
/// The service ID/name to link
service: Option<String>,
}

Expand All @@ -25,6 +25,13 @@ pub async fn command(args: Args, _json: bool) -> Result<()> {
.services
.edges
.iter()
.filter(|a| {
a.node
.service_instances
.edges
.iter()
.any(|b| b.node.environment_id == linked_project.environment)
})
.map(|s| PromptService(&s.node))
.collect();

Expand All @@ -43,9 +50,31 @@ pub async fn command(args: Args, _json: bool) -> Result<()> {
bail!("No services found");
}

let service = prompt_select("Select a service", services)?;
let service = if !services.is_empty() {
Some(if let Some(service) = args.service {
let service_norm = services.iter().find(|s| {
(s.0.name.to_lowercase() == service.to_lowercase())
|| (s.0.id.to_lowercase() == service.to_lowercase())
});
if let Some(service) = service_norm {
fake_select("Select a service", &service.0.name);
service.clone()
} else {
return Err(RailwayError::ServiceNotFound(service).into());
}
} else {
prompt_options("Select a service", services)?
})
} else {
None
};

configs.link_service(service.0.id.clone())?;
configs.write()?;
if let Some(service) = service {
configs.link_service(service.0.id.clone())?;
configs.write()?;
println!("Linked service {}", service.0.name.green())
} else {
bail!("No service found");
}
Ok(())
}

0 comments on commit 829259c

Please sign in to comment.