diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..efd50a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +# Rust +target/ diff --git a/examples/rust/Cargo.lock b/examples/rust/Cargo.lock new file mode 100644 index 0000000..c2e82e9 --- /dev/null +++ b/examples/rust/Cargo.lock @@ -0,0 +1,183 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anstream" +version = "0.6.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64e15c1ab1f89faffbf04a634d5e1962e9074f2741eef6d97f3c4e322426d526" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bec1de6f59aedf83baf9ff929c98f2ad654b97c9510f4e70cf6f661d49fd5b1" + +[[package]] +name = "anstyle-parse" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb47de1e80c2b463c735db5b217a0ddc39d612e7ac9e2e96a5aed1f57616c1cb" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d36fc52c7f6c869915e99412912f22093507da8d9e942ceaf66fe4b7c14422a" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bf74e1b6e971609db8ca7a9ce79fd5768ab6ae46441c572e46cf596f59e57f8" +dependencies = [ + "anstyle", + "windows-sys", +] + +[[package]] +name = "clap" +version = "4.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3e5a21b8495e732f1b3c364c9949b201ca7bae518c502c80256c96ad79eaf6ac" +dependencies = [ + "clap_builder", +] + +[[package]] +name = "clap_builder" +version = "4.5.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cf2dd12af7a047ad9d6da2b6b249759a22a7abc0f474c1dae1777afa4b21a73" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_lex" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1462739cb27611015575c0c11df5df7601141071f07518d56fcc1be504cbec97" + +[[package]] +name = "colorchoice" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3fd119d74b830634cea2a0f58bbd0d54540518a14397557951e79340abc28c0" + +[[package]] +name = "hello" +version = "0.1.0" +dependencies = [ + "clap", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_gnullvm", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" diff --git a/examples/rust/Cargo.toml b/examples/rust/Cargo.toml new file mode 100644 index 0000000..f98b839 --- /dev/null +++ b/examples/rust/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "hello" +version = "0.1.0" +edition = "2021" + +[dependencies] +clap = "4.5.17" diff --git a/examples/rust/Earthfile b/examples/rust/Earthfile new file mode 100644 index 0000000..4b750ac --- /dev/null +++ b/examples/rust/Earthfile @@ -0,0 +1,68 @@ +VERSION 0.8 + +IMPORT github.com/earthly/lib/rust:3.0.3 AS rust + +deps: + FROM rust:1.81 + + WORKDIR /work + + COPY Cargo.lock Cargo.toml . + COPY cross.sh . + + DO rust+INIT + +src: + FROM +deps + + COPY --keep-ts --dir src . + +build: + FROM +src + + ARG USEROS + ARG USERARCH + + ARG OS=$USEROS + ARG ARCH=$USERARCH + ARG TARGET=$(./cross.sh ${OS}/${ARCH}) + + DO rust+CROSS --target ${TARGET} + DO rust+COPY_OUTPUT --output=".*?/release/[^\./]+" + + SAVE ARTIFACT ./target/${TARGET}/release/hello hello + +release: + FROM scratch + + ARG TARGETOS + ARG TARGETARCH + ARG USERPLATFORM + + COPY \ + --platform=$USERPLATFORM \ + (+build/hello \ + --OS=$TARGETOS \ + --ARCH=$TARGETARCH) bin/hello + + SAVE ARTIFACT bin/hello hello + +publish: + FROM debian:bookworm-slim + WORKDIR /workspace + + ARG container="rust" + ARG tag="latest" + + ARG TARGETOS + ARG TARGETARCH + ARG USERPLATFORM + + COPY \ + --platform=$USERPLATFORM \ + (+build/hello \ + --OS=$TARGETOS \ + --ARCH=$TARGETARCH) /usr/local/bin/hello + + ENTRYPOINT ["/usr/local/bin/hello"] + SAVE IMAGE ${container}:${tag} \ No newline at end of file diff --git a/examples/rust/blueprint.cue b/examples/rust/blueprint.cue new file mode 100644 index 0000000..2f67161 --- /dev/null +++ b/examples/rust/blueprint.cue @@ -0,0 +1,23 @@ +version: "1.0.0" +project: { + name: "rust" + ci: targets: { + build: { + privileged: true + } + release: { + platforms: [ + "linux/amd64", + "linux/arm64", + ] + privileged: true + } + publish: { + platforms: [ + "linux/amd64", + "linux/arm64", + ] + privileged: true + } + } +} diff --git a/examples/rust/cross.sh b/examples/rust/cross.sh new file mode 100755 index 0000000..e6b942b --- /dev/null +++ b/examples/rust/cross.sh @@ -0,0 +1,55 @@ +#!/usr/bin/env bash + +# Function to map Go-style platform to Rust target triple +map_go_to_rust() { + local go_os=$1 + local go_arch=$2 + + case "$go_os/$go_arch" in + linux/amd64) + echo "x86_64-unknown-linux-gnu" + ;; + linux/386) + echo "i686-unknown-linux-gnu" + ;; + linux/arm64) + echo "aarch64-unknown-linux-gnu" + ;; + linux/arm) + echo "armv7-unknown-linux-gnueabihf" + ;; + darwin/amd64) + echo "x86_64-apple-darwin" + ;; + darwin/arm64) + echo "aarch64-apple-darwin" + ;; + windows/amd64) + echo "x86_64-pc-windows-msvc" + ;; + windows/386) + echo "i686-pc-windows-msvc" + ;; + windows/arm64) + echo "aarch64-pc-windows-msvc" + ;; + *) + echo "Unsupported GOOS/GOARCH combination: $go_os/$go_arch" + exit 1 + ;; + esac +} + +# Check for correct number of arguments +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + echo "Example: $0 linux/amd64" + exit 1 +fi + +# Split the input argument into GOOS and GOARCH +input="$1" +IFS='/' read -r go_os go_arch <<<"$input" + +# Call the function to get the Rust target triple +map_go_to_rust "$go_os" "$go_arch" diff --git a/examples/rust/release/hello b/examples/rust/release/hello new file mode 100755 index 0000000..58bfd27 Binary files /dev/null and b/examples/rust/release/hello differ diff --git a/examples/rust/release/rust b/examples/rust/release/rust new file mode 100755 index 0000000..36e84c4 Binary files /dev/null and b/examples/rust/release/rust differ diff --git a/examples/rust/src/main.rs b/examples/rust/src/main.rs new file mode 100644 index 0000000..3bc3429 --- /dev/null +++ b/examples/rust/src/main.rs @@ -0,0 +1,41 @@ +use clap::{Arg, Command}; + +fn hello(name: &str) -> String { + format!("Hello, {}!", name) +} + +fn main() { + // Define the version dynamically using an environment variable set at compile time + let version = env!("CARGO_PKG_VERSION"); + + // Initialize Clap Command + let matches = Command::new("Hello CLI") + .version(version) + .about("A simple CLI to greet users") + .arg( + Arg::new("input") + .help("The name to greet") + .index(1) + .required(false), + ) + .get_matches(); + + // Get the input or default to "World" + let input = matches.get_one::("input").map(|s| s.as_str()).unwrap_or("World"); + + // Output the greeting + println!("{}", hello(input)); +} + +#[cfg(test)] +mod tests { + use super::*; + + // Test for the hello function + #[test] + fn test_hello() { + assert_eq!(hello("Alice"), "Hello, Alice!"); + assert_eq!(hello("World"), "Hello, World!"); + assert_eq!(hello(""), "Hello, !"); + } +} \ No newline at end of file