fluentci_graphql/schema/objects/
pkgx.rs

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