Standardize on tokio::fs

This commit is contained in:
Luc Perkins 2024-05-28 18:29:55 -03:00
parent 50c47c34eb
commit fabff97e46
No known key found for this signature in database
GPG key ID: 16DB1108FB591835
3 changed files with 30 additions and 18 deletions

View file

@ -8,3 +8,6 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[*.rs]
indent_size = 4

View file

@ -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");
}

View file

@ -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<std::fs::File> = None;
if let Some(nix_conf_path) = &args.nix_conf {
let mut nix_conf_file: Option<File> = 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&parallel-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<PathBuf>,
}
fn init_logging() -> Result<LogGuard> {
async fn init_logging() -> Result<LogGuard> {
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<LogGuard> {
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();