Skip to content

Commit 5c08d73

Browse files
authored
feat: adds rust example (#2)
1 parent 468a8b0 commit 5c08d73

File tree

9 files changed

+379
-0
lines changed

9 files changed

+379
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Rust
2+
target/

examples/rust/Cargo.lock

Lines changed: 183 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/rust/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[package]
2+
name = "hello"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
[dependencies]
7+
clap = "4.5.17"

examples/rust/Earthfile

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
VERSION 0.8
2+
3+
IMPORT github.com/earthly/lib/rust:3.0.3 AS rust
4+
5+
deps:
6+
FROM rust:1.81
7+
8+
WORKDIR /work
9+
10+
COPY Cargo.lock Cargo.toml .
11+
COPY cross.sh .
12+
13+
DO rust+INIT
14+
15+
src:
16+
FROM +deps
17+
18+
COPY --keep-ts --dir src .
19+
20+
build:
21+
FROM +src
22+
23+
ARG USEROS
24+
ARG USERARCH
25+
26+
ARG OS=$USEROS
27+
ARG ARCH=$USERARCH
28+
ARG TARGET=$(./cross.sh ${OS}/${ARCH})
29+
30+
DO rust+CROSS --target ${TARGET}
31+
DO rust+COPY_OUTPUT --output=".*?/release/[^\./]+"
32+
33+
SAVE ARTIFACT ./target/${TARGET}/release/hello hello
34+
35+
release:
36+
FROM scratch
37+
38+
ARG TARGETOS
39+
ARG TARGETARCH
40+
ARG USERPLATFORM
41+
42+
COPY \
43+
--platform=$USERPLATFORM \
44+
(+build/hello \
45+
--OS=$TARGETOS \
46+
--ARCH=$TARGETARCH) bin/hello
47+
48+
SAVE ARTIFACT bin/hello hello
49+
50+
publish:
51+
FROM debian:bookworm-slim
52+
WORKDIR /workspace
53+
54+
ARG container="rust"
55+
ARG tag="latest"
56+
57+
ARG TARGETOS
58+
ARG TARGETARCH
59+
ARG USERPLATFORM
60+
61+
COPY \
62+
--platform=$USERPLATFORM \
63+
(+build/hello \
64+
--OS=$TARGETOS \
65+
--ARCH=$TARGETARCH) /usr/local/bin/hello
66+
67+
ENTRYPOINT ["/usr/local/bin/hello"]
68+
SAVE IMAGE ${container}:${tag}

examples/rust/blueprint.cue

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
version: "1.0.0"
2+
project: {
3+
name: "rust"
4+
ci: targets: {
5+
build: {
6+
privileged: true
7+
}
8+
release: {
9+
platforms: [
10+
"linux/amd64",
11+
"linux/arm64",
12+
]
13+
privileged: true
14+
}
15+
publish: {
16+
platforms: [
17+
"linux/amd64",
18+
"linux/arm64",
19+
]
20+
privileged: true
21+
}
22+
}
23+
}

examples/rust/cross.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
3+
# Function to map Go-style platform to Rust target triple
4+
map_go_to_rust() {
5+
local go_os=$1
6+
local go_arch=$2
7+
8+
case "$go_os/$go_arch" in
9+
linux/amd64)
10+
echo "x86_64-unknown-linux-gnu"
11+
;;
12+
linux/386)
13+
echo "i686-unknown-linux-gnu"
14+
;;
15+
linux/arm64)
16+
echo "aarch64-unknown-linux-gnu"
17+
;;
18+
linux/arm)
19+
echo "armv7-unknown-linux-gnueabihf"
20+
;;
21+
darwin/amd64)
22+
echo "x86_64-apple-darwin"
23+
;;
24+
darwin/arm64)
25+
echo "aarch64-apple-darwin"
26+
;;
27+
windows/amd64)
28+
echo "x86_64-pc-windows-msvc"
29+
;;
30+
windows/386)
31+
echo "i686-pc-windows-msvc"
32+
;;
33+
windows/arm64)
34+
echo "aarch64-pc-windows-msvc"
35+
;;
36+
*)
37+
echo "Unsupported GOOS/GOARCH combination: $go_os/$go_arch"
38+
exit 1
39+
;;
40+
esac
41+
}
42+
43+
# Check for correct number of arguments
44+
if [ "$#" -ne 1 ]; then
45+
echo "Usage: $0 <GOOS/GOARCH>"
46+
echo "Example: $0 linux/amd64"
47+
exit 1
48+
fi
49+
50+
# Split the input argument into GOOS and GOARCH
51+
input="$1"
52+
IFS='/' read -r go_os go_arch <<<"$input"
53+
54+
# Call the function to get the Rust target triple
55+
map_go_to_rust "$go_os" "$go_arch"

examples/rust/release/hello

995 KB
Binary file not shown.

examples/rust/release/rust

995 KB
Binary file not shown.

examples/rust/src/main.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use clap::{Arg, Command};
2+
3+
fn hello(name: &str) -> String {
4+
format!("Hello, {}!", name)
5+
}
6+
7+
fn main() {
8+
// Define the version dynamically using an environment variable set at compile time
9+
let version = env!("CARGO_PKG_VERSION");
10+
11+
// Initialize Clap Command
12+
let matches = Command::new("Hello CLI")
13+
.version(version)
14+
.about("A simple CLI to greet users")
15+
.arg(
16+
Arg::new("input")
17+
.help("The name to greet")
18+
.index(1)
19+
.required(false),
20+
)
21+
.get_matches();
22+
23+
// Get the input or default to "World"
24+
let input = matches.get_one::<String>("input").map(|s| s.as_str()).unwrap_or("World");
25+
26+
// Output the greeting
27+
println!("{}", hello(input));
28+
}
29+
30+
#[cfg(test)]
31+
mod tests {
32+
use super::*;
33+
34+
// Test for the hello function
35+
#[test]
36+
fn test_hello() {
37+
assert_eq!(hello("Alice"), "Hello, Alice!");
38+
assert_eq!(hello("World"), "Hello, World!");
39+
assert_eq!(hello(""), "Hello, !");
40+
}
41+
}

0 commit comments

Comments
 (0)