Skip to content
Merged
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
8 changes: 7 additions & 1 deletion src/commands/view_command.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::commands::view_command::view_preset_command::ViewPresetCommand;
use crate::commands::view_command::ViewCommand::Preset;
use crate::commands::view_command::view_service_command::ViewServiceCommand;
use crate::commands::view_command::ViewCommand::{Preset, Service};
use crate::config::current_config::HtrsConfig;
use crate::outcomes::{HtrsAction, HtrsError};
use clap::{ArgMatches, Command};

mod view_preset_command;
mod view_service_command;

pub enum ViewCommand {
Preset(ViewPresetCommand),
Service(ViewServiceCommand),
}

impl ViewCommand {
Expand All @@ -16,18 +19,21 @@ impl ViewCommand {
.about("View an item")
.arg_required_else_help(true)
.subcommand(ViewPresetCommand::get_command())
.subcommand(ViewServiceCommand::get_command())
}

pub fn bind_from_matches(args: &ArgMatches) -> ViewCommand {
match args.subcommand() {
Some(("preset", preset_matches)) => Preset(ViewPresetCommand::bind_from_matches(preset_matches)),
Some(("service", service_matches)) => Service(ViewServiceCommand::bind_from_matches(service_matches)),
_ => unreachable!(),
}
}

pub fn execute(&self, config: &HtrsConfig) -> Result<HtrsAction, HtrsError> {
match self {
Preset(command) => command.execute(config),
Service(command) => command.execute(config),
}
}
}
80 changes: 80 additions & 0 deletions src/commands/view_command/view_service_command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use crate::commands::bindings::MatchBinding;
use crate::config::current_config::{Endpoint, Environment, HtrsConfig};
use crate::outcomes::HtrsAction::PrintDialogue;
use crate::outcomes::{HtrsAction, HtrsError};
use clap::{Arg, ArgMatches, Command};

pub struct ViewServiceCommand {
pub name: String,
}

impl ViewServiceCommand {
pub fn get_command() -> Command {
Command::new("service")
.arg_required_else_help(true)
.arg(
Arg::new("name")
.required(true)
.help("Name or alias of the service to view")
)
}

pub fn bind_from_matches(args: &ArgMatches) -> Self {
Self {
name: args.bind_field("name"),
}
}

pub fn execute(&self, config: &HtrsConfig) -> Result<HtrsAction, HtrsError> {
let Some(service) = config.get_service(&self.name) else {
return Err(HtrsError::new(format!("No service could be found with name or alias `{}`", self.name).as_str()));
};

let mut text = String::new();
text.push_str(format!("Name: {}\n", service.name).as_str());
if let Some(alias) = &service.alias {
text.push_str(format!(" Alias: {}\n", alias).as_str());
}
text.push_str("Environments:\n");
let environment_text = service.environments.iter()
.map(Self::get_environment_str)
.collect::<Vec<String>>()
.join("");
text.push_str(match environment_text.is_empty() {
true => " (no environments)\n",
false => environment_text.as_str()
});

text.push_str("Endpoints:\n");
let endpoint_text = service.endpoints.iter()
.map(Self::get_endpoint_string)
.collect::<Vec<String>>()
.join("");
text.push_str(match endpoint_text.is_empty() {
true => " (no endpoints)\n",
false => endpoint_text.as_str(),
});

Ok(PrintDialogue(text))
}

fn get_environment_str(environment: &Environment) -> String {
match environment.alias {
Some(ref alias) => format!(" - {} ({}) ~ {}\n", environment.name, alias, environment.host),
None => format!(" - {} ~ {}\n", environment.name, environment.host)
}
}

fn get_endpoint_string(endpoint: &Endpoint) -> String {
let mut text = String::new();
text.push_str(format!(" - {} ~ {}\n", endpoint.name, endpoint.path_template).as_str());
for param in &endpoint.query_parameters {
match param.required {
true => text.push_str(format!(" - *{}\n", param.name).as_str()),
false => text.push_str(format!(" - {}\n", param.name).as_str()),
};
}

text
}
}
3 changes: 2 additions & 1 deletion tests/service/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mod create_new_service_tests;
mod delete_service_tests;
mod edit_service_tests;
mod list_services_tests;
mod list_services_tests;
mod view_service_tests;
71 changes: 71 additions & 0 deletions tests/service/view_service_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

#[cfg(test)]
mod view_service_tests {
use crate::common::builders::{EndpointBuilder, EnvironmentBuilder, HtrsConfigBuilder, ServiceBuilder};
use crate::common::test_helpers::{clear_config, setup};
use assert_cmd::Command;
use predicates::boolean::PredicateBooleanExt;
use predicates::str::contains;
use std::error::Error;

#[test]
fn given_view_service_command_with_unknown_service_then_should_error() -> Result<(), Box<dyn Error>> {
let path = setup(None);

Command::cargo_bin("htrs")?
.env("HTRS_CONFIG_PATH", &path)
.arg("view")
.arg("service")
.arg("unknown_service")
.assert()
.failure()
.stdout("No service could be found with name or alias `unknown_service`\n");

clear_config(&path);
Ok(())
}

#[test]
fn given_view_service_command_with_known_service_then_should_succeed() -> Result<(), Box<dyn Error>> {
let config = HtrsConfigBuilder::new()
.with_service(
ServiceBuilder::new()
.with_name("foo_name")
.with_alias("foo_alias")
.with_environment(
EnvironmentBuilder::new()
.with_name("foo_environment")
.with_alias("foo_env_alias")
.with_host("foo.com")
)
.with_endpoint(
EndpointBuilder::new()
.with_name("foo_endpoint")
.with_path("/my/{path_param}/path")
.with_query_param("required_param", true)
.with_query_param("optional_param", false)
)
)
.build();
let path = setup(Some(config));

Command::cargo_bin("htrs")?
.env("HTRS_CONFIG_PATH", &path)
.arg("view")
.arg("service")
.arg("foo_name")
.assert()
.success()
.stdout(
contains("Name: foo_name")
.and(contains("Alias: foo_alias"))
.and(contains(" - foo_environment (foo_env_alias) ~ foo.com"))
.and(contains("foo_endpoint ~ /my/{path_param}/path"))
.and(contains(" - *required_param"))
.and(contains(" - optional_param"))
);

clear_config(&path);
Ok(())
}
}