Skip to content

Commit

Permalink
Allow specifying only a subtarget for images in config
Browse files Browse the repository at this point in the history
  • Loading branch information
thirteenowls committed May 13, 2024
1 parent ea76d47 commit 350e314
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .changes/1491.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"description": "Allow specifying only a tag for images in config",
"description": "Allow specifying only a tag or subtarget for images in config",
"issues": [1169],
"type": "changed"
}
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ search = "<!-- next-url -->"
replace = "<!-- next-url -->\n\n[Unreleased]: https://github.com/cross-rs/{{crate_name}}/compare/v{{version}}...HEAD"
exactly = 1

[[package.metadata.release.pre-release-replacements]]
file = "docs/config_file.md"
search = "(# Translates to `.*?:).*?(-centos`)"
replace = "${1}{{version}}$2"
exactly = 1

[package.metadata.binstall]
pkg-url = "{ repo }/releases/download/v{ version }/{ name }-{ target }.tar.gz"
bin-dir = "{ bin }{ binary-ext }"
Expand Down
8 changes: 8 additions & 0 deletions docs/config_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,14 @@ image = ":edge"
image = "@sha256:77db671d8356a64ae72a3e1415e63f547f26d374fbe3c4762c1cd36c7eac7b99"
```

You can also specify a subtarget with no tag nor image name:

```toml
[target.x86_64-unknown-linux-gnu]
# Translates to `ghcr.io/cross-rs/x86_64-unknown-linux-gnu:0.3.0-centos`
image = "-centos"
```

The `image` key can also take the toolchains/platforms supported by the image:

```toml
Expand Down
28 changes: 26 additions & 2 deletions src/docker/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ use std::str::FromStr;

use serde::{Deserialize, Serialize};

use crate::{errors::*, shell::MessageInfo, TargetTriple};
use crate::{
docker::{CROSS_IMAGE, DEFAULT_IMAGE_VERSION},
errors::*,
shell::MessageInfo,
TargetTriple,
};

use super::Engine;

Expand Down Expand Up @@ -114,20 +119,39 @@ pub enum ImageReference {
Name(String),
/// Unqualified reference, only a tag or digest
Identifier(String),
/// Unqualified reference, only a subtarget
Subtarget(String),
}

impl ImageReference {
pub fn get(&self) -> &str {
match self {
Self::Name(s) => s,
Self::Identifier(s) => s,
Self::Subtarget(s) => s,
}
}

pub fn ensure_qualified(&mut self, target_name: &str) {
let image_name = match self {
Self::Name(_) => return,
Self::Identifier(id) => {
format!("{CROSS_IMAGE}/{target_name}{id}")
}
Self::Subtarget(sub) => {
format!("{CROSS_IMAGE}/{target_name}:{DEFAULT_IMAGE_VERSION}{sub}")
}
};

*self = Self::Name(image_name);
}
}

impl From<String> for ImageReference {
fn from(s: String) -> Self {
if s.starts_with(':') || s.starts_with('@') {
if s.starts_with('-') {
Self::Subtarget(s)
} else if s.starts_with(':') || s.starts_with('@') {
Self::Identifier(s)
} else {
Self::Name(s)
Expand Down
12 changes: 7 additions & 5 deletions src/docker/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::{env, fs, time};

use super::custom::{Dockerfile, PreBuild};
use super::image::{ImageReference, PossibleImage};
use super::image::PossibleImage;
use super::Image;
use super::PROVIDED_IMAGES;
use super::{engine::*, ProvidedImage};
Expand Down Expand Up @@ -1252,10 +1252,8 @@ fn get_user_image(
}

if let Some(image) = &mut image {
if let ImageReference::Identifier(id) = &image.reference {
let target_name = get_target_name(target, uses_zig);
image.reference = ImageReference::Name(format!("{CROSS_IMAGE}/{target_name}{id}"));
}
let target_name = get_target_name(target, uses_zig);
image.reference.ensure_qualified(target_name);
}

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

map.insert("CROSS_TARGET_X86_64_UNKNOWN_LINUX_GNU_IMAGE", "-centos");
let centos_tag = format!("{default_ver}-centos");
test(map.clone(), &centos_tag, &centos_tag)?;

map.insert("CROSS_TARGET_X86_64_UNKNOWN_LINUX_GNU_IMAGE", ":edge");
test(map.clone(), ":edge", ":edge")?;

Expand Down

0 comments on commit 350e314

Please sign in to comment.