fluentci_graphql/schema/objects/
hermit.rs

1use std::sync::{mpsc::Receiver, Arc, Mutex};
2
3use super::service::Service;
4use async_graphql::{Context, Error, Object, ID};
5use fluentci_common::{common, hermit as common_hermit};
6use fluentci_core::deps::Graph;
7use fluentci_ext::hermit::Hermit as HermitExt;
8use fluentci_types::hermit as types;
9
10#[derive(Debug, Clone, Default)]
11pub struct Hermit {
12    pub id: ID,
13}
14
15#[Object]
16impl Hermit {
17    async fn id(&self) -> &ID {
18        &self.id
19    }
20
21    async fn with_exec(&self, ctx: &Context<'_>, args: Vec<String>) -> Result<&Hermit, Error> {
22        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
23        common::with_exec(
24            graph.clone(),
25            args,
26            Arc::new(Box::new(HermitExt::default())),
27        )?;
28        Ok(self)
29    }
30
31    async fn with_workdir(&self, ctx: &Context<'_>, path: String) -> Result<&Hermit, Error> {
32        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
33        common::with_workdir(
34            graph.clone(),
35            path,
36            Arc::new(Box::new(HermitExt::default())),
37        )?;
38        Ok(self)
39    }
40
41    async fn with_service(&self, ctx: &Context<'_>, service: ID) -> Result<&Hermit, Error> {
42        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
43        common::with_service(graph.clone(), service.into())?;
44        Ok(self)
45    }
46
47    async fn with_cache(
48        &self,
49        ctx: &Context<'_>,
50        path: String,
51        cache: ID,
52    ) -> Result<&Hermit, Error> {
53        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
54        common::with_cache(graph.clone(), cache.into(), path)?;
55        Ok(self)
56    }
57
58    async fn with_file(
59        &self,
60        ctx: &Context<'_>,
61        path: String,
62        file_id: ID,
63    ) -> Result<&Hermit, Error> {
64        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
65        common::with_file(graph.clone(), file_id.into(), path)?;
66        Ok(self)
67    }
68
69    async fn with_packages(
70        &self,
71        ctx: &Context<'_>,
72        packages: Vec<String>,
73    ) -> Result<&Hermit, Error> {
74        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
75        common_hermit::with_packages(graph.clone(), packages)?;
76        Ok(self)
77    }
78
79    async fn install(&self, ctx: &Context<'_>) -> Result<&Hermit, Error> {
80        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
81        common_hermit::install(graph.clone())?;
82        Ok(self)
83    }
84
85    async fn stdout(&self, ctx: &Context<'_>) -> Result<String, Error> {
86        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
87        let rx = ctx.data::<Arc<Mutex<Receiver<(String, usize)>>>>().unwrap();
88        common::stdout(graph.clone(), rx.clone()).map_err(|e| Error::new(e.to_string()))
89    }
90
91    async fn stderr(&self, ctx: &Context<'_>) -> Result<String, Error> {
92        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
93        let rx = ctx.data::<Arc<Mutex<Receiver<(String, usize)>>>>().unwrap();
94        common::stderr(graph.clone(), rx.clone()).map_err(|e| Error::new(e.to_string()))
95    }
96
97    async fn as_service(&self, ctx: &Context<'_>, name: String) -> Result<Service, Error> {
98        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
99        let service = common::as_service(graph.clone(), name)?;
100        Ok(service.into())
101    }
102
103    async fn with_env_variable(
104        &self,
105        ctx: &Context<'_>,
106        name: String,
107        value: String,
108    ) -> Result<&Hermit, Error> {
109        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
110        common::with_env_variable(graph.clone(), &name, &value)?;
111        Ok(self)
112    }
113
114    async fn wait_on(
115        &self,
116        ctx: &Context<'_>,
117        port: u32,
118        timeout: Option<u32>,
119    ) -> Result<&Hermit, Error> {
120        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
121        common::wait_on(graph.clone(), port, timeout)?;
122        Ok(self)
123    }
124
125    async fn with_secret_variable(
126        &self,
127        ctx: &Context<'_>,
128        name: String,
129        secret: ID,
130    ) -> Result<&Hermit, Error> {
131        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
132        let g = graph.lock().unwrap();
133        let secret_name = g.secret_names.get(secret.as_str()).unwrap().clone();
134        drop(g);
135        common::with_secret_variable(graph.clone(), &name, secret.as_str(), &secret_name)?;
136        Ok(self)
137    }
138}
139
140impl From<types::Hermit> for Hermit {
141    fn from(hermit: types::Hermit) -> Self {
142        Self { id: ID(hermit.id) }
143    }
144}