Skip to content

Commit 9b87ef6

Browse files
committed
Deprecate tedge::command::BuildContext
Now the commands are directly given the config instead of a way to build the config. As some commands have to update the config (notably `tedge config set`), the command are given not only the config but also the location of that config. Signed-off-by: Didier Wenzek <didier.wenzek@free.fr>
1 parent 34a027a commit 9b87ef6

File tree

14 files changed

+129
-83
lines changed

14 files changed

+129
-83
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ use clap::ValueHint;
1111
use tedge_config::tedge_toml::OptionalConfigError;
1212
use tedge_config::tedge_toml::ProfileName;
1313
use tedge_config::TEdgeConfig;
14+
use tedge_config::TEdgeConfigLocation;
1415

1516
use crate::cli::common::Cloud;
1617
use crate::cli::common::CloudArg;
1718
use crate::command::BuildCommand;
18-
use crate::command::BuildContext;
1919
use crate::command::Command;
2020
use crate::ConfigError;
2121

@@ -69,8 +69,11 @@ pub enum TEdgeCertCli {
6969
}
7070

7171
impl BuildCommand for TEdgeCertCli {
72-
fn build_command(self, context: BuildContext) -> Result<Box<dyn Command>, ConfigError> {
73-
let config = context.load_config()?;
72+
fn build_command(
73+
self,
74+
config: TEdgeConfig,
75+
_: TEdgeConfigLocation,
76+
) -> Result<Box<dyn Command>, ConfigError> {
7477
let (user, group) = if config.mqtt.bridge.built_in {
7578
("tedge", "tedge")
7679
} else {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
use super::TEdgeCli;
22
use crate::command::BuildCommand;
3-
use crate::command::BuildContext;
43
use crate::command::Command;
54
use crate::ConfigError;
65
use clap::CommandFactory;
76
use std::io;
7+
use tedge_config::TEdgeConfig;
8+
use tedge_config::TEdgeConfigLocation;
89

910
#[derive(clap::ValueEnum, Clone, Copy, Debug, strum_macros::Display)]
1011
#[strum(serialize_all = "snake_case")]
@@ -36,7 +37,11 @@ impl From<Shell> for Box<dyn clap_complete::env::EnvCompleter> {
3637
}
3738

3839
impl BuildCommand for Shell {
39-
fn build_command(self, _: BuildContext) -> Result<Box<dyn Command>, ConfigError> {
40+
fn build_command(
41+
self,
42+
_: TEdgeConfig,
43+
_: TEdgeConfigLocation,
44+
) -> Result<Box<dyn Command>, ConfigError> {
4045
Ok(Box::new(CompletionsCmd { shell: self }))
4146
}
4247
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use clap_complete::ArgValueCandidates;
66
use tedge_config::tedge_toml::ProfileName;
77
use tedge_config::tedge_toml::ReadableKey;
88
use tedge_config::tedge_toml::WritableKey;
9+
use tedge_config::TEdgeConfig;
10+
use tedge_config::TEdgeConfigLocation;
911

1012
#[derive(clap::Subcommand, Debug)]
1113
pub enum ConfigCmd {
@@ -132,9 +134,11 @@ macro_rules! try_with_profile {
132134
}
133135

134136
impl BuildCommand for ConfigCmd {
135-
fn build_command(self, context: BuildContext) -> Result<Box<dyn Command>, ConfigError> {
136-
let config_location = context.config_location;
137-
137+
fn build_command(
138+
self,
139+
_: TEdgeConfig,
140+
config_location: TEdgeConfigLocation,
141+
) -> Result<Box<dyn Command>, ConfigError> {
138142
match self {
139143
ConfigCmd::Get { key, profile } => Ok(GetConfigCommand {
140144
key: try_with_profile!(key, profile),

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use crate::cli::common::CloudArg;
22
use crate::cli::connect::*;
33
use crate::command::BuildCommand;
4-
use crate::command::BuildContext;
54
use crate::command::Command;
65
use crate::system_services::service_manager;
6+
use tedge_config::TEdgeConfig;
7+
use tedge_config::TEdgeConfigLocation;
78

89
#[derive(clap::Args, Debug, Eq, PartialEq)]
910
pub struct TEdgeConnectOpt {
@@ -20,19 +21,23 @@ pub struct TEdgeConnectOpt {
2021
}
2122

2223
impl BuildCommand for TEdgeConnectOpt {
23-
fn build_command(self, context: BuildContext) -> Result<Box<dyn Command>, crate::ConfigError> {
24+
fn build_command(
25+
self,
26+
config: TEdgeConfig,
27+
config_location: TEdgeConfigLocation,
28+
) -> Result<Box<dyn Command>, crate::ConfigError> {
2429
let Self {
2530
is_test_connection,
2631
offline_mode,
2732
cloud,
2833
} = self;
2934
Ok(Box::new(ConnectCommand {
30-
config_location: context.config_location.clone(),
31-
config: context.load_config()?,
35+
config_location: config_location.clone(),
36+
config,
3237
cloud: cloud.try_into()?,
3338
is_test_connection,
3439
offline_mode,
35-
service_manager: service_manager(&context.config_location.tedge_config_root_path)?,
40+
service_manager: service_manager(&config_location.tedge_config_root_path)?,
3641
is_reconnect: false,
3742
}))
3843
}

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::cli::common::CloudArg;
22
use crate::cli::disconnect::disconnect_bridge::*;
33
use crate::command::*;
44
use crate::system_services::service_manager;
5+
use tedge_config::TEdgeConfig;
6+
use tedge_config::TEdgeConfigLocation;
57

68
#[derive(clap::Args, Debug)]
79
pub struct TEdgeDisconnectBridgeCli {
@@ -10,12 +12,16 @@ pub struct TEdgeDisconnectBridgeCli {
1012
}
1113

1214
impl BuildCommand for TEdgeDisconnectBridgeCli {
13-
fn build_command(self, context: BuildContext) -> Result<Box<dyn Command>, crate::ConfigError> {
15+
fn build_command(
16+
self,
17+
_: TEdgeConfig,
18+
config_location: TEdgeConfigLocation,
19+
) -> Result<Box<dyn Command>, crate::ConfigError> {
1420
let cmd = DisconnectBridgeCommand {
15-
config_location: context.config_location.clone(),
21+
config_location: config_location.clone(),
1622
cloud: self.cloud.try_into()?,
1723
use_mapper: true,
18-
service_manager: service_manager(&context.config_location.tedge_config_root_path)?,
24+
service_manager: service_manager(&config_location.tedge_config_root_path)?,
1925
};
2026
Ok(cmd.into_boxed())
2127
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use crate::cli::http::command::HttpAction;
22
use crate::cli::http::command::HttpCommand;
33
use crate::command::BuildCommand;
4-
use crate::command::BuildContext;
54
use crate::command::Command;
65
use crate::ConfigError;
76
use anyhow::anyhow;
@@ -14,6 +13,8 @@ use reqwest::Client;
1413
use reqwest::Identity;
1514
use tedge_config::tedge_toml::ProfileName;
1615
use tedge_config::OptionalConfig;
16+
use tedge_config::TEdgeConfig;
17+
use tedge_config::TEdgeConfigLocation;
1718
use tokio::fs::File;
1819

1920
#[derive(clap::Subcommand, Debug)]
@@ -194,8 +195,11 @@ impl Content {
194195
}
195196

196197
impl BuildCommand for TEdgeHttpCli {
197-
fn build_command(self, context: BuildContext) -> Result<Box<dyn Command>, ConfigError> {
198-
let config = context.load_config()?;
198+
fn build_command(
199+
self,
200+
config: TEdgeConfig,
201+
_: TEdgeConfigLocation,
202+
) -> Result<Box<dyn Command>, ConfigError> {
199203
let uri = self.uri();
200204

201205
let (protocol, host, port) = if uri.starts_with("/c8y") {

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::command::BuildContext;
1+
use super::log::MaybeFancy;
22
use crate::command::Command;
33
use crate::Component;
44
use anyhow::bail;
@@ -8,28 +8,35 @@ use std::io::ErrorKind;
88
use std::os::unix::fs::MetadataExt;
99
use std::path::Path;
1010
use std::path::PathBuf;
11+
use tedge_config::TEdgeConfig;
12+
use tedge_config::TEdgeConfigLocation;
1113
use tedge_utils::file::change_user_and_group;
1214
use tedge_utils::file::create_directory;
1315
use tedge_utils::file::PermissionEntry;
1416
use tracing::debug;
1517

16-
use super::log::MaybeFancy;
17-
18-
#[derive(Debug)]
1918
pub struct TEdgeInitCmd {
2019
user: String,
2120
group: String,
2221
relative_links: bool,
23-
context: BuildContext,
22+
config_location: TEdgeConfigLocation,
23+
config: TEdgeConfig,
2424
}
2525

2626
impl TEdgeInitCmd {
27-
pub fn new(user: String, group: String, relative_links: bool, context: BuildContext) -> Self {
27+
pub fn new(
28+
user: String,
29+
group: String,
30+
relative_links: bool,
31+
config: TEdgeConfig,
32+
config_location: TEdgeConfigLocation,
33+
) -> Self {
2834
Self {
2935
user,
3036
group,
3137
relative_links,
32-
context,
38+
config_location,
39+
config,
3340
}
3441
}
3542
}
@@ -79,7 +86,7 @@ impl TEdgeInitCmd {
7986
create_symlinks_for(component, target, executable_dir, &RealEnv).await?;
8087
}
8188

82-
let config_dir = self.context.config_location.tedge_config_root_path.clone();
89+
let config_dir = self.config_location.tedge_config_root_path.clone();
8390
let permissions = {
8491
PermissionEntry::new(
8592
Some(self.user.clone()),
@@ -96,7 +103,7 @@ impl TEdgeInitCmd {
96103
create_directory(config_dir.join("device-certs"), &permissions).await?;
97104
create_directory(config_dir.join(".tedge-mapper-c8y"), &permissions).await?;
98105

99-
let config = self.context.load_config()?;
106+
let config = &self.config;
100107

101108
create_directory(&config.logs.path, &permissions).await?;
102109
create_directory(&config.data.path, &permissions).await?;

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

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
pub use self::certificate::*;
22
use self::refresh_bridges::RefreshBridgesCmd;
33
use crate::command::BuildCommand;
4-
use crate::command::BuildContext;
54
use crate::command::Command;
65
use c8y_firmware_plugin::FirmwarePluginOpt;
76
use c8y_remote_access_plugin::C8yRemoteAccessPluginOpt;
@@ -10,6 +9,8 @@ pub use connect::*;
109
use tedge_agent::AgentOpt;
1110
use tedge_apt_plugin::AptCli;
1211
use tedge_config::cli::CommonArgs;
12+
use tedge_config::TEdgeConfig;
13+
use tedge_config::TEdgeConfigLocation;
1314
use tedge_mapper::MapperOpt;
1415
use tedge_watchdog::WatchdogOpt;
1516
use tedge_write::bin::Args as TedgeWriteOpt;
@@ -182,7 +183,11 @@ fn styles() -> clap::builder::Styles {
182183
}
183184

184185
impl BuildCommand for TEdgeOpt {
185-
fn build_command(self, context: BuildContext) -> Result<Box<dyn Command>, crate::ConfigError> {
186+
fn build_command(
187+
self,
188+
config: TEdgeConfig,
189+
config_location: TEdgeConfigLocation,
190+
) -> Result<Box<dyn Command>, crate::ConfigError> {
186191
match self {
187192
TEdgeOpt::Init {
188193
user,
@@ -192,22 +197,25 @@ impl BuildCommand for TEdgeOpt {
192197
user,
193198
group,
194199
relative_links,
195-
context,
200+
config,
201+
config_location,
196202
))),
197-
TEdgeOpt::Upload(opt) => opt.build_command(context),
198-
TEdgeOpt::Cert(opt) => opt.build_command(context),
199-
TEdgeOpt::Config(opt) => opt.build_command(context),
200-
TEdgeOpt::Connect(opt) => opt.build_command(context),
201-
TEdgeOpt::Disconnect(opt) => opt.build_command(context),
202-
TEdgeOpt::RefreshBridges => RefreshBridgesCmd::new(&context).map(Command::into_boxed),
203-
TEdgeOpt::Mqtt(opt) => opt.build_command(context),
204-
TEdgeOpt::Http(opt) => opt.build_command(context),
205-
TEdgeOpt::Reconnect(opt) => opt.build_command(context),
203+
TEdgeOpt::Upload(opt) => opt.build_command(config, config_location),
204+
TEdgeOpt::Cert(opt) => opt.build_command(config, config_location),
205+
TEdgeOpt::Config(opt) => opt.build_command(config, config_location),
206+
TEdgeOpt::Connect(opt) => opt.build_command(config, config_location),
207+
TEdgeOpt::Disconnect(opt) => opt.build_command(config, config_location),
208+
TEdgeOpt::RefreshBridges => {
209+
RefreshBridgesCmd::new(config, config_location).map(Command::into_boxed)
210+
}
211+
TEdgeOpt::Mqtt(opt) => opt.build_command(config, config_location),
212+
TEdgeOpt::Http(opt) => opt.build_command(config, config_location),
213+
TEdgeOpt::Reconnect(opt) => opt.build_command(config, config_location),
206214
TEdgeOpt::Run(_) => {
207215
// This method has to be kept in sync with tedge::redirect_if_multicall()
208216
panic!("tedge mapper|agent|write commands are launched as multicall")
209217
}
210-
TEdgeOpt::Completions { shell } => shell.build_command(context),
218+
TEdgeOpt::Completions { shell } => shell.build_command(config, config_location),
211219
}
212220
}
213221
}

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@ use crate::cli::mqtt::publish::MqttPublishCommand;
22
use crate::cli::mqtt::subscribe::MqttSubscribeCommand;
33
use crate::cli::mqtt::subscribe::SimpleTopicFilter;
44
use crate::command::BuildCommand;
5-
use crate::command::BuildContext;
65
use crate::command::Command;
76
use clap_complete::ArgValueCandidates;
87
use clap_complete::CompletionCandidate;
98
use mqtt_channel::Topic;
109
use rumqttc::QoS;
1110
use tedge_config::models::SecondsOrHumanTime;
11+
use tedge_config::TEdgeConfig;
12+
use tedge_config::TEdgeConfigLocation;
1213

1314
const PUB_CLIENT_PREFIX: &str = "tedge-pub";
1415
const SUB_CLIENT_PREFIX: &str = "tedge-sub";
@@ -55,8 +56,11 @@ pub enum TEdgeMqttCli {
5556
}
5657

5758
impl BuildCommand for TEdgeMqttCli {
58-
fn build_command(self, context: BuildContext) -> Result<Box<dyn Command>, crate::ConfigError> {
59-
let config = context.load_config()?;
59+
fn build_command(
60+
self,
61+
config: TEdgeConfig,
62+
_: TEdgeConfigLocation,
63+
) -> Result<Box<dyn Command>, crate::ConfigError> {
6064
let auth_config = config.mqtt_client_auth_config();
6165

6266
let cmd = {

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use super::command::ReconnectBridgeCommand;
22
use crate::cli::common::CloudArg;
33
use crate::command::*;
44
use crate::system_services::service_manager;
5+
use tedge_config::TEdgeConfig;
6+
use tedge_config::TEdgeConfigLocation;
57

68
#[derive(clap::Args, Debug)]
79
pub struct TEdgeReconnectCli {
@@ -10,11 +12,15 @@ pub struct TEdgeReconnectCli {
1012
}
1113

1214
impl BuildCommand for TEdgeReconnectCli {
13-
fn build_command(self, context: BuildContext) -> Result<Box<dyn Command>, crate::ConfigError> {
15+
fn build_command(
16+
self,
17+
config: TEdgeConfig,
18+
config_location: TEdgeConfigLocation,
19+
) -> Result<Box<dyn Command>, crate::ConfigError> {
1420
Ok(ReconnectBridgeCommand {
15-
config: context.load_config()?,
16-
service_manager: service_manager(&context.config_location.tedge_config_root_path)?,
17-
config_location: context.config_location,
21+
config,
22+
service_manager: service_manager(&config_location.tedge_config_root_path)?,
23+
config_location,
1824
cloud: self.cloud.try_into()?,
1925
use_mapper: true,
2026
}

0 commit comments

Comments
 (0)