Skip to content

Commit 6027b34

Browse files
committed
Deprecate TEdgeConfigRepository
Signed-off-by: Didier Wenzek <didier.wenzek@free.fr>
1 parent e77a5df commit 6027b34

File tree

9 files changed

+47
-50
lines changed

9 files changed

+47
-50
lines changed

crates/common/tedge_config/src/lib.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ pub use self::tedge_config_cli::error::*;
77
pub use self::tedge_config_cli::models::*;
88
pub use self::tedge_config_cli::tedge_config::*;
99
pub use self::tedge_config_cli::tedge_config_location::*;
10-
pub use self::tedge_config_cli::tedge_config_repository::*;
1110
pub use camino::Utf8Path as Path;
1211
pub use camino::Utf8PathBuf as PathBuf;
1312
pub use certificate::CertificateError;
@@ -16,11 +15,26 @@ pub use tedge_config_macros::OptionalConfig;
1615

1716
impl TEdgeConfig {
1817
pub fn new(config_location: TEdgeConfigLocation) -> Result<Self, TEdgeConfigError> {
19-
TEdgeConfigRepository::new(config_location).load()
18+
config_location.load()
2019
}
2120

2221
pub fn load(config_dir: &Path) -> Result<TEdgeConfig, TEdgeConfigError> {
2322
let config_location = TEdgeConfigLocation::from_custom_root(config_dir);
2423
TEdgeConfig::new(config_location)
2524
}
25+
26+
#[cfg(feature = "test")]
27+
/// A test only method designed for injecting configuration into tests
28+
///
29+
/// ```
30+
/// use tedge_config::TEdgeConfig;
31+
/// let config = TEdgeConfig::load_toml_str("service.ty = \"service\"");
32+
///
33+
/// assert_eq!(&config.service.ty, "service");
34+
/// // Defaults are preserved
35+
/// assert_eq!(config.sudo.enable, true);
36+
/// ```
37+
pub fn load_toml_str(toml: &str) -> TEdgeConfig {
38+
TEdgeConfigLocation::load_toml_str(toml)
39+
}
2640
}

crates/common/tedge_config/src/tedge_config_cli/tedge_config_repository.rs

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ use super::figment::FileAndEnvironment;
1010
use super::figment::FileOnly;
1111
use super::figment::UnusedValueWarnings;
1212

