Signed-off-by: Lars Wirzenius <liw@liw.fi>
This commit is contained in:
Lars Wirzenius
2023-12-21 13:57:54 +02:00
parent 017bac2ca5
commit f3368ab05d
+42
View File
@@ -50,7 +50,11 @@ fn fallible_main() -> Result<(), NativeError> {
info!("radicle-native-ci starts");
let config = Config::load_via_env()?;
let mut logfile = config.open_log()?;
logfile.write("radicle-native-ci starts".into())?;
let (run_id, run_dir) = mkdir_run(&config)?;
logfile.write(format!("created run directory {}", run_dir.display()))?;
let src = run_dir.join("src");
let log = run_dir.join("log");
@@ -229,6 +233,9 @@ struct Config {
/// Directory where per-run directories are stored. Each run gets
/// its own dedicated subdirectory.
state: PathBuf,
/// File where native CI should write a log.
log: PathBuf,
}
impl Config {
@@ -241,6 +248,10 @@ impl Config {
Ok(config)
}
fn open_log(&self) -> Result<LogFile, NativeError> {
LogFile::open(&self.log)
}
/// Read configuration specification from a file.
fn read(filename: &Path) -> Result<Self, NativeError> {
let file = std::fs::File::open(filename)
@@ -251,6 +262,31 @@ impl Config {
}
}
struct LogFile {
filename: PathBuf,
file: std::fs::File,
}
impl LogFile {
fn open(filename: &Path) -> Result<Self, NativeError> {
let file = std::fs::OpenOptions::new()
.append(true)
.create(true)
.open(filename)
.map_err(|e| NativeError::OpenLogFile(filename.into(), e))?;
Ok(Self {
filename: filename.into(),
file,
})
}
fn write(&mut self, msg: String) -> Result<(), NativeError> {
self.file
.write_all(msg.as_bytes())
.map_err(|e| NativeError::WriteLogFile(self.filename.clone(), e))
}
}
/// How to run CI for this repository.
#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
@@ -323,4 +359,10 @@ enum NativeError {
#[error("failed to load Radicle profile")]
LoadProfile(#[source] radicle::profile::Error),
#[error("failed to open log file {0}")]
OpenLogFile(PathBuf, #[source] std::io::Error),
#[error("failed to write to log file {0}")]
WriteLogFile(PathBuf, #[source] std::io::Error),
}