fluentci_graphql/schema/objects/
flox.rs

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