Add some logging

This commit is contained in:
Matteo Settenvini 2022-08-01 22:54:59 +02:00
parent 7f48c6cfa4
commit 9ccec9878a
Signed by: matteo
GPG Key ID: 8576CC1AD97D42DF
6 changed files with 45 additions and 2 deletions

21
Cargo.lock generated
View File

@ -156,9 +156,11 @@ dependencies = [
"clap",
"csv",
"directories",
"env_logger",
"futures",
"icalendar",
"libxml",
"log",
"regex",
"reqwest",
"rusty-hook",
@ -273,6 +275,19 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "env_logger"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b2cf0344971ee6c64c31be0d530793fba457d322dfec2810c453d0ef228f9c3"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "envmnt"
version = "0.8.4"
@ -512,6 +527,12 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c4a1e36c821dbe04574f602848a19f742f4fb3c98d40449f11bcad18d6b17421"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "hyper"
version = "0.14.20"

View File

@ -37,6 +37,9 @@ features = ["cargo"]
[dependencies.directories]
version = "4.0"
[dependencies.env_logger]
version = "0.9"
[dependencies.futures]
version = "0.3"
@ -47,6 +50,9 @@ features = ["parser"]
[dependencies.libxml]
version = "0.3"
[dependencies.log]
version = "0.4"
[dependencies.regex]
version = "1.6"

View File

@ -25,7 +25,7 @@ pub async fn with(
let ingredients = get_ingredients(api_client, ids).await?;
let ingredients = merge_ingredients(ingredients);
let md = prepare_grocery_list(&ingredients)?;
// TODO filename is hardcoded for now
log::debug!("Saving the following grocery list:\n\n{}", &md);
save_grocery_list(api_client, location, &md).await?;
Ok(())
}
@ -71,7 +71,10 @@ where
.await
.expect(&format!("Cannot fetch recipe with id {}", id));
response.json::<Recipe>().await.map(|r| r.ingredients)
response.json::<Recipe>().await.map(|r| {
log::info!("Retrieved ingredients for '{}'", r.name);
r.ingredients
})
});
let ingredients = futures::future::try_join_all(ingredients).await?;
@ -138,6 +141,7 @@ async fn save_grocery_list(api_client: &ApiClient, filename: &str, contents: &st
})?;
let file_url = dav_base_url.join(filename).unwrap();
log::info!("Saving grocery list to {}", &file_url);
let response = api_client
.rest()
.put(file_url.clone())

View File

@ -24,6 +24,7 @@ where
response.status()
);
}
log::info!("Imported recipe into cookbook: {}", url.as_ref());
}
Ok(())

View File

@ -31,6 +31,9 @@ pub async fn with(api_client: &ApiClient, calendar: &str, csv_file: &Path) -> Re
let recipe_urls = urls_from_csv(records.iter())?;
import::with(&api_client, recipe_urls.into_iter()).await?;
// Unfortunately, Nextcloud Cookbook doesn't return an id for imported recipes,
// so we have to resort to fetch all of them to match them
let recipes = get_all_recipes(&api_client).await?;
let events = records
@ -70,6 +73,7 @@ where
}
async fn get_all_recipes(api_client: &ApiClient) -> Result<HashMap<String, Rc<recipe::Recipe>>> {
log::info!("Getting list of all recipes");
let metadata = api_client
.rest()
.get(api_client.base_url().join("apps/cookbook/api/recipes")?)
@ -130,6 +134,11 @@ where
&helpers::ical_escape_text(&ev.recipe.name)
);
log::info!(
"Saving event at {} for '{}'",
&ev.ends_at.date(),
&ev.recipe.name
);
let cal = icalendar::Calendar::new()
.push::<icalendar::Event>(ev.into())
.done();

View File

@ -19,6 +19,8 @@ use {
#[tokio::main(flavor = "multi_thread")]
async fn main() -> Result<()> {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("info")).init();
let args = setup_args();
parse_args(&args).await
}