fluentci_graphql/schema/objects/
directory.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;
6
7use fluentci_common::devbox::devbox as common_devbox;
8use fluentci_common::devenv::devenv as common_devenv;
9use fluentci_common::directory::entries;
10use fluentci_common::envhub::envhub as common_envhub;
11use fluentci_common::flox::flox as common_flox;
12use fluentci_common::hermit::hermit as common_hermit;
13use fluentci_common::mise::mise as common_mise;
14use fluentci_common::nix::nix as common_nix;
15use fluentci_common::pixi::pixi as common_pixi;
16use fluentci_common::pkgx::pkgx as common_pkgx;
17use fluentci_common::proto::proto as common_proto;
18use fluentci_ext::runner::Runner;
19use fluentci_types::{directory as types, nix};
20use uuid::Uuid;
21
22use crate::schema::objects::{envhub::Envhub, file::File, mise::Mise};
23
24use super::{
25    devbox::Devbox,
26    devenv::Devenv,
27    flox::Flox,
28    hermit::Hermit,
29    nix::{Nix, NixArgs},
30    pixi::Pixi,
31    pkgx::Pkgx,
32    proto::Proto,
33    service::Service,
34};
35
36#[derive(Debug, Clone, Default)]
37pub struct Directory {
38    pub id: ID,
39    pub path: String,
40}
41
42#[Object]
43impl Directory {
44    async fn id(&self) -> &ID {
45        &self.id
46    }
47
48    async fn path(&self) -> &str {
49        &self.path
50    }
51
52    async fn directory(&self, path: String) -> Result<Directory, Error> {
53        let id = Uuid::new_v4().to_string();
54        let directory = Directory { id: ID(id), path };
55        Ok(directory)
56    }
57
58    async fn entries(&self) -> Result<Vec<String>, Error> {
59        let path = self.path.clone();
60        entries(path).map_err(|e| Error::new(e.to_string()))
61    }
62
63    async fn devbox(&self, ctx: &Context<'_>) -> Result<Devbox, Error> {
64        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
65        let devbox = common_devbox(graph.clone(), false)?;
66        Ok(Devbox::from(devbox))
67    }
68
69    async fn devenv(&self, ctx: &Context<'_>) -> Result<Devenv, Error> {
70        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
71        let devenv = common_devenv(graph.clone(), false)?;
72        Ok(Devenv::from(devenv))
73    }
74
75    async fn flox(&self, ctx: &Context<'_>) -> Result<Flox, Error> {
76        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
77        let flox = common_flox(graph.clone(), false)?;
78        Ok(Flox::from(flox))
79    }
80
81    async fn hermit(&self, ctx: &Context<'_>) -> Result<Hermit, Error> {
82        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
83        let hermit = common_hermit(graph.clone(), false)?;
84        Ok(Hermit::from(hermit))
85    }
86
87    async fn nix(&self, ctx: &Context<'_>, args: Option<NixArgs>) -> Result<Nix, Error> {
88        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
89        let args: nix::NixArgs = args.unwrap_or_default().into();
90        let mut g = graph.lock().unwrap();
91        g.nix_args = args.clone();
92        drop(g);
93
94        let nix = common_nix(graph.clone(), false, args)?;
95        Ok(Nix::from(nix))
96    }
97
98    async fn pkgx(&self, ctx: &Context<'_>) -> Result<Pkgx, Error> {
99        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
100        let pkgx = common_pkgx(graph.clone(), false)?;
101        Ok(Pkgx::from(pkgx))
102    }
103
104    async fn pixi(&self, ctx: &Context<'_>) -> Result<Pixi, Error> {
105        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
106        let pixi = common_pixi(graph.clone(), false)?;
107        Ok(Pixi::from(pixi))
108    }
109
110    async fn proto(&self, ctx: &Context<'_>) -> Result<Proto, Error> {
111        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
112        let proto = common_proto(graph.clone(), false)?;
113        Ok(Proto::from(proto))
114    }
115
116    async fn mise(&self, ctx: &Context<'_>) -> Result<Mise, Error> {
117        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
118        let mise = common_mise(graph.clone(), false)?;
119        Ok(Mise::from(mise))
120    }
121
122    async fn envhub(&self, ctx: &Context<'_>) -> Result<Envhub, Error> {
123        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
124        let envhub = common_envhub(graph.clone(), false)?;
125        Ok(Envhub::from(envhub))
126    }
127
128    async fn with_workdir(&self, ctx: &Context<'_>, path: String) -> Result<&Directory, Error> {
129        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
130        common::with_workdir(graph.clone(), path, Arc::new(Box::new(Runner::default())))?;
131        Ok(self)
132    }
133
134    async fn with_exec(&self, ctx: &Context<'_>, args: Vec<String>) -> Result<&Directory, Error> {
135        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
136        common::with_exec(graph.clone(), args, Arc::new(Box::new(Runner::default())))?;
137        Ok(self)
138    }
139
140    async fn with_service(&self, ctx: &Context<'_>, service: ID) -> Result<&Directory, Error> {
141        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
142        common::with_service(graph.clone(), service.into())?;
143        Ok(self)
144    }
145
146    async fn with_cache(
147        &self,
148        ctx: &Context<'_>,
149        path: String,
150        cache: ID,
151    ) -> Result<&Directory, Error> {
152        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
153        common::with_cache(graph.clone(), cache.into(), path)?;
154        Ok(self)
155    }
156
157    async fn with_file(
158        &self,
159        ctx: &Context<'_>,
160        path: String,
161        file_id: ID,
162    ) -> Result<&Directory, Error> {
163        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
164        common::with_file(graph.clone(), file_id.into(), path)?;
165        Ok(self)
166    }
167
168    async fn stdout(&self, ctx: &Context<'_>) -> Result<String, Error> {
169        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
170        let rx = ctx.data::<Arc<Mutex<Receiver<(String, usize)>>>>().unwrap();
171        common::stdout(graph.clone(), rx.clone()).map_err(|e| Error::new(e.to_string()))
172    }
173
174    async fn stderr(&self, ctx: &Context<'_>) -> Result<String, Error> {
175        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
176        let rx = ctx.data::<Arc<Mutex<Receiver<(String, usize)>>>>().unwrap();
177        common::stderr(graph.clone(), rx.clone()).map_err(|e| Error::new(e.to_string()))
178    }
179
180    async fn zip(&self, ctx: &Context<'_>) -> Result<File, Error> {
181        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
182        let file = common::zip(graph.clone(), self.path.clone())?;
183        Ok(File::from(file))
184    }
185
186    async fn tar_czvf(&self, ctx: &Context<'_>) -> Result<File, Error> {
187        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
188        let file = common::tar_czvf(graph.clone(), self.path.clone())?;
189        Ok(File::from(file))
190    }
191
192    async fn as_service(&self, ctx: &Context<'_>, name: String) -> Result<Service, Error> {
193        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
194        let service = common::as_service(graph.clone(), name)?;
195        Ok(service.into())
196    }
197
198    async fn with_env_variable(
199        &self,
200        ctx: &Context<'_>,
201        name: String,
202        value: String,
203    ) -> Result<&Directory, Error> {
204        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
205        common::with_env_variable(graph.clone(), &name, &value)?;
206        Ok(self)
207    }
208
209    async fn wait_on(
210        &self,
211        ctx: &Context<'_>,
212        port: u32,
213        timeout: Option<u32>,
214    ) -> Result<&Directory, Error> {
215        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
216        common::wait_on(graph.clone(), port, timeout)?;
217        Ok(self)
218    }
219
220    async fn with_secret_variable(
221        &self,
222        ctx: &Context<'_>,
223        name: String,
224        secret: ID,
225    ) -> Result<&Directory, Error> {
226        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
227        let g = graph.lock().unwrap();
228        let secret_name = g.secret_names.get(secret.as_str()).unwrap().clone();
229        drop(g);
230        common::with_secret_variable(graph.clone(), &name, secret.as_str(), &secret_name)?;
231        Ok(self)
232    }
233}
234
235impl From<types::Directory> for Directory {
236    fn from(directory: types::Directory) -> Self {
237        Directory {
238            id: ID(directory.id),
239            path: directory.path,
240        }
241    }
242}