-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathutils.rs
More file actions
113 lines (102 loc) · 3.86 KB
/
Copy pathutils.rs
File metadata and controls
113 lines (102 loc) · 3.86 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
use std::process::Command;
use crate::{cli::HostPort, core::platform::Architecture};
/// Formats and returns the image repository URI, eg. `oci.stackable.tech/sdp/opa`.
pub fn format_image_repository_uri(
image_registry: &HostPort,
registry_namespace: &str,
image_name: &str,
) -> String {
format!("{image_registry}/{registry_namespace}/{image_name}")
}
pub fn format_image_cache_repository_uri(
image_cache_registry: &HostPort,
cache_registry_namespace: Option<&str>,
registry_namespace: &str,
image_name: &str,
) -> String {
// We don't use .map here because we are unable to borrow the formatted string long enough
if let Some(cache_registry_namespace) = cache_registry_namespace {
format_image_repository_uri(image_cache_registry, cache_registry_namespace, image_name)
} else {
format_image_repository_uri(
image_cache_registry,
&format!("{}-cache", registry_namespace),
image_name,
)
}
}
/// Formats and returns the image manifest URI, eg. `oci.stackable.tech/sdp/opa:1.4.2-stackable25.7.0-amd64`.
pub fn format_image_manifest_uri(image_repository_uri: &str, image_manifest_tag: &str) -> String {
format!("{image_repository_uri}:{image_manifest_tag}")
}
/// Formats and returns the image index manifest tag, eg. `1.4.2-stackable25.7.0`.
pub fn format_image_index_manifest_tag(
image_version: &str,
vendor_tag_prefix: &str,
vendor_image_version: &str,
) -> String {
format!("{image_version}-{vendor_tag_prefix}{vendor_image_version}")
}
/// Formats and returns the image manifest tag, eg. `1.4.2-stackable25.7.0-amd64`.
///
/// The `strip_architecture` parameter controls if the architecture is included in the tag.
pub fn format_image_manifest_tag(
image_index_manifest_tag: &str,
// TODO (@Techassi): Maybe turn this into an Option to get rid of the bool
architecture: &Architecture,
strip_architecture: bool,
) -> String {
if strip_architecture {
image_index_manifest_tag.to_owned()
} else {
format!("{image_index_manifest_tag}-{architecture}")
}
}
// TODO (@Techassi): Can we design this better? Maybe add a new struct/type which implements a bunch
// of associated functions.
#[allow(clippy::too_many_arguments)]
pub fn format_image_tag_parts(
image_registry: &HostPort,
registry_namespace: &str,
image_name: &str,
image_version: &str,
vendor_tag_prefix: &str,
vendor_image_version: &str,
architecture: &Architecture,
strip_architecture: bool,
) -> (String, String, String, String) {
let image_repository_uri =
format_image_repository_uri(image_registry, registry_namespace, image_name);
let image_index_manifest_tag =
format_image_index_manifest_tag(image_version, vendor_tag_prefix, vendor_image_version);
let image_manifest_tag =
format_image_manifest_tag(&image_index_manifest_tag, architecture, strip_architecture);
let image_manifest_uri = format_image_manifest_uri(&image_repository_uri, &image_manifest_tag);
(
image_repository_uri,
image_index_manifest_tag,
image_manifest_tag,
image_manifest_uri,
)
}
/// Formats and returns the registry-specific env var name, eg. `BOIL_REGISTRY_TOKEN_OCI_STACKABLE_TECH`.
pub fn format_registry_token_env_var_name(registry_uri: &str) -> String {
format!(
"BOIL_REGISTRY_TOKEN_{registry_uri}",
registry_uri = registry_uri.replace(['.', '-'], "_").to_uppercase()
)
}
pub trait CommandExt {
/// Adds an argument to the command if the `predicate` is `true`.
fn arg_if<S>(&mut self, predicate: bool, arg: S) -> &mut Self
where
S: AsRef<std::ffi::OsStr>;
}
impl CommandExt for Command {
fn arg_if<S>(&mut self, predicate: bool, arg: S) -> &mut Self
where
S: AsRef<std::ffi::OsStr>,
{
if predicate { self.arg(arg) } else { self }
}
}