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