Radish alpha
r
rad:z3qg5TKmN83afz2fj9z3fQjU8vaYE
Radicle CI adapter for native CI
Radicle
Git
log file
Lars Wirzenius committed 2 years ago
commit f3368ab05dd7ea75b4a65b4c8a2b072e36bc00bb
parent 017bac2
1 file changed +42 -0
modified src/main.rs
@@ -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),
}