fluentci_graphql/schema/objects/
nix.rs

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