aither_sandbox/spawn/
launch.rs1use std::borrow::Cow;
8
9use askama::Template;
10
11use crate::ContainerPath;
12
13#[derive(Template)]
14#[template(path = "tool_path.sh", escape = "none")]
15struct PathTemplate<'a> {
16 bin_dir: Cow<'a, str>,
17 exports: Vec<(&'a str, Cow<'a, str>)>,
18 script: &'a str,
19}
20
21#[derive(Template)]
22#[template(path = "managed_shell.sh", escape = "none")]
23struct ShellTemplate<'a> {
24 shell: Cow<'a, str>,
25 arguments: String,
26}
27
28fn with_path(
29 bin_dir: &str,
30 exports: &[(String, String)],
31 script: &str,
32) -> Result<String, askama::Error> {
33 PathTemplate {
34 bin_dir: shell_words::quote(bin_dir),
35 exports: exports
36 .iter()
37 .map(|(name, value)| (name.as_str(), shell_words::quote(value)))
38 .collect(),
39 script,
40 }
41 .render()
42}
43
44#[must_use]
47pub fn shell_arguments(script: &str) -> Vec<String> {
48 ["--noprofile", "--norc", "-c", script]
49 .map(Into::into)
50 .into()
51}
52
53#[derive(Debug, Clone, PartialEq, Eq)]
57pub struct HostLaunch {
58 shell: std::sync::Arc<str>,
59}
60
61impl HostLaunch {
62 #[must_use]
64 pub fn new(shell: impl Into<std::sync::Arc<str>>) -> Self {
65 Self {
66 shell: shell.into(),
67 }
68 }
69
70 #[must_use]
72 pub const fn shell(&self) -> &std::sync::Arc<str> {
73 &self.shell
74 }
75}
76
77#[derive(Debug, Clone, PartialEq, Eq)]
80pub struct ContainerLaunch {
81 bin_dir: ContainerPath,
82 shell: ContainerPath,
83}
84
85impl ContainerLaunch {
86 #[must_use]
88 pub const fn new(bin_dir: ContainerPath, shell: ContainerPath) -> Self {
89 Self { bin_dir, shell }
90 }
91
92 #[must_use]
94 pub const fn bin_dir(&self) -> &ContainerPath {
95 &self.bin_dir
96 }
97
98 pub fn wrap_script(&self, script: &str) -> Result<String, askama::Error> {
103 with_path(self.bin_dir.as_str(), &[], script)
104 }
105
106 pub fn shell_command(&self, script: &str) -> Result<String, askama::Error> {
111 ShellTemplate {
112 shell: shell_words::quote(self.shell.as_str()),
113 arguments: shell_words::join(shell_arguments(script)),
114 }
115 .render()
116 }
117}
118
119#[derive(Debug, Clone, PartialEq, Eq)]
123pub struct RemoteLaunch {
124 bin_dir: String,
125 shell: String,
126 exports: Vec<(String, String)>,
127 writable_dir: Option<String>,
128}
129
130impl RemoteLaunch {
131 #[must_use]
133 pub fn new(bin_dir: impl Into<String>, shell: impl Into<String>) -> Self {
134 Self {
135 bin_dir: bin_dir.into(),
136 shell: shell.into(),
137 exports: Vec::new(),
138 writable_dir: None,
139 }
140 }
141
142 #[must_use]
144 pub fn with_exports(mut self, exports: impl IntoIterator<Item = (String, String)>) -> Self {
145 self.exports.extend(exports);
146 self
147 }
148
149 #[must_use]
152 pub fn with_writable_dir(mut self, dir: impl Into<String>) -> Self {
153 self.writable_dir = Some(dir.into());
154 self
155 }
156
157 #[must_use]
159 pub fn bin_dir(&self) -> &str {
160 &self.bin_dir
161 }
162
163 #[must_use]
165 pub fn shell(&self) -> &str {
166 &self.shell
167 }
168
169 #[must_use]
171 pub fn writable_dir(&self) -> Option<&str> {
172 self.writable_dir.as_deref()
173 }
174
175 pub fn wrap_script(&self, script: &str) -> Result<String, askama::Error> {
181 with_path(&self.bin_dir, &self.exports, script)
182 }
183}