1use crate::DAGGER_ENGINE_VERSION;
2use crate::{
3 cli_session::CliSession, config::Config, connect_params::ConnectParams, downloader::Downloader,
4};
5
6pub struct Engine {}
7
8impl Engine {
9 pub fn new() -> Self {
10 Self {}
11 }
12
13 async fn from_cli(&self, cfg: &Config) -> eyre::Result<(ConnectParams, tokio::process::Child)> {
14 let cli = Downloader::new(DAGGER_ENGINE_VERSION.into())?
15 .get_cli()
16 .await?;
17
18 let cli_session = CliSession::new();
19
20 Ok(cli_session.connect(cfg, &cli).await?)
21 }
22
23 pub async fn start(
24 &self,
25 cfg: &Config,
26 ) -> eyre::Result<(ConnectParams, Option<tokio::process::Child>)> {
27 tracing::info!("starting dagger-engine");
28
29 if let Ok(conn) = self.from_session_env().await {
30 return Ok((conn, None));
31 }
32
33 let (conn, proc) = self.from_cli(cfg).await?;
34
35 Ok((conn, Some(proc)))
36 }
37
38 async fn from_session_env(&self) -> eyre::Result<ConnectParams> {
39 let port = std::env::var("DAGGER_SESSION_PORT").map(|p| p.parse::<u64>())??;
40 let token = std::env::var("DAGGER_SESSION_TOKEN")?;
41
42 Ok(ConnectParams {
43 port,
44 session_token: token,
45 })
46 }
47}