13-
/// TEdgeConfigRepository is responsible for loading and storing TEdgeConfig entities.
14-
#[derive(Debug, Clone)]
15-
pub struct TEdgeConfigRepository {
16-
config_location: TEdgeConfigLocation,
17-
}
18-
19-
impl TEdgeConfigRepository {
13+
/// TEdgeConfigLocation is responsible for loading and storing TEdgeConfig entities.
14+
impl TEdgeConfigLocation {
2015
pub fn update_toml(
2116
&self,
2217
update: &impl Fn(&mut TEdgeConfigDto) -> ConfigSettingResult<()>,
@@ -28,20 +23,16 @@ impl TEdgeConfigRepository {
2823
}
2924

3025
fn toml_path(&self) -> &Utf8Path {
31-
self.config_location.tedge_config_file_path()
32-
}
33-
34-
pub fn new(config_location: TEdgeConfigLocation) -> Self {
35-
Self { config_location }
26+
self.tedge_config_file_path()
3627
}
3728

3829
pub fn load(&self) -> Result<TEdgeConfig, TEdgeConfigError> {
3930
let dto = self.load_dto::<FileAndEnvironment>(self.toml_path())?;
4031
debug!(
4132
"Loading configuration from {:?}",
42-
self.config_location.tedge_config_file_path
33+
self.tedge_config_file_path
4334
);
44-
Ok(TEdgeConfig::from_dto(&dto, &self.config_location))
35+
Ok(TEdgeConfig::from_dto(&dto, self))
4536
}
4637

4738
fn load_dto<Sources: ConfigSources>(
@@ -59,8 +50,8 @@ impl TEdgeConfigRepository {
5950
/// A test only method designed for injecting configuration into tests
6051
///
6152
/// ```
62-
/// use tedge_config::TEdgeConfigRepository;
63-
/// let config = TEdgeConfigRepository::load_toml_str("service.ty = \"service\"");
53+
/// use tedge_config::TEdgeConfigLocation;
54+
/// let config = TEdgeConfigLocation::load_toml_str("service.ty = \"service\"");
6455
///
6556
/// assert_eq!(&config.service.ty, "service");
6657
/// // Defaults are preserved
@@ -101,17 +92,13 @@ impl TEdgeConfigRepository {
10192
Ok((dto, warnings))
10293
}
10394

104-
pub fn get_config_location(&self) -> &TEdgeConfigLocation {
105-
&self.config_location
106-
}
107-
10895
// TODO: Explicitly set the file permissions in this function and file ownership!
10996
fn store<S: Serialize>(&self, config: &S) -> Result<(), TEdgeConfigError> {
11097
let toml = toml::to_string_pretty(&config)?;
11198

11299
// Create `$HOME/.tedge` or `/etc/tedge` directory in case it does not exist yet
113-
if !self.config_location.tedge_config_root_path.exists() {
114-
fs::create_dir(self.config_location.tedge_config_root_path())?;
100+
if !self.tedge_config_root_path.exists() {
101+
fs::create_dir(self.tedge_config_root_path())?;
115102
}
116103

117104
atomically_write_file_sync(self.toml_path(), toml.as_bytes())?;
@@ -196,7 +183,7 @@ child_update_timeout = 3429
196183
type = "a-service-type""#;
197184
let (_tempdir, config_location) = create_temp_tedge_config(toml).unwrap();
198185
let toml_path = config_location.tedge_config_file_path();
199-
let (dto, warnings) = TEdgeConfigRepository::new(config_location.clone())
186+
let (dto, warnings) = config_location
200187
.load_dto_with_warnings::<FileOnly>(toml_path)
201188
.unwrap();
202189

crates/core/tedge/src/cli/config/cli.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,29 +41,29 @@ pub enum ConfigCmd {
4141

4242
impl BuildCommand for ConfigCmd {
4343
fn build_command(self, context: BuildContext) -> Result<Box<dyn Command>, ConfigError> {
44-
let config_repository = context.config_repository;
44+
let config_location = context.config_location;
4545

4646
match self {
4747
ConfigCmd::Get { key } => Ok(GetConfigCommand {
4848
key,
49-
config: config_repository.load()?,
49+
config: config_location.load()?,
5050
}
5151
.into_boxed()),
5252
ConfigCmd::Set { key, value } => Ok(SetConfigCommand {
5353
key,
5454
value,
55-
config_repository,
55+
config_location,
5656
}
5757
.into_boxed()),
5858
ConfigCmd::Unset { key } => Ok(UnsetConfigCommand {
5959
key,
60-
config_repository,
60+
config_location,
6161
}
6262
.into_boxed()),
6363
ConfigCmd::List { is_all, is_doc } => Ok(ListConfigCommand {
6464
is_all,
6565
is_doc,
66-
config: config_repository.load()?,
66+
config: config_location.load()?,
6767
}
6868
.into_boxed()),
6969
}

crates/core/tedge/src/cli/config/commands/set.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use crate::command::Command;
2-
use tedge_config::TEdgeConfigRepository;
2+
use tedge_config::TEdgeConfigLocation;
33
use tedge_config::WritableKey;
44

55
pub struct SetConfigCommand {
66
pub key: WritableKey,
77
pub value: String,
8-
pub config_repository: TEdgeConfigRepository,
8+
pub config_location: TEdgeConfigLocation,
99
}
1010

1111
impl Command for SetConfigCommand {
@@ -18,7 +18,7 @@ impl Command for SetConfigCommand {
1818
}
1919

2020
fn execute(&self) -> anyhow::Result<()> {
21-
self.config_repository.update_toml(&|dto| {
21+
self.config_location.update_toml(&|dto| {
2222
dto.try_update_str(self.key, &self.value)
2323
.map_err(|e| e.into())
2424
})?;

crates/core/tedge/src/cli/config/commands/unset.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::command::Command;
2-
use tedge_config::TEdgeConfigRepository;
2+
use tedge_config::TEdgeConfigLocation;
33
use tedge_config::WritableKey;
44

55
pub struct UnsetConfigCommand {
66
pub key: WritableKey,
7-
pub config_repository: TEdgeConfigRepository,
7+
pub config_location: TEdgeConfigLocation,
88
}
99

1010
impl Command for UnsetConfigCommand {
@@ -13,7 +13,7 @@ impl Command for UnsetConfigCommand {
1313
}
1414

1515
fn execute(&self) -> anyhow::Result<()> {
16-
self.config_repository.update_toml(&|dto| {
16+
self.config_location.update_toml(&|dto| {
1717
dto.unset_key(self.key);
1818
Ok(())
1919
})?;

crates/core/tedge/src/command.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ pub trait Command {
128128
/// fn build_command(self, context: BuildContext) -> Result<Box<dyn Command>, ConfigError> {
129129
/// let cmd = match self {
130130
/// ConfigCmd::Set { key, value } => SetConfigCommand {
131-
/// config_repository: context.config_repository,
131+
/// config_location: context.config_location,
132132
/// key,
133133
/// value,
134134
/// }.into_boxed(),
135135
/// ConfigCmd::Get { key } => GetConfigCommand {
136-
/// config: context.config_repository.load()?,
136+
/// config: context.load_config()?,
137137
/// key,
138138
/// }.into_boxed(),
139139
/// };
@@ -149,17 +149,13 @@ pub trait BuildCommand {
149149
///
150150
#[derive(Debug)]
151151
pub struct BuildContext {
152-
pub config_repository: tedge_config::TEdgeConfigRepository,
153152
pub config_location: tedge_config::TEdgeConfigLocation,
154153
}
155154

156155
impl BuildContext {
157156
pub fn new(config_dir: impl AsRef<Path>) -> Self {
158157
let config_location = tedge_config::TEdgeConfigLocation::from_custom_root(config_dir);
159-
BuildContext {
160-
config_repository: tedge_config::TEdgeConfigRepository::new(config_location.clone()),
161-
config_location,
162-
}
158+
BuildContext { config_location }
163159
}
164160

165161
pub fn load_config(&self) -> Result<tedge_config::TEdgeConfig, tedge_config::TEdgeConfigError> {

crates/extensions/c8y_mapper_ext/src/converter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,7 @@ pub(crate) mod tests {
16271627
use tedge_api::mqtt_topics::MqttSchema;
16281628
use tedge_api::mqtt_topics::OperationType;
16291629
use tedge_api::SoftwareUpdateCommand;
1630-
use tedge_config::TEdgeConfigRepository;
1630+
use tedge_config::TEdgeConfig;
16311631
use tedge_mqtt_ext::test_helpers::assert_messages_matching;
16321632
use tedge_mqtt_ext::Message;
16331633
use tedge_mqtt_ext::MqttMessage;
@@ -2825,7 +2825,7 @@ pub(crate) mod tests {
28252825
async fn handles_empty_service_type_2383() {
28262826
let tmp_dir = TempTedgeDir::new();
28272827
let mut config = c8y_converter_config(&tmp_dir);
2828-
let tedge_config = TEdgeConfigRepository::load_toml_str("service.ty = \"\"");
2828+
let tedge_config = TEdgeConfig::load_toml_str("service.ty = \"\"");
28292829
config.service = tedge_config.service.clone();
28302830

28312831
let (mut converter, _) = create_c8y_converter_from_config(config);
@@ -3136,7 +3136,7 @@ pub(crate) mod tests {
31363136
let device_id = "test-device".into();
31373137
let device_topic_id = EntityTopicId::default_main_device();
31383138
let device_type = "test-device-type".into();
3139-
let tedge_config = TEdgeConfigRepository::load_toml_str("service.ty = \"service\"");
3139+
let tedge_config = TEdgeConfig::load_toml_str("service.ty = \"service\"");
31403140
let c8y_host = "test.c8y.io".into();
31413141
let tedge_http_host = "localhost".into();
31423142
let auth_proxy_addr = "127.0.0.1".into();

crates/extensions/c8y_mapper_ext/src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use tedge_api::mqtt_topics::EntityTopicId;
3535
use tedge_api::mqtt_topics::MqttSchema;
3636
use tedge_api::CommandStatus;
3737
use tedge_api::SoftwareUpdateCommand;
38-
use tedge_config::TEdgeConfigRepository;
38+
use tedge_config::TEdgeConfig;
3939
use tedge_file_system_ext::FsWatchEvent;
4040
use tedge_mqtt_ext::test_helpers::assert_received_contains_str;
4141
use tedge_mqtt_ext::test_helpers::assert_received_includes_json;
@@ -2382,7 +2382,7 @@ pub(crate) async fn spawn_c8y_mapper_actor(
23822382
let device_name = "test-device".into();
23832383
let device_topic_id = EntityTopicId::default_main_device();
23842384
let device_type = "test-device-type".into();
2385-
let config = TEdgeConfigRepository::load_toml_str("service.ty = \"service\"");
2385+
let config = TEdgeConfig::load_toml_str("service.ty = \"service\"");
23862386
let c8y_host = "test.c8y.io".into();
23872387
let tedge_http_host = "localhost:8888".into();
23882388
let mqtt_schema = MqttSchema::default();

crates/extensions/tedge_health_ext/src/tests.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use tedge_actors::SimpleMessageBoxBuilder;
1212
use tedge_api::mqtt_topics::EntityTopicId;
1313
use tedge_api::mqtt_topics::MqttSchema;
1414
use tedge_api::mqtt_topics::Service;
15-
use tedge_config::TEdgeConfigRepository;
15+
use tedge_config::TEdgeConfig;
1616
use tedge_mqtt_ext::MqttConfig;
1717
use tedge_mqtt_ext::MqttMessage;
1818
use tedge_mqtt_ext::Topic;
@@ -82,7 +82,7 @@ async fn spawn_a_health_check_actor(
8282
let mut health_mqtt_builder = MqttActorBuilder::new(mqtt_config);
8383

8484
let mqtt_schema = MqttSchema::new();
85-
let config = TEdgeConfigRepository::load_toml_str("service.ty = \"service\"");
85+
let config = TEdgeConfig::load_toml_str("service.ty = \"service\"");
8686
let service = Service {
8787
service_topic_id: EntityTopicId::default_main_service(service_to_be_monitored)
8888
.unwrap()

0 commit comments

Comments
 (0)