fluentci_graphql/schema/objects/
envhub.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, GraphCommand};
6use fluentci_ext::envhub::Envhub as EnvhubExt;
7use fluentci_types::envhub as types;
8use uuid::Uuid;
9
10use super::service::Service;
11
12#[derive(Debug, Clone, Default)]
13pub struct Envhub {
14    pub id: ID,
15}
16
17#[Object]
18impl Envhub {
19    async fn id(&self) -> &ID {
20        &self.id
21    }
22
23    async fn r#use(&self, ctx: &Context<'_>, environment: String) -> Result<&Envhub, Error> {
24        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
25        let mut graph = graph.lock().unwrap();
26
27        let id = Uuid::new_v4().to_string();
28        let dep_id = graph.vertices[graph.size() - 1].id.clone();
29        let deps = match graph.size() {
30            1 => vec![],
31            _ => vec![dep_id],
32        };
33        graph.execute(GraphCommand::AddVertex(
34            id.clone(),
35            "useEnv".into(),
36            environment,
37            deps,
38            Arc::new(Box::new(EnvhubExt::default())),
39        ))?;
40
41        if graph.size() > 2 {
42            let x = graph.size() - 2;
43            let y = graph.size() - 1;
44            graph.execute(GraphCommand::AddEdge(x, y))?;
45        }
46
47        Ok(self)
48    }
49
50    async fn with_exec(&self, ctx: &Context<'_>, args: Vec<String>) -> Result<&Envhub, Error> {
51        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
52        common::with_exec(
53            graph.clone(),
54            args,
55            Arc::new(Box::new(EnvhubExt::default())),
56        )?;
57        Ok(self)
58    }
59
60    async fn with_workdir(&self, ctx: &Context<'_>, path: String) -> Result<&Envhub, Error> {
61        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
62        common::with_workdir(
63            graph.clone(),
64            path,
65            Arc::new(Box::new(EnvhubExt::default())),
66        )?;
67        Ok(self)
68    }
69
70    async fn with_service(&self, ctx: &Context<'_>, service: ID) -> Result<&Envhub, Error> {
71        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
72        common::with_service(graph.clone(), service.into())?;
73        Ok(self)
74    }
75
76    async fn with_cache(
77        &self,
78        ctx: &Context<'_>,
79        path: String,
80        cache: ID,
81    ) -> Result<&Envhub, Error> {
82        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
83        common::with_cache(graph.clone(), cache.into(), path)?;
84        Ok(self)
85    }
86
87    async fn with_file(
88        &self,
89        ctx: &Context<'_>,
90        path: String,
91        file_id: ID,
92    ) -> Result<&Envhub, Error> {
93        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
94        common::with_file(graph.clone(), file_id.into(), path)?;
95        Ok(self)
96    }
97
98    async fn stdout(&self, ctx: &Context<'_>) -> Result<String, Error> {
99        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
100        let rx = ctx.data::<Arc<Mutex<Receiver<(String, usize)>>>>().unwrap();
101        common::stdout(graph.clone(), rx.clone()).map_err(|e| Error::new(e.to_string()))
102    }
103
104    async fn stderr(&self, ctx: &Context<'_>) -> Result<String, Error> {
105        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
106        let rx = ctx.data::<Arc<Mutex<Receiver<(String, usize)>>>>().unwrap();
107        common::stderr(graph.clone(), rx.clone()).map_err(|e| Error::new(e.to_string()))
108    }
109
110    async fn as_service(&self, ctx: &Context<'_>, name: String) -> Result<Service, Error> {
111        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
112        let service = common::as_service(graph.clone(), name)?;
113        Ok(service.into())
114    }
115
116    async fn with_env_variable(
117        &self,
118        ctx: &Context<'_>,
119        name: String,
120        value: String,
121    ) -> Result<&Envhub, Error> {
122        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
123        common::with_env_variable(graph.clone(), &name, &value)?;
124        Ok(self)
125    }
126
127    async fn wait_on(
128        &self,
129        ctx: &Context<'_>,
130        port: u32,
131        timeout: Option<u32>,
132    ) -> Result<&Envhub, Error> {
133        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
134        common::wait_on(graph.clone(), port, timeout)?;
135        Ok(self)
136    }
137
138    async fn with_secret_variable(
139        &self,
140        ctx: &Context<'_>,
141        name: String,
142        secret: ID,
143    ) -> Result<&Envhub, Error> {
144        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
145        let g = graph.lock().unwrap();
146        let secret_name = g.secret_names.get(secret.as_str()).unwrap().clone();
147        drop(g);
148        common::with_secret_variable(graph.clone(), &name, secret.as_str(), &secret_name)?;
149        Ok(self)
150    }
151}
152
153impl From<types::Envhub> for Envhub {
154    fn from(envhub: types::Envhub) -> Self {
155        Self { id: ID(envhub.id) }
156    }
157}