Skip to content

Commit

Permalink
feat: adds rust example (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgilman authored Sep 18, 2024
1 parent 468a8b0 commit 5c08d73
Show file tree
Hide file tree
Showing 9 changed files with 379 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Rust
target/
183 changes: 183 additions & 0 deletions examples/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions examples/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "hello"
version = "0.1.0"
edition = "2021"

[dependencies]
clap = "4.5.17"
68 changes: 68 additions & 0 deletions examples/rust/Earthfile
Original file line number Diff line number Diff line change
@@ -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}
23 changes: 23 additions & 0 deletions examples/rust/blueprint.cue
Original file line number Diff line number Diff line change
@@ -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
}
}
}
55 changes: 55 additions & 0 deletions examples/rust/cross.sh
Original file line number Diff line number Diff line change
@@ -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 <GOOS/GOARCH>"
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"
Binary file added examples/rust/release/hello
Binary file not shown.
Binary file added examples/rust/release/rust
Binary file not shown.
41 changes: 41 additions & 0 deletions examples/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -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::<String>("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, !");
}
}

0 comments on commit 5c08d73

Please sign in to comment.