Skip to content

Commit

Permalink
refactor service finding logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Milo123459 committed Sep 24, 2023
1 parent 78fce24 commit 2f4a8db
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 28 deletions.
26 changes: 12 additions & 14 deletions src/commands/down.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,20 @@ pub async fn command(args: Args, _json: bool) -> Result<()> {
let services = project.services.edges.iter().collect::<Vec<_>>();

let environment_id = get_matched_environment(&project, environment)?.id;
let service = if let Some(service_arg) = args.service {
let service = match (args.service, linked_project.service) {
// If the user specified a service, use that
let service_id = services
(Some(service_arg), _) => services
.iter()
.find(|service| service.node.name == service_arg || service.node.id == service_arg);
if let Some(service_id) = service_id {
Some(service_id.node.id.to_owned())
} else {
bail!("Service not found");
}
} else if let Some(service) = linked_project.service {
// If the user didn't specify a service, but we have a linked service, use that
Some(service)
} else {
bail!("No service could be found. Please either link one with `railway service` or specify one via the `--service` flag.");
}.unwrap();
.find(|service| service.node.name == service_arg || service.node.id == service_arg)
.with_context(|| format!("Service '{service_arg}' not found"))?
.node
.id
.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."),
};

let vars = queries::deployments::Variables {
input: DeploymentListInput {
Expand Down
26 changes: 12 additions & 14 deletions src/commands/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,20 @@ pub async fn command(args: Args, json: bool) -> Result<()> {
let services = project.services.edges.iter().collect::<Vec<_>>();

let environment_id = get_matched_environment(&project, environment)?.id;
let service = if let Some(service_arg) = args.service {
let service = match (args.service, linked_project.service) {
// If the user specified a service, use that
let service_id = services
(Some(service_arg), _) => services
.iter()
.find(|service| service.node.name == service_arg || service.node.id == service_arg);
if let Some(service_id) = service_id {
Some(service_id.node.id.to_owned())
} else {
bail!("Service not found");
}
} else if let Some(service) = linked_project.service {
// If the user didn't specify a service, but we have a linked service, use that
Some(service)
} else {
bail!("No service could be found. Please either link one with `railway service` or specify one via the `--service` flag.");
}.unwrap();
.find(|service| service.node.name == service_arg || service.node.id == service_arg)
.with_context(|| format!("Service '{service_arg}' not found"))?
.node
.id
.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."),
};

let vars = queries::deployments::Variables {
input: DeploymentListInput {
Expand Down

0 comments on commit 2f4a8db

Please sign in to comment.