From 891312c0a6dd6b1004c19fb3fda65d0edec3c4c5 Mon Sep 17 00:00:00 2001 From: Erica Marigold Date: Sat, 30 Sep 2023 13:20:03 +0530 Subject: [PATCH] feat: include logging --- package/action/Cargo.lock | 123 +++++++++++++++++++++++++++++++++ package/action/Cargo.toml | 3 + package/action/src/download.rs | 68 +++++++++++++++++- package/action/src/main.rs | 18 ++++- 4 files changed, 207 insertions(+), 5 deletions(-) diff --git a/package/action/Cargo.lock b/package/action/Cargo.lock index 61c668c..78896ae 100644 --- a/package/action/Cargo.lock +++ b/package/action/Cargo.lock @@ -13,6 +13,9 @@ dependencies = [ "home", "serde", "serde_json", + "tracing", + "tracing-subscriber", + "tracing-unwrap", "ureq", "url", "zip", @@ -409,6 +412,16 @@ dependencies = [ "adler", ] +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "object" version = "0.32.1" @@ -430,6 +443,12 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "password-hash" version = "0.4.2" @@ -459,6 +478,12 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" +[[package]] +name = "pin-project-lite" +version = "0.2.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8afb450f006bf6385ca15ef45d71d2288452bc3683ce2e2cacc0d18e4be60b58" + [[package]] name = "pkg-config" version = "0.3.27" @@ -631,6 +656,21 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1b21f559e07218024e7e9f90f96f601825397de0e25420135f7f952453fed0b" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "smallvec" +version = "1.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "942b4a808e05215192e39f4ab80813e599068285906cc91aa64f923db842bd5a" + [[package]] name = "spin" version = "0.5.2" @@ -674,6 +714,16 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "time" version = "0.3.29" @@ -706,6 +756,73 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "tracing" +version = "0.1.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ce8c33a8d48bd45d624a6e523445fd21ec13d3653cd51f681abf67418f54eb8" +dependencies = [ + "cfg-if", + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.26" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f4f31f56159e98206da9efd823404b79b6ef3143b4a7ab76e67b1751b25a4ab" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ddad33d2d10b1ed7eb9d1f518a5674713876e97e5bb9b7345a7984fbb4f922" +dependencies = [ + "lazy_static", + "log", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", +] + +[[package]] +name = "tracing-unwrap" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77934bd2f10b38daeb37d0e24fb94f7bb83e08b474a0568d6ba71876c797c02e" +dependencies = [ + "tracing", +] + [[package]] name = "typenum" version = "1.17.0" @@ -775,6 +892,12 @@ dependencies = [ "getrandom", ] +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" diff --git a/package/action/Cargo.toml b/package/action/Cargo.toml index a78388e..0148305 100644 --- a/package/action/Cargo.toml +++ b/package/action/Cargo.toml @@ -13,6 +13,9 @@ directories = "5.0.1" home = "0.5.5" serde = { version = "1.0.188", features = ["derive"] } serde_json = "1.0.107" +tracing = "0.1.37" +tracing-subscriber = "0.3.17" +tracing-unwrap = "0.10.0" ureq = "2.7.1" url = "2.4.1" zip = "0.6.6" diff --git a/package/action/src/download.rs b/package/action/src/download.rs index a858772..599da59 100644 --- a/package/action/src/download.rs +++ b/package/action/src/download.rs @@ -17,12 +17,21 @@ use crate::{ }; fn parse_release_data(data: &LuneReleaseData) -> LuneReleaseFetched { + const TARGET: &str = "download::parse_release_data"; + + tracing::info!(target: TARGET, "Parsing received LuneReleaseData"); + let unknown = &String::from("UNKNOWN"); let mut version = unknown.to_owned(); let mut download = unknown.to_owned(); let mut artifact_name = unknown.to_owned(); + tracing::info!( + target: TARGET, + "Defaulting to `unknown` values before parsing" + ); + for asset in &data.assets { let parts = asset .name @@ -36,12 +45,25 @@ fn parse_release_data(data: &LuneReleaseData) -> LuneReleaseFetched { // [3] => arch .. ".zip" if parts[2] == env::consts::OS && parts[3].trim_end_matches(".zip") == env::consts::ARCH { + tracing::info!( + target: TARGET, + "Supported platform found, overwriting `unknown` values" + ); + version = (&parts[1]).to_owned(); download = (&asset.browser_download_url).to_owned(); artifact_name = (&asset.name).to_owned(); + } else { + tracing::warn!( + target: TARGET, + "Supported platform not found for asset {:?}", + asset + ); } } + tracing::info!(target: TARGET, "Done."); + LuneReleaseFetched { version, download, @@ -51,10 +73,24 @@ fn parse_release_data(data: &LuneReleaseData) -> LuneReleaseFetched { } pub fn download_release() -> Result<(PathBuf, LuneReleaseFetched)> { + const TARGET: &str = "download::download_release"; + + tracing::info!(target: TARGET, "Initializing routine"); + let parsed_release_data = parse_release_data(&GitHub::new(("filiptibell", "lune")).fetch_releases()?); + tracing::info!(target: TARGET, "Received API resp and parsed release data"); + tracing::info!(target: TARGET, "Got lune {}", parsed_release_data.version); + let fetcher = Fetcher::new(); + + tracing::info!( + target: TARGET, + "Proceeding to download release from API resp (url = {})", + &parsed_release_data.download + ); + let resp = fetcher.fetch::<_, ()>( Method::Get, Url::from_str(&parsed_release_data.download.as_str())?, @@ -63,6 +99,12 @@ pub fn download_release() -> Result<(PathBuf, LuneReleaseFetched)> { match resp { FetchResult::Response(res) => { + tracing::info!( + target: TARGET, + "Received download resp, proceeding to write zip to disk at {}", + &parsed_release_data.artifact_name + ); + let mut zip_archive = File::create(&parsed_release_data.artifact_name)?; let mut bytes = res @@ -71,7 +113,9 @@ pub fn download_release() -> Result<(PathBuf, LuneReleaseFetched)> { .map(|elem| elem.unwrap()) .collect::>(); - zip_archive.write_all(&mut bytes)? + zip_archive.write_all(&mut bytes)?; + + tracing::info!(target: TARGET, "Successfully downloaded release"); } FetchResult::Result(_) => unreachable!("Fetcher returned Result instead of Response"), }; @@ -83,11 +127,19 @@ pub fn download_release() -> Result<(PathBuf, LuneReleaseFetched)> { } pub fn install_lune(lune_archive_reader: impl Read + Seek, meta: LuneReleaseFetched) -> Result<()> { + const TARGET: &str = "download::install_lune"; + + tracing::info!(target: TARGET, "Initializing routine"); + let dir_name = meta.artifact_name.trim_end_matches(".zip"); + tracing::info!(target: TARGET, "Proceeding to extract release zip"); + let mut lune_zip = ZipArchive::new(lune_archive_reader)?; lune_zip.extract(dir_name)?; + tracing::info!(target: TARGET, "Successfully extracted release zip"); + let bin_name = match env::consts::OS { "windows" => "lune.exe", "macos" | "linux" => "lune", @@ -103,14 +155,26 @@ pub fn install_lune(lune_archive_reader: impl Read + Seek, meta: LuneReleaseFetc .join("bin"); if !bin_base_dir.exists() { + tracing::warn!(target: TARGET, "bin_base_dir nonexistent, creating"); std::fs::create_dir(&bin_base_dir)?; } + tracing::warn!( + target: TARGET, + "macOS and windows support for this action is incomplete" + ); + // TODO: macOS and windows support let lune_bin_path = bin_base_dir.join(bin_name); File::create(&lune_bin_path)?; - std::fs::rename(PathBuf::from(dir_name).join(bin_name), lune_bin_path)?; + std::fs::rename(PathBuf::from(dir_name).join(bin_name), &lune_bin_path)?; + + tracing::info!( + target: TARGET, + "Installed lune bin to {}", + lune_bin_path.to_string_lossy() + ); Ok(()) } diff --git a/package/action/src/main.rs b/package/action/src/main.rs index 696f2aa..729b348 100644 --- a/package/action/src/main.rs +++ b/package/action/src/main.rs @@ -1,11 +1,21 @@ use std::fs::File; use action::download::{download_release, install_lune}; +use tracing::Level; +use tracing_unwrap::ResultExt; fn main() { - better_panic::install(); + if cfg!(debug_assertions) { + better_panic::install(); + } else { + // Check for is_debug in github actions + tracing_subscriber::fmt() + .with_max_level(Level::DEBUG) + .init(); + } - let (zip_path, meta) = download_release().expect("failed to download latest lune release"); + let (zip_path, meta) = + download_release().expect_or_log("failed to download latest lune release"); install_lune( File::open(&zip_path).expect( @@ -17,5 +27,7 @@ fn main() { ), meta, ) - .expect("failed to install lune. did we not have perms to write to the required directories?"); + .expect_or_log( + "failed to install lune. did we not have perms to write to the required directories?", + ); }