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