summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <matthias.beyer@ifm.com>2022-04-13 14:02:36 +0200
committerMatthias Beyer <matthias.beyer@ifm.com>2022-04-13 14:34:30 +0200
commitcf1af609c295b529dac245c22a5f32172605ab85 (patch)
treefc06068f164b3b142b1497b10bda30fa5bb38d6c
parentbde5b9f075837d4a4740775ff59c376f34f59e02 (diff)
tedge-cli: Adapt for miette as error handling crate
This patch adapts the tedge-cli crate for the changes from commit 9ccd86589db8f9a72c4d7f545813e8da40b4d039 ("Replace PluginError with miette::Error") which replaced anyhow with miette. Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
-rw-r--r--Cargo.lock2
-rw-r--r--tedge/Cargo.toml2
-rw-r--r--tedge/src/logging.rs4
-rw-r--r--tedge/src/main.rs29
4 files changed, 19 insertions, 18 deletions
diff --git a/Cargo.lock b/Cargo.lock
index c0b618eb..02435664 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3007,11 +3007,11 @@ dependencies = [
name = "tedge-cli"
version = "0.1.0"
dependencies = [
- "anyhow",
"cfg-if 1.0.0",
"clap 3.1.6",
"console-subscriber",
"env_logger 0.9.0",
+ "miette",
"plugin_avg",
"plugin_httpstop",
"plugin_inotify",
diff --git a/tedge/Cargo.toml b/tedge/Cargo.toml
index 537925db..a7eb3b40 100644
--- a/tedge/Cargo.toml
+++ b/tedge/Cargo.toml
@@ -7,9 +7,9 @@ edition = "2021"
[dependencies]
clap = { version = "3", features = ["derive", "cargo", "suggestions"] }
-anyhow = "1"
toml = "0.5.8"
tokio = { version = "1", features = ["fs", "macros", "rt-multi-thread", "signal"] }
+miette = "4.4"
cfg-if = "1"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"], optional = true }
diff --git a/tedge/src/logging.rs b/tedge/src/logging.rs
index 26e3e1a4..fbbe8088 100644
--- a/tedge/src/logging.rs
+++ b/tedge/src/logging.rs
@@ -1,5 +1,5 @@
#[allow(unused)]
-pub(crate) fn setup_logging(verbose: bool, debugging: bool) -> anyhow::Result<()> {
+pub(crate) fn setup_logging(verbose: bool, debugging: bool) -> miette::Result<()> {
#[cfg(all(feature = "core_logging", not(feature = "core_debugging")))]
{
use tracing_subscriber::filter::EnvFilter;
@@ -18,7 +18,7 @@ pub(crate) fn setup_logging(verbose: bool, debugging: bool) -> anyhow::Result<()
.finish();
tracing::subscriber::set_global_default(subscriber)
- .map_err(|e| anyhow::anyhow!("Failed to set global subscriber: {:?}", e))?;
+ .map_err(|e| miette::miette!("Failed to set global subscriber: {:?}", e))?;
}
Ok(())
diff --git a/tedge/src/main.rs b/tedge/src/main.rs
index 90749277..53fe68ec 100644
--- a/tedge/src/main.rs
+++ b/tedge/src/main.rs
@@ -1,11 +1,12 @@
use std::collections::HashSet;
use clap::Parser;
+use miette::IntoDiagnostic;
use tedge_api::PluginBuilder;
+use tedge_core::configuration::TedgeConfiguration;
use tedge_core::TedgeApplication;
use tedge_core::TedgeApplicationCancelSender;
-use tedge_core::configuration::TedgeConfiguration;
use tedge_lib::measurement::Measurement;
use tracing::debug;
use tracing::error;
@@ -16,7 +17,7 @@ mod logging;
#[tokio::main]
#[tracing::instrument]
-async fn main() -> anyhow::Result<()> {
+async fn main() -> miette::Result<()> {
#[cfg(feature = "core_debugging")]
{
console_subscriber::init();
@@ -30,9 +31,9 @@ async fn main() -> anyhow::Result<()> {
cli::CliCommand::ValidateConfig { ref config } => config,
};
- let configuration = tokio::fs::read_to_string(config).await?;
+ let configuration = tokio::fs::read_to_string(config).await.into_diagnostic()?;
- let config: TedgeConfiguration = toml::de::from_str(&configuration)?;
+ let config: TedgeConfiguration = toml::de::from_str(&configuration).into_diagnostic()?;
info!("Configuration loaded.");
let application = TedgeApplication::builder();
@@ -46,9 +47,9 @@ async fn main() -> anyhow::Result<()> {
let kind_name: &'static str = <$pluginbuilder as PluginBuilder<tedge_core::PluginDirectory>>::kind_name();
info!("Registering plugin builder for plugins of type {}", kind_name);
if !plugin_kinds.insert(kind_name) {
- anyhow::bail!("Plugin kind '{}' was already registered, cannot register!", kind_name)
+ miette::bail!("Plugin kind '{}' was already registered, cannot register!", kind_name)
}
- $app.with_plugin_builder($pbinstance)?
+ $app.with_plugin_builder($pbinstance).into_diagnostic()?
} else {
trace!("Not supporting plugins of type {}", std::stringify!($pluginbuilder));
$app
@@ -88,7 +89,7 @@ async fn main() -> anyhow::Result<()> {
plugin_httpstop::HttpStopPluginBuilder
);
- let (cancel_sender, application) = application.with_config(config)?;
+ let (cancel_sender, application) = application.with_config(config).into_diagnostic()?;
info!("Application built");
match args.command {
@@ -108,19 +109,19 @@ async fn main() -> anyhow::Result<()> {
async fn run(
cancel_sender: TedgeApplicationCancelSender,
application: TedgeApplication,
-) -> anyhow::Result<()> {
+) -> miette::Result<()> {
info!("Booting app now.");
let mut run_fut = Box::pin(application.run());
- let kill_app = |fut| -> anyhow::Result<()> {
+ let kill_app = |fut| -> miette::Result<()> {
error!("Killing application");
drop(fut);
- anyhow::bail!("Application killed")
+ miette::bail!("Application killed")
};
let res = tokio::select! {
res = &mut run_fut => {
- res.map_err(anyhow::Error::from)
+ res.into_diagnostic()
},
_int = tokio::signal::ctrl_c() => {
@@ -128,7 +129,7 @@ async fn run(
info!("Shutting down...");
cancel_sender.cancel_app();
tokio::select! {
- res = &mut run_fut => res.map_err(anyhow::Error::from),
+ res = &mut run_fut => res.into_diagnostic(),
_ = tokio::signal::ctrl_c() => kill_app(run_fut),
}
} else {
@@ -141,7 +142,7 @@ async fn run(
res
}
-async fn validate_config(application: &TedgeApplication) -> anyhow::Result<()> {
+async fn validate_config(application: &TedgeApplication) -> miette::Result<()> {
let mut any_err = false;
for (plugin_name, res) in application.verify_configurations().await {
match res {
@@ -156,7 +157,7 @@ async fn validate_config(application: &TedgeApplication) -> anyhow::Result<()> {
}
if any_err {
- Err(anyhow::anyhow!("Plugin configuration error"))
+ Err(miette::miette!("Plugin configuration error"))
} else {
Ok(())
}