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
34 changes: 34 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ lazy_static = "1.5.0"
rstest = "0.26.1"
assert_cmd = "2.0.17"
uuid = { version = "1.18.1", features = ["v4"]}
httptest = "0.16.3"
httptest = "0.16.3"
predicates = "3.1.3"
12 changes: 11 additions & 1 deletion src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ mod delete_command;
mod list_command;
mod set_command;
mod bindings;
mod view_command;

use crate::commands::call_command::CallServiceEndpointCommand;
use crate::commands::delete_command::DeleteCommand;
use crate::commands::edit_command::EditCommand;
use crate::commands::list_command::ListCommand;
use crate::commands::new_command::NewCommand;
use crate::commands::set_command::SetCommand;
use crate::commands::RootCommand::{Call, Delete, Edit, List, New, Set};
use crate::commands::view_command::ViewCommand;
use crate::commands::RootCommand::{Call, Delete, Edit, List, New, Set, View};
use crate::config::current_config::HtrsConfig;
use crate::htrs_binding_error::HtrsBindingError;
use crate::outcomes::{HtrsAction, HtrsError};
Expand All @@ -26,6 +28,7 @@ pub enum RootCommand {
Delete(DeleteCommand),
List(ListCommand),
Set(SetCommand),
View(ViewCommand)
}

impl RootCommand {
Expand All @@ -40,6 +43,7 @@ impl RootCommand {
.subcommand(DeleteCommand::get_command())
.subcommand(ListCommand::get_command())
.subcommand(SetCommand::get_command())
.subcommand(ViewCommand::get_command())
}

pub fn bind_from_matches(args: &ArgMatches, config: &HtrsConfig) -> Result<RootCommand, HtrsBindingError> {
Expand Down Expand Up @@ -74,6 +78,11 @@ impl RootCommand {
Ok(Set(
SetCommand::bind_from_matches(set_matches)
))
},
Some(("view", view_matches)) => {
Ok(View(
ViewCommand::bind_from_matches(view_matches)
))
}
_ => unreachable!()
}
Expand All @@ -89,6 +98,7 @@ impl RootCommand {
Delete(delete_command) => delete_command.execute(config),
List(list_command) => list_command.execute(config),
Set(set_command) => set_command.execute(config),
View(view_command) => view_command.execute(config),
}
}
}
33 changes: 33 additions & 0 deletions src/commands/view_command.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::commands::view_command::view_preset_command::ViewPresetCommand;
use crate::commands::view_command::ViewCommand::Preset;
use crate::config::current_config::HtrsConfig;
use crate::outcomes::{HtrsAction, HtrsError};
use clap::{ArgMatches, Command};

mod view_preset_command;

pub enum ViewCommand {
Preset(ViewPresetCommand),
}

impl ViewCommand {
pub fn get_command() -> Command {
Command::new("view")
.about("View an item")
.arg_required_else_help(true)
.subcommand(ViewPresetCommand::get_command())
}

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

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

pub struct ViewPresetCommand {
pub name: String,
}

impl ViewPresetCommand {
pub fn get_command() -> Command {
Command::new("preset")
.about("View a preset")
.arg(
Arg::new("name")
.help("Name or alias of the preset")
.value_name("name")
.required(true)
)
}

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

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

let name = match preset.alias {
Some(ref alias) => format!("{} ({}):", preset.name, alias),
None => format!("{}:", preset.name),
};

let values = preset.values.iter()
.map(|(key, value)| format!(" - {}: {}", key, value))
.collect::<Vec<String>>()
.join("\n");

Ok(HtrsAction::PrintDialogue(format!("{}\n{}\n", name, values)))
}
}
9 changes: 8 additions & 1 deletion tests/common/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct ServiceBuilder {

pub struct PresetBuilder {
pub name: Option<String>,
pub alias: Option<String>,
pub values: HashMap<String, String>,
}

Expand Down Expand Up @@ -121,6 +122,7 @@ impl PresetBuilder {
pub fn new() -> Self {
Self {
name: None,
alias: None,
values: HashMap::new(),
}
}
Expand All @@ -130,6 +132,11 @@ impl PresetBuilder {
self
}

pub fn with_alias(mut self, alias: &str) -> Self {
self.alias = Some(alias.to_string());
self
}

pub fn with_value(mut self, key: &str, value: &str) -> Self {
self.values.insert(key.to_string(), value.to_string());
self
Expand All @@ -138,7 +145,7 @@ impl PresetBuilder {
pub fn build(self) -> Preset {
Preset {
name: self.name.unwrap(),
alias: None,
alias: self.alias,
values: self.values,
}
}
Expand Down
3 changes: 2 additions & 1 deletion tests/presets/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
mod create_new_preset_tests;
mod delete_preset_tests;
mod edit_preset_tests;
mod edit_preset_tests;
mod view_preset_tests;
80 changes: 80 additions & 0 deletions tests/presets/view_preset_tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

mod view_preset_tests {
use crate::common::builders::{HtrsConfigBuilder, PresetBuilder};
use crate::common::test_helpers::{clear_config, setup};
use assert_cmd::Command;
use predicates::prelude::*;
use std::error::Error;

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

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

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

#[test]
pub fn given_known_preset_name_when_view_should_succeed() -> Result<(), Box<dyn Error>> {
let config = HtrsConfigBuilder::new()
.with_preset(
PresetBuilder::new()
.with_name("foo_name")
.with_value("key", "value")
)
.build();
let path = setup(Some(config));

Command::cargo_bin("htrs")?
.env("HTRS_CONFIG_PATH", &path)
.arg("view")
.arg("preset")
.arg("foo_name")
.assert()
.success()
.stdout(
predicate::str::contains("foo_name")
.and(predicate::str::contains(" - key: value"))
);

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

#[test]
pub fn given_known_preset_alias_when_view_should_succeed() -> Result<(), Box<dyn Error>> {
let config = HtrsConfigBuilder::new()
.with_preset(
PresetBuilder::new()
.with_name("foo_name")
.with_alias("foo_alias")
.with_value("key", "value")
)
.build();
let path = setup(Some(config));

Command::cargo_bin("htrs")?
.env("HTRS_CONFIG_PATH", &path)
.arg("view")
.arg("preset")
.arg("foo_alias")
.assert()
.success()
.stdout(
predicate::str::contains("foo_name (foo_alias)")
.and(predicate::str::contains(" - key: value"))
);

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