use crate::types::{RunnerConfig, RunnerConfigScope};
use coodev_runner::{Runner, Volume};
use std::{fs, path::PathBuf};
use uuid::Uuid;
pub fn create_runner(config: RunnerConfig, working_dir: &PathBuf) -> anyhow::Result<Runner> {
let github_auth = config.github_authorization;
let volumes_dir = working_dir.join("volumes");
let runner = Runner::builder()
.github_authorization(github_auth)
.working_dir(working_dir)
.build()?;
fs::create_dir_all(&volumes_dir).map_err(|e| {
log::error!("Error creating volumes dir: {}", e);
anyhow::anyhow!("Error creating volumes dir: {}", e)
})?;
for volume in config.volumes {
let filename = Uuid::new_v4().to_string();
let path = volumes_dir.join(filename);
fs::write(&path, volume.content).map_err(|e| {
log::error!("Error writing file: {}", e);
anyhow::anyhow!("Error writing file: {}", e)
})?;
let path = path
.to_str()
.ok_or_else(|| anyhow::anyhow!("Error getting path"))?;
let v = match volume.scope {
RunnerConfigScope::Repository(repo) => Volume::new(volume.key)
.repository(repo)
.path(path)
.build()?,
RunnerConfigScope::Organization(org) => {
Volume::new(volume.key).owner(org).path(path).build()?
}
RunnerConfigScope::Global => Volume::new(volume.key).path(path).build()?,
};
runner.register_volume(v);
}
for secret in config.secrets {
let secret = secret.try_into()?;
runner.register_secret(secret);
}
Ok(runner)
}