Skip to content

Commit 3ecc434

Browse files
authored
Added support for extensions (#4)
* Added support for extensions * Removed re-parse * Pass runtime instead of getting it from metadata
1 parent 565be66 commit 3ecc434

1 file changed

Lines changed: 60 additions & 9 deletions

File tree

src/main.rs

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -108,29 +108,34 @@ fn run(run: RunCommand, verbose: bool) -> anyhow::Result<ExitCode> {
108108
let available_runtimes =
109109
list_available_runtimes(&install_dirs).context("Could not list runtimes")?;
110110

111-
let (runtime, app_files_path) = match (&run.app, run.runtime) {
111+
let raw_app_metadata: Option<String>;
112+
let (runtime, app_files_path, app_metadata) = match (&run.app, run.runtime) {
112113
(Some(app), None) => {
113114
let app_path = find_install_path(app, true, &install_dirs)
114115
.context("Could not find app install dir")?
115116
.join("current")
116117
.join("active");
117118
let app_metadata_path = app_path.join("metadata");
118119

119-
let raw_app_metadata =
120-
fs::read_to_string(app_metadata_path).context("Could not read app metadata")?;
121-
let app_metadata =
122-
parse_keyfile(&raw_app_metadata).context("Could not parse app metadata")?;
120+
raw_app_metadata = Some(
121+
fs::read_to_string(app_metadata_path).context("Could not read app metadata")?,
122+
);
123+
let app_metadata = parse_keyfile(raw_app_metadata.as_ref().unwrap())
124+
.context("Could not parse app metadata")?;
123125

124126
let app_runtime = app_metadata
125127
.get("Application")
126128
.and_then(|app| app.get("runtime"))
127-
.context("Could not read app runtime")?;
129+
.context("Could not read app runtime")?
130+
.to_string();
128131

129132
let app_files_path = app_path.join("files");
130133

131-
(app_runtime.to_string(), Some(app_files_path))
134+
(app_runtime, Some(app_files_path), Some(app_metadata))
135+
}
136+
(None, Some(runtime)) => {
137+
(runtime, None, None)
132138
}
133-
(None, Some(runtime)) => (runtime, None),
134139
(Some(_), Some(_)) => bail!("Only app or runtime flags can be used at once"),
135140
(None, None) => bail!("Either app or runtime has to be specified"),
136141
};
@@ -165,6 +170,16 @@ fn run(run: RunCommand, verbose: bool) -> anyhow::Result<ExitCode> {
165170
&install_dirs,
166171
)?;
167172

173+
if let Some(ref app_meta) = app_metadata {
174+
setup_app_extensions(
175+
&mut bwrap,
176+
app_meta,
177+
&runtime,
178+
&available_runtimes,
179+
&install_dirs,
180+
)?;
181+
}
182+
168183
add_ld_so_conf(&mut bwrap)?;
169184

170185
setup_env(&mut bwrap, runtime_env, run.app.as_deref());
@@ -283,6 +298,7 @@ fn setup_runtime_extensions(
283298
version,
284299
available_runtimes,
285300
install_dirs,
301+
Path::new("/usr"),
286302
)
287303
.with_context(|| format!("Could not set up extension {extension}"))?;
288304
}
@@ -291,6 +307,40 @@ fn setup_runtime_extensions(
291307
Ok(())
292308
}
293309

310+
fn setup_app_extensions(
311+
bwrap: &mut BwrapBuilder,
312+
app_metadata: &IndexMap<&str, IndexMap<&str, &str>>,
313+
runtime: &str,
314+
available_runtimes: &[String],
315+
install_dirs: &[PathBuf],
316+
) -> anyhow::Result<()> {
317+
let mut runtime_split = runtime.split('/').skip(1);
318+
let arch = runtime_split
319+
.next()
320+
.context("Could not extract architecture from runtime id")?;
321+
let version = runtime_split
322+
.next()
323+
.context("Could not extract version from runtime id")?;
324+
325+
for (group, metadata) in app_metadata {
326+
if let Some(extension) = group.strip_prefix(EXTENSION_PREFIX) {
327+
setup_extension(
328+
metadata,
329+
bwrap,
330+
extension,
331+
arch,
332+
version,
333+
available_runtimes,
334+
install_dirs,
335+
Path::new("/app"),
336+
)
337+
.with_context(|| format!("Could not set up app extension {extension}"))?;
338+
}
339+
}
340+
341+
Ok(())
342+
}
343+
294344
fn setup_extension(
295345
extension_metadata: &IndexMap<&str, &str>,
296346
bwrap: &mut BwrapBuilder,
@@ -299,13 +349,14 @@ fn setup_extension(
299349
runtime_version: &str,
300350
available_runtimes: &[String],
301351
install_dirs: &[PathBuf],
352+
base_path: &Path,
302353
) -> anyhow::Result<()> {
303354
let directory = extension_metadata
304355
.get("directory")
305356
.context("Missing directory")?;
306357

307358
let expected_prefix = format!("{name}.");
308-
let extension_base_mount_path = Path::new("/usr").join(directory);
359+
let extension_base_mount_path = base_path.join(directory);
309360

310361
let allowed_versions = extension_metadata
311362
.get("versions")

0 commit comments

Comments
 (0)