File tree Expand file tree Collapse file tree 9 files changed +379
-0
lines changed Expand file tree Collapse file tree 9 files changed +379
-0
lines changed Original file line number Diff line number Diff line change 1+ # Rust
2+ target /
Original file line number Diff line number Diff line change 1+ [package ]
2+ name = " hello"
3+ version = " 0.1.0"
4+ edition = " 2021"
5+
6+ [dependencies ]
7+ clap = " 4.5.17"
Original file line number Diff line number Diff line change 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 }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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 "
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments