coodev_runner/runner/
runner_builder.rs1use crate::{
2 actions::Actions,
3 runner::{GithubAuthorization, Runner, RunnerInner},
4};
5use parking_lot::Mutex;
6use std::{collections::HashMap, path::PathBuf, sync::Arc};
7use tokio::sync::Semaphore;
8
9use super::{MessageSender, PluginManager};
10
11#[derive(Debug, Clone, Default)]
12pub struct RunnerBuilder {
13 github_authorization: Option<GithubAuthorization>,
14 working_dir: Option<PathBuf>,
16 parallel_size: Option<usize>,
18}
19
20impl RunnerBuilder {
21 pub fn github_authorization(&mut self, github_authorization: GithubAuthorization) -> &mut Self {
22 self.github_authorization = Some(github_authorization);
23
24 self
25 }
26
27 pub fn working_dir(&mut self, working_dir: impl Into<PathBuf>) -> &mut Self {
28 self.working_dir = Some(working_dir.into());
29
30 self
31 }
32
33 pub fn parallel_size(&mut self, parallel_size: usize) -> &mut Self {
34 self.parallel_size = Some(parallel_size);
35
36 self
37 }
38
39 pub fn build(&self) -> anyhow::Result<Runner> {
40 let github_authorization = self.github_authorization.clone().ok_or(
41 anyhow::anyhow!("Github authorization is required. Please set github authorization by Runner::builder().github_authorization()"),
42 )?;
43 let parallel_size = self.parallel_size.unwrap_or(10);
44
45 let plugin_manager = PluginManager::new();
46 let runner = Runner {
47 github_authorization,
48 parallel_semaphore: Arc::new(Semaphore::new(parallel_size)),
49 working_dir: self.working_dir.clone(),
50 inner: Arc::new(Mutex::new(RunnerInner {
51 actions: Actions::new(),
52 secrets: HashMap::new(),
53 volumes: HashMap::new(),
54 workflow_message_sender: MessageSender::new(),
55 plugin_manager,
56 })),
57 };
58
59 Ok(runner)
60 }
61}