Skip to content

Commit 350e314

Browse files
committed
Allow specifying only a subtarget for images in config
1 parent ea76d47 commit 350e314

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

.changes/1491.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"description": "Allow specifying only a tag for images in config",
2+
"description": "Allow specifying only a tag or subtarget for images in config",
33
"issues": [1169],
44
"type": "changed"
55
}

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ search = "<!-- next-url -->"
8686
replace = "<!-- next-url -->\n\n[Unreleased]: https://github.com/cross-rs/{{crate_name}}/compare/v{{version}}...HEAD"
8787
exactly = 1
8888

89+
[[package.metadata.release.pre-release-replacements]]
90+
file = "docs/config_file.md"
91+
search = "(# Translates to `.*?:).*?(-centos`)"
92+
replace = "${1}{{version}}$2"
93+
exactly = 1
94+
8995
[package.metadata.binstall]
9096
pkg-url = "{ repo }/releases/download/v{ version }/{ name }-{ target }.tar.gz"
9197
bin-dir = "{ bin }{ binary-ext }"

docs/config_file.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,14 @@ image = ":edge"
234234
image = "@sha256:77db671d8356a64ae72a3e1415e63f547f26d374fbe3c4762c1cd36c7eac7b99"
235235
```
236236

237+
You can also specify a subtarget with no tag nor image name:
238+
239+
```toml
240+
[target.x86_64-unknown-linux-gnu]
241+
# Translates to `ghcr.io/cross-rs/x86_64-unknown-linux-gnu:0.3.0-centos`
242+
image = "-centos"
243+
```
244+
237245
The `image` key can also take the toolchains/platforms supported by the image:
238246

239247
```toml

src/docker/image.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@ use std::str::FromStr;
22

33
use serde::{Deserialize, Serialize};
44

5-
use crate::{errors::*, shell::MessageInfo, TargetTriple};
5+
use crate::{
6+
docker::{CROSS_IMAGE, DEFAULT_IMAGE_VERSION},
7+
errors::*,
8+
shell::MessageInfo,
9+
TargetTriple,
10+
};
611

712
use super::Engine;
813

@@ -114,20 +119,39 @@ pub enum ImageReference {
114119
Name(String),
115120
/// Unqualified reference, only a tag or digest
116121
Identifier(String),
122+
/// Unqualified reference, only a subtarget
123+
Subtarget(String),
117124
}
118125

119126
impl ImageReference {
120127
pub fn get(&self) -> &str {
121128
match self {
122129
Self::Name(s) => s,
123130
Self::Identifier(s) => s,
131+
Self::Subtarget(s) => s,
124132
}
125133
}
134+
135+
pub fn ensure_qualified(&mut self, target_name: &str) {
136+
let image_name = match self {
137+
Self::Name(_) => return,
138+
Self::Identifier(id) => {
139+
format!("{CROSS_IMAGE}/{target_name}{id}")
140+
}
141+
Self::Subtarget(sub) => {
142+
format!("{CROSS_IMAGE}/{target_name}:{DEFAULT_IMAGE_VERSION}{sub}")
143+
}
144+
};
145+
146+
*self = Self::Name(image_name);
147+
}
126148
}
127149

128150
impl From<String> for ImageReference {
129151
fn from(s: String) -> Self {
130-
if s.starts_with(':') || s.starts_with('@') {
152+
if s.starts_with('-') {
153+
Self::Subtarget(s)
154+
} else if s.starts_with(':') || s.starts_with('@') {
131155
Self::Identifier(s)
132156
} else {
133157
Self::Name(s)

src/docker/shared.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
55
use std::{env, fs, time};
66

77
use super::custom::{Dockerfile, PreBuild};
8-
use super::image::{ImageReference, PossibleImage};
8+
use super::image::PossibleImage;
99
use super::Image;
1010
use super::PROVIDED_IMAGES;
1111
use super::{engine::*, ProvidedImage};
@@ -1252,10 +1252,8 @@ fn get_user_image(
12521252
}
12531253

12541254
if let Some(image) = &mut image {
1255-
if let ImageReference::Identifier(id) = &image.reference {
1256-
let target_name = get_target_name(target, uses_zig);
1257-
image.reference = ImageReference::Name(format!("{CROSS_IMAGE}/{target_name}{id}"));
1258-
}
1255+
let target_name = get_target_name(target, uses_zig);
1256+
image.reference.ensure_qualified(target_name);
12591257
}
12601258

12611259
Ok(image)
@@ -1633,6 +1631,10 @@ mod tests {
16331631
let mut map = HashMap::new();
16341632
test(map.clone(), &default_ver, &default_ver)?;
16351633

1634+
map.insert("CROSS_TARGET_X86_64_UNKNOWN_LINUX_GNU_IMAGE", "-centos");
1635+
let centos_tag = format!("{default_ver}-centos");
1636+
test(map.clone(), &centos_tag, &centos_tag)?;
1637+
16361638
map.insert("CROSS_TARGET_X86_64_UNKNOWN_LINUX_GNU_IMAGE", ":edge");
16371639
test(map.clone(), ":edge", ":edge")?;
16381640

0 commit comments

Comments
 (0)