-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
379 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Rust | ||
target/ |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, !"); | ||
} | ||
} |