1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
//!  SPDX-License-Identifier: MIT
//!
//! Copyright (c) 2023, eunomia-bpf
//! All rights reserved.
//!
use oci_distribution::{manifest, secrets::RegistryAuth, Client, Reference};

use crate::error::{Error, Result};

pub struct PullArgs {
    pub write_file: String,
    pub image_url: String,
}

pub(super) async fn pull_wasm_from_registry(
    client: &mut Client,
    auth: &RegistryAuth,
    reference: &Reference,
) -> Result<Vec<u8>> {
    if let Some(img_data) = client
        .pull(reference, auth, vec![manifest::WASM_LAYER_MEDIA_TYPE])
        .await
        .map_err(|e| Error::OciPull(e.to_string()))?
        .layers
        .into_iter()
        .next()
        .map(|layer| layer.data)
    {
        Ok(img_data)
    } else {
        let repo_url = format!(
            "{}/{}:{}",
            reference.registry(),
            reference.repository(),
            reference.tag().unwrap_or("latest"),
        );
        Err(Error::OciPull(format!(
            "no data found in url: {}",
            repo_url
        )))
    }
}