diff options
Diffstat (limited to 'crates/core/tedge/src/cli/config/commands')
-rw-r--r-- | crates/core/tedge/src/cli/config/commands/get.rs | 39 | ||||
-rw-r--r-- | crates/core/tedge/src/cli/config/commands/list.rs | 60 | ||||
-rw-r--r-- | crates/core/tedge/src/cli/config/commands/mod.rs | 6 | ||||
-rw-r--r-- | crates/core/tedge/src/cli/config/commands/set.rs | 25 | ||||
-rw-r--r-- | crates/core/tedge/src/cli/config/commands/unset.rs | 24 |
5 files changed, 154 insertions, 0 deletions
diff --git a/crates/core/tedge/src/cli/config/commands/get.rs b/crates/core/tedge/src/cli/config/commands/get.rs new file mode 100644 index 00000000..4cb0f1cc --- /dev/null +++ b/crates/core/tedge/src/cli/config/commands/get.rs @@ -0,0 +1,39 @@ +use crate::cli::config::ConfigKey; +use crate::command::Command; + +pub struct GetConfigCommand { + pub config_key: ConfigKey, + pub config: tedge_config::TEdgeConfig, +} + +impl Command for GetConfigCommand { + fn description(&self) -> String { + format!( + "get the configuration value for key: {}", + self.config_key.key + ) + } + + fn execute(&self) -> anyhow::Result<()> { + match (self.config_key.get)(&self.config) { + Ok(value) => { + println!("{}", value); + } + Err(tedge_config::ConfigSettingError::ConfigNotSet { .. }) => { + println!( + "The provided config key: '{}' is not set", + self.config_key.key + ); + } + Err(tedge_config::ConfigSettingError::SettingIsNotConfigurable { .. }) => { + println!( + "The provided config key: '{}' is not configurable", + self.config_key.key + ); + } + Err(err) => return Err(err.into()), + } + + Ok(()) + } +} diff --git a/crates/core/tedge/src/cli/config/commands/list.rs b/crates/core/tedge/src/cli/config/commands/list.rs new file mode 100644 index 00000000..8645e7ec --- /dev/null +++ b/crates/core/tedge/src/cli/config/commands/list.rs @@ -0,0 +1,60 @@ +use crate::cli::config::config_key::*; +use crate::command::Command; +use crate::ConfigError; +use tedge_config::*; + +pub struct ListConfigCommand { + pub is_all: bool, + pub is_doc: bool, + pub config: TEdgeConfig, + pub config_keys: Vec<ConfigKey>, +} + +impl Command for ListConfigCommand { + fn description(&self) -> String { + "list the configuration keys and values".into() + } + + fn execute(&self) -> anyhow::Result<()> { + if self.is_doc { + print_config_doc(&self.config_keys); + } else { + print_config_list(&self.config_keys, &self.config, self.is_all)?; + } + + Ok(()) + } +} + +fn print_config_list( + config_keys: &[ConfigKey], + config: &TEdgeConfig, + all: bool, +) -> Result<(), ConfigError> { + let mut keys_without_values: Vec<String> = Vec::new(); + for config_key in config_keys { + match (config_key.get)(config) { + Ok(value) => { + println!("{}={}", config_key.key, value); + } + Err(tedge_config::ConfigSettingError::ConfigNotSet { .. }) + | Err(tedge_config::ConfigSettingError::SettingIsNotConfigurable { .. }) => { + keys_without_values.push(config_key.key.into()); + } + Err(err) => return Err(err.into()), + } + } + if all && !keys_without_values.is_empty() { + println!(); + for key in keys_without_values { + println!("{}=", key); + } + } + Ok(()) +} + +fn print_config_doc(config_keys: &[ConfigKey]) { + for config_key in config_keys { + println!("{:<30} {}", config_key.key, config_key.description); + } +} diff --git a/crates/core/tedge/src/cli/config/commands/mod.rs b/crates/core/tedge/src/cli/config/commands/mod.rs new file mode 100644 index 00000000..e08b70da --- /dev/null +++ b/crates/core/tedge/src/cli/config/commands/mod.rs @@ -0,0 +1,6 @@ +mod get; +mod list; +mod set; +mod unset; + +pub use self::{get::*, list::*, set::*, unset::*}; diff --git a/crates/core/tedge/src/cli/config/commands/set.rs b/crates/core/tedge/src/cli/config/commands/set.rs new file mode 100644 index 00000000..715f2664 --- /dev/null +++ b/crates/core/tedge/src/cli/config/commands/set.rs @@ -0,0 +1,25 @@ +use crate::cli::config::ConfigKey; +use crate::command::Command; +use tedge_config::*; + +pub struct SetConfigCommand { + pub config_key: ConfigKey, + pub value: String, + pub config_repository: TEdgeConfigRepository, +} + +impl Command for SetConfigCommand { + fn description(&self) -> String { + format!( + "set the configuration key: {} with value: {}.", + self.config_key.key, self.value + ) + } + + fn execute(&self) -> anyhow::Result<()> { + let mut config = self.config_repository.load()?; + let () = (self.config_key.set)(&mut config, self.value.to_string())?; + self.config_repository.store(&config)?; + Ok(()) + } +} diff --git a/crates/core/tedge/src/cli/config/commands/unset.rs b/crates/core/tedge/src/cli/config/commands/unset.rs new file mode 100644 index 00000000..222d8a3e --- /dev/null +++ b/crates/core/tedge/src/cli/config/commands/unset.rs @@ -0,0 +1,24 @@ +use crate::cli::config::ConfigKey; +use crate::command::Command; +use tedge_config::*; + +pub struct UnsetConfigCommand { + pub config_key: ConfigKey, + pub config_repository: TEdgeConfigRepository, +} + +impl Command for UnsetConfigCommand { + fn description(&self) -> String { + format!( + "unset the configuration value for key: {}", + self.config_key.key + ) + } + + fn execute(&self) -> anyhow::Result<()> { + let mut config = self.config_repository.load()?; + let () = (self.config_key.unset)(&mut config)?; + self.config_repository.store(&config)?; + Ok(()) + } +} |