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
2 changes: 1 addition & 1 deletion Cargo.lock

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

201 changes: 157 additions & 44 deletions docs/website/root/manual/develop/nodes/mithril-client.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion mithril-client-cli/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "mithril-client-cli"
version = "0.12.19"
version = "0.12.20"
description = "A Mithril Client"
authors = { workspace = true }
edition = { workspace = true }
Expand Down
74 changes: 61 additions & 13 deletions mithril-client-cli/src/command_context.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
use anyhow::anyhow;
use config::ConfigBuilder;
use config::builder::DefaultState;
use slog::Logger;
use std::collections::HashMap;

use mithril_client::MithrilResult;

use crate::configuration::ConfigParameters;

/// Context for the command execution
pub struct CommandContext {
config_builder: ConfigBuilder<DefaultState>,
config_parameters: ConfigParameters,
unstable_enabled: bool,
json: bool,
logger: Logger,
}

impl CommandContext {
/// Create a new command context
pub fn new(
config_builder: ConfigBuilder<DefaultState>,
config_parameters: ConfigParameters,
unstable_enabled: bool,
json: bool,
logger: Logger,
) -> Self {
Self {
config_builder,
config_parameters,
unstable_enabled,
json,
logger,
}
}
Expand All @@ -34,6 +34,11 @@ impl CommandContext {
self.unstable_enabled
}

/// Check if JSON output is enabled
pub fn is_json_output_enabled(&self) -> bool {
self.json
}

/// Ensure that unstable commands are enabled
pub fn require_unstable(
&self,
Expand All @@ -51,11 +56,14 @@ impl CommandContext {
}
}

/// Get the configured parameters
pub fn config_parameters(&self) -> MithrilResult<ConfigParameters> {
let config = self.config_builder.clone().build()?;
let config_hash_map = config.try_deserialize::<HashMap<String, String>>()?;
Ok(ConfigParameters::new(config_hash_map))
/// Get a reference to the configured parameters
pub fn config_parameters(&self) -> &ConfigParameters {
&self.config_parameters
}

/// Get a mutable reference to the configured parameters
pub fn config_parameters_mut(&mut self) -> &mut ConfigParameters {
&mut self.config_parameters
}

/// Get the shared logger
Expand All @@ -67,15 +75,19 @@ impl CommandContext {
#[cfg(test)]
mod tests {
use slog::o;
use std::collections::HashMap;

use crate::configuration::{ConfigError, ConfigSource};

use super::*;

#[test]
fn require_unstable_return_ok_if_unstable_enabled() {
let unstable_enabled = true;
let context = CommandContext::new(
ConfigBuilder::default(),
ConfigParameters::default(),
unstable_enabled,
true,
Logger::root(slog::Discard, o!()),
);

Expand All @@ -87,12 +99,48 @@ mod tests {
fn require_unstable_return_err_if_unstable_disabled() {
let unstable_enabled = false;
let context = CommandContext::new(
ConfigBuilder::default(),
ConfigParameters::default(),
unstable_enabled,
true,
Logger::root(slog::Discard, o!()),
);

let result = context.require_unstable("test", None);
assert!(result.is_err(), "Expected Err, got {result:?}");
}

#[test]
fn can_edit_config_parameters() {
struct ParamSource {
key: String,
value: String,
}
impl ConfigSource for ParamSource {
fn collect(&self) -> Result<HashMap<String, String>, ConfigError> {
Ok(HashMap::from([(self.key.clone(), self.value.clone())]))
}
}

let mut context = CommandContext::new(
ConfigParameters::default(),
false,
true,
Logger::root(slog::Discard, o!()),
);

assert_eq!(context.config_parameters_mut().get("key"), None,);

context
.config_parameters_mut()
.add_source(&ParamSource {
key: "key".to_string(),
value: "value".to_string(),
})
.unwrap();

assert_eq!(
context.config_parameters_mut().get("key"),
Some("value".to_string())
);
}
}
Loading
Loading