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
43
44
45
46
47
48
49
50
51
52
53
//!  SPDX-License-Identifier: MIT
//!
//! Copyright (c) 2023, eunomia-bpf
//! All rights reserved.
//!
use std::collections::HashMap;

use oci_distribution::{
    client::{Config, ImageLayer},
    manifest,
    secrets::RegistryAuth,
    Client, Reference,
};

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

/// Configuration for a pushing process
pub struct PushArgs {
    /// Local file path
    pub file: String,
    /// URL to push
    pub image_url: String,
}

// return the manifest url
pub(super) async fn push_wasm_to_registry(
    client: &mut Client,
    auth: &RegistryAuth,
    reference: &Reference,
    module: Vec<u8>,
    annotations: Option<HashMap<String, String>>,
) -> Result<String> {
    let layers = vec![ImageLayer::new(
        module,
        manifest::WASM_LAYER_MEDIA_TYPE.to_string(),
        None,
    )];

    let config = Config {
        data: b"{}".to_vec(),
        media_type: manifest::WASM_CONFIG_MEDIA_TYPE.to_string(),
        annotations: None,
    };

    let image_manifest = manifest::OciImageManifest::build(&layers, &config, annotations);

    let resp = client
        .push(reference, &layers, config, auth, Some(image_manifest))
        .await
        .map_err(|e| Error::OciPush(e.to_string()))?;

    Ok(resp.manifest_url)
}