diff --git a/.editorconfig b/.editorconfig index a56f2d2..107273a 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,3 +8,6 @@ end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true + +[*.rs] +indent_size = 4 diff --git a/magic-nix-cache/src/api.rs b/magic-nix-cache/src/api.rs index c9437d2..9ad1c0c 100644 --- a/magic-nix-cache/src/api.rs +++ b/magic-nix-cache/src/api.rs @@ -6,6 +6,7 @@ use attic::nix_store::StorePath; use axum::{extract::Extension, routing::post, Json, Router}; use axum_macros::debug_handler; use serde::{Deserialize, Serialize}; +use tokio::fs::read_to_string; use super::State; use crate::error::{Error, Result}; @@ -114,7 +115,7 @@ async fn workflow_finish( // NOTE(cole-h): see `init_logging` if let Some(logfile) = &state.logfile { - let logfile_contents = std::fs::read_to_string(logfile)?; + let logfile_contents = read_to_string(logfile).await?; println!("Every log line throughout the lifetime of the program:"); println!("\n{logfile_contents}\n"); } diff --git a/magic-nix-cache/src/main.rs b/magic-nix-cache/src/main.rs index 385e9b0..7808342 100644 --- a/magic-nix-cache/src/main.rs +++ b/magic-nix-cache/src/main.rs @@ -23,7 +23,7 @@ mod util; use std::borrow::BorrowMut; use std::collections::HashSet; -use std::fs::{self, create_dir_all}; +use std::fs::Permissions; use std::io::Write; use std::net::SocketAddr; use std::os::unix::fs::PermissionsExt; @@ -35,7 +35,7 @@ use anyhow::{anyhow, Context, Result}; use axum::{extract::Extension, routing::get, Router}; use clap::Parser; use tempfile::NamedTempFile; -use tokio::fs::File; +use tokio::fs::{self, create_dir_all, File, OpenOptions}; use tokio::io::AsyncWriteExt; use tokio::process::Command; use tokio::sync::{oneshot, Mutex, RwLock}; @@ -176,7 +176,7 @@ struct StateInner { } async fn main_cli() -> Result<()> { - let guard = init_logging()?; + let guard = init_logging().await?; let _tracing_guard = guard.appender_guard; let args = Args::parse(); @@ -186,21 +186,24 @@ async fn main_cli() -> Result<()> { let metrics = Arc::new(telemetry::TelemetryReport::new()); - let mut nix_conf_file: Option = None; - - if let Some(nix_conf_path) = &args.nix_conf { + let mut nix_conf_file: Option = if let Some(nix_conf_path) = &args.nix_conf { if let Some(parent) = Path::new(&nix_conf_path).parent() { - create_dir_all(parent).with_context(|| "Creating parent directories of nix.conf")?; + create_dir_all(parent) + .await + .with_context(|| "Creating parent directories of nix.conf")?; } - nix_conf_file = Some( - std::fs::OpenOptions::new() + Some( + OpenOptions::new() .create(true) .append(true) .open(nix_conf_path) + .await .with_context(|| "Creating nix.conf")?, - ); - } + ) + } else { + None + }; let store = Arc::new(NixStore::connect()?); @@ -238,6 +241,7 @@ async fn main_cli() -> Result<()> { ) .as_bytes(), ) + .await .with_context(|| "Writing to nix.conf")?; } @@ -257,7 +261,7 @@ async fn main_cli() -> Result<()> { let gha_cache = if args.use_gha_cache { let credentials = if let Some(credentials_file) = &args.credentials_file { tracing::info!("Loading credentials from {:?}", credentials_file); - let bytes = fs::read(credentials_file).with_context(|| { + let bytes = fs::read(credentials_file).await.with_context(|| { format!( "Failed to read credentials file '{}'", credentials_file.display() @@ -288,6 +292,7 @@ async fn main_cli() -> Result<()> { if let Some(ref mut nix_conf_file) = nix_conf_file { nix_conf_file .write_all(format!("extra-substituters = http://{}?trusted=1&compression=zstd¶llel-compression=true&priority=1\n", args.listen).as_bytes()) + .await .with_context(|| "Writing to nix.conf")?; } @@ -324,7 +329,8 @@ async fn main_cli() -> Result<()> { .with_context(|| "Keeping the post-build hook")? .1; - fs::set_permissions(&path, fs::Permissions::from_mode(0o755)) + fs::set_permissions(&path, Permissions::from_mode(0o755)) + .await .with_context(|| "Setting permissions on the post-build hook")?; /* Copy the script to the Nix store so we know for sure that @@ -358,6 +364,7 @@ async fn main_cli() -> Result<()> { ) .as_bytes(), ) + .await .with_context(|| "Writing to nix.conf")?; } @@ -524,7 +531,7 @@ pub struct LogGuard { logfile: Option, } -fn init_logging() -> Result { +async fn init_logging() -> Result { let filter = EnvFilter::try_from_default_env().unwrap_or_else(|_| { #[cfg(debug_assertions)] return EnvFilter::new("info") @@ -542,12 +549,13 @@ fn init_logging() -> Result { let (guard, file_layer) = match std::env::var("RUNNER_DEBUG") { Ok(val) if val == "1" => { let logfile = debug_logfile(); - let file = std::fs::OpenOptions::new() + let file = OpenOptions::new() .create(true) .write(true) .truncate(true) - .open(&logfile)?; - let (nonblocking, guard) = tracing_appender::non_blocking(file); + .open(&logfile) + .await?; + let (nonblocking, guard) = tracing_appender::non_blocking(file.into_std().await); let file_layer = tracing_subscriber::fmt::layer() .with_writer(nonblocking) .pretty();