dagger_sdk/core/
engine.rs1use crate::core::DAGGER_ENGINE_VERSION;
2use crate::core::{
3 cli_session::CliSession, config::Config, connect_params::ConnectParams, downloader::Downloader,
4};
5use std::path::PathBuf;
6
7use super::cli_session::DaggerSessionProc;
8
9#[derive(Default)]
10pub struct Engine {}
11
12impl Engine {
13 pub fn new() -> Self {
14 Self {}
15 }
16
17 #[allow(clippy::wrong_self_convention)]
18 async fn from_cli(&self, cfg: &Config) -> eyre::Result<(ConnectParams, DaggerSessionProc)> {
19 let cli = Downloader::new(DAGGER_ENGINE_VERSION.into())
20 .get_cli()
21 .await?;
22
23 let cli_session = CliSession::new();
24
25 cli_session.connect(cfg, &cli).await
26 }
27
28 pub async fn start(
29 &self,
30 cfg: &Config,
31 ) -> eyre::Result<(ConnectParams, Option<DaggerSessionProc>)> {
32 tracing::info!("starting dagger-engine");
33
34 if let Ok(conn) = self.from_session_env().await {
35 return Ok((conn, None));
36 }
37
38 if let Ok((conn, child)) = self.from_local_cli(cfg).await {
39 return Ok((conn, Some(child)));
40 }
41
42 let (conn, proc) = self.from_cli(cfg).await?;
43
44 Ok((conn, Some(proc)))
45 }
46
47 #[allow(clippy::wrong_self_convention)]
48 async fn from_session_env(&self) -> eyre::Result<ConnectParams> {
49 let port = std::env::var("DAGGER_SESSION_PORT").map(|p| p.parse::<u64>())??;
50 let token = std::env::var("DAGGER_SESSION_TOKEN")?;
51
52 Ok(ConnectParams {
53 port,
54 session_token: token,
55 })
56 }
57
58 #[allow(clippy::wrong_self_convention)]
59 async fn from_local_cli(
60 &self,
61 cfg: &Config,
62 ) -> eyre::Result<(ConnectParams, DaggerSessionProc)> {
63 let bin: PathBuf = std::env::var("_EXPERIMENTAL_DAGGER_CLI_BIN")?.into();
64 let cli_session = CliSession::new();
65
66 cli_session.connect(cfg, &bin).await
67 }
68}