Compare commits

...

28 commits

Author SHA1 Message Date
db523065e0 feat: remove openssl transitive dependency
All checks were successful
/ test (map[name:nightly]) (push) Successful in 43s
/ test (map[name:stable]) (push) Successful in 45s
2025-01-18 20:53:11 +00:00
3f87d011e7 feat: add dbus-dev to image
All checks were successful
/ test (map[name:nightly]) (push) Successful in 1m16s
/ test (map[name:stable]) (push) Successful in 1m21s
2025-01-18 20:25:51 +00:00
26c46227e6 feat: push-next uses 'latest' image
All checks were successful
/ test (map[name:nightly]) (push) Successful in 2m7s
/ test (map[name:stable]) (push) Successful in 1m19s
2025-01-16 08:40:00 +00:00
05d4233d3f build: add fmt recipe to justfile
All checks were successful
/ test (map[name:nightly]) (push) Successful in 53s
/ test (map[name:stable]) (push) Successful in 56s
2025-01-16 08:40:00 +00:00
2a9c2cef76 fix: add cargo components to toolchain stable-x86_64-unknown-linux-musl
All checks were successful
/ test (map[name:stable]) (push) Successful in 54s
/ test (map[name:nightly]) (push) Successful in 52s
2025-01-16 08:39:29 +00:00
ee426b1a43 fix: should build with kxio dependency
All checks were successful
/ test (map[name:stable]) (push) Successful in 52s
/ test (map[name:nightly]) (push) Successful in 51s
2025-01-16 08:39:28 +00:00
97f794a36e build: add clippy recipe to justfile
All checks were successful
/ test (map[name:stable]) (push) Successful in 42s
/ test (map[name:nightly]) (push) Successful in 40s
2025-01-16 07:41:45 +00:00
0c1f4ba125 docs: update readme to include caveat
All checks were successful
/ test (map[name:nightly]) (push) Successful in 40s
/ test (map[name:stable]) (push) Successful in 41s
2025-01-16 07:41:09 +00:00
f1a2ae0311 feat: install most apks later
All checks were successful
/ test (map[name:stable]) (push) Successful in 44s
/ test (map[name:nightly]) (push) Successful in 40s
2025-01-16 07:40:38 +00:00
e2f2015e06 test: add regression test for native-tls crate
All checks were successful
/ test (map[name:nightly]) (push) Successful in 39s
/ test (map[name:stable]) (push) Successful in 39s
2025-01-15 08:03:47 +00:00
4bd50bdadf build: justfile adds test and shell recipes
All checks were successful
/ test (map[name:nightly]) (push) Successful in 17s
/ test (map[name:stable]) (push) Successful in 53s
2025-01-15 08:02:08 +00:00
28d34eb85d fix: remove rust 1.74.1
All checks were successful
/ test (map[name:nightly]) (push) Successful in 13s
/ test (map[name:stable]) (push) Successful in 12s
2025-01-14 21:01:18 +00:00
2c8b4776d9 chore: use --no-chache and libssl3 + build-base packages
All checks were successful
/ test (map[name:nightly]) (push) Successful in 9s
/ test (map[name:1.74.1]) (push) Successful in 14s
/ test (map[name:stable]) (push) Successful in 13s
2025-01-14 20:45:55 +00:00
6aa876047a fix: should build with libssl dependency
All checks were successful
/ test (map[name:1.74.1]) (push) Successful in 10s
/ test (map[name:stable]) (push) Successful in 9s
/ test (map[name:nightly]) (push) Successful in 12s
2025-01-14 20:45:55 +00:00
9a12ffc5fc chore: add build recipe to justfile
All checks were successful
/ test (map[name:nightly]) (push) Successful in 4s
/ test (map[name:1.74.1]) (push) Successful in 5s
/ test (map[name:stable]) (push) Successful in 5s
2025-01-14 20:41:57 +00:00
7ab30ce7c3 feat: add bash to image
All checks were successful
/ test (map[name:1.74.1]) (push) Successful in 4s
/ test (map[name:nightly]) (push) Successful in 5s
/ test (map[name:stable]) (push) Successful in 4s
2025-01-14 18:08:14 +00:00
6d50289a35 build: replace debian base with alpine linux
All checks were successful
/ test (map[name:nightly]) (push) Successful in 5s
/ test (map[name:stable]) (push) Successful in 6s
/ test (map[name:1.74.1]) (push) Successful in 5s
2025-01-14 07:54:14 +00:00
3941fe6a1f build: use check-for-ignored
All checks were successful
/ test (map[name:nightly]) (push) Successful in 4s
/ test (map[name:stable]) (push) Successful in 4s
/ test (map[name:1.74.1]) (push) Successful in 41s
2025-01-13 22:10:30 +00:00
b853a6acec feat: add check-for-ignored script
All checks were successful
/ test (map[name:nightly]) (push) Successful in 4s
/ test (map[name:stable]) (push) Successful in 5s
/ test (map[name:1.74.1]) (push) Successful in 5s
Checks for ignored files that have been tracked by git.
2025-01-13 21:40:51 +00:00
5d372a60de build: check for ignored files being included in repo
All checks were successful
/ test (map[name:stable]) (push) Successful in 4s
/ test (map[name:1.74.1]) (push) Successful in 5s
/ test (map[name:nightly]) (push) Successful in 4s
2025-01-13 08:43:07 +00:00
1a89052244 build: use correct env var for auth
All checks were successful
/ test (map[name:1.74.1]) (push) Successful in 5s
/ test (map[name:nightly]) (push) Successful in 5s
/ test (map[name:stable]) (push) Successful in 4s
2025-01-12 19:44:16 +00:00
1f659c13ab build: fix typo
All checks were successful
/ test (map[name:1.74.1]) (push) Successful in 4s
/ test (map[name:nightly]) (push) Successful in 4s
/ test (map[name:stable]) (push) Successful in 4s
2025-01-12 19:15:37 +00:00
d6ea7a689e build: use env to get name of repo
All checks were successful
/ test (map[name:1.74.1]) (push) Successful in 39s
/ test (map[name:nightly]) (push) Successful in 42s
/ test (map[name:stable]) (push) Successful in 4s
2025-01-12 19:02:15 +00:00
5a5942c480 build: delete images safely and securily
All checks were successful
/ test (map[name:1.74.1]) (push) Successful in 37s
/ test (map[name:nightly]) (push) Successful in 5s
/ test (map[name:stable]) (push) Successful in 5s
- xargs -r -- only run the docker rmi command if there are images to delete
- --force  -- delete even if used
2025-01-12 18:57:54 +00:00
70e801e03c build: handle removing existing images when there are none
All checks were successful
/ test (map[name:nightly]) (push) Successful in 5s
/ test (map[name:stable]) (push) Successful in 4s
/ test (map[name:1.74.1]) (push) Successful in 4s
2025-01-12 18:51:25 +00:00
766156841b feat: drop entrypoint in favour of using 'run' over 'uses'
All checks were successful
/ test (map[name:1.74.1]) (push) Successful in 4s
/ test (map[name:nightly]) (push) Successful in 5s
/ test (map[name:stable]) (push) Successful in 4s
2025-01-12 11:48:56 +00:00
da790827d4 feat: drop entrypoint in favour of using 'run' over 'uses'
All checks were successful
/ test (map[name:stable]) (push) Successful in 4s
/ test (map[name:1.74.1]) (push) Successful in 3s
/ test (map[name:nightly]) (push) Successful in 4s
2025-01-12 11:40:56 +00:00
cb915104cb build: bump rust action to 2.6.0-rc3
All checks were successful
/ test (map[name:nightly]) (push) Successful in 6s
/ test (map[name:stable]) (push) Successful in 6s
/ test (map[name:v1.74.1]) (push) Successful in 5s
2025-01-11 20:19:14 +00:00
11 changed files with 311 additions and 132 deletions

View file

@ -13,14 +13,14 @@ jobs:
- name: Clear image from cache - name: Clear image from cache
run: | run: |
docker rmi git.kemitix.net/kemitix/rust:latest docker images git.kemitix.net/${{ env.GITHUB_REPOSITORY }} -q | sort -u | xargs -r docker rmi --force
docker system prune --force --all docker system prune --force --all
- name: Build - name: Build
run: docker build . -t git.kemitix.net/kemitix/rust:latest run: docker build . -t git.kemitix.net/${{ env.GITHUB_REPOSITORY }}:latest
- name: Login - name: Login
run: docker login --username kemitix --password ${{ secrets.FORGE_USER_PASSWORD }} git.kemitix.net run: docker login git.kemitix.net --username kemitix --password ${{ secrets.FORGEJO_TOKEN_WRITE_PACKAGE }}
- name: Publish - name: Publish
run: docker push git.kemitix.net/kemitix/rust:latest run: docker push git.kemitix.net/${{ env.GITHUB_REPOSITORY }}:latest

View file

@ -0,0 +1,46 @@
on:
push:
branches:
- next
jobs:
test:
runs-on: docker
container:
image:
git.kemitix.net/kemitix/rust:latest
strategy:
matrix:
toolchain:
- name: stable
- name: nightly
steps:
- uses: actions/checkout@v4
- name: Ignored Files
run: check-for-ignored
# run: |
# IGNORED=$(git ls-files --cached -i --exclude-standard)
# if [ -n "$IGNORED" ]
# then
# echo "Ignored files present:\n$IGNORED"
# exit 1
# fi
- name: Machete
run: cargo machete
- name: Format
run: cargo +${{ matrix.toolchain.name }} fmt --check
- name: Clippy
run: cargo +${{ matrix.toolchain.name }} clippy
- name: Test
run: cargo +${{ matrix.toolchain.name }} test
- name: Build
run: cargo +${{ matrix.toolchain.name }} build
- name: Run
run: cargo +${{ matrix.toolchain.name }} run

View file

@ -1,46 +0,0 @@
on:
push:
branches:
- next
jobs:
test:
runs-on: docker
strategy:
matrix:
toolchain:
- name: stable
- name: nightly
- name: v1.74.1
steps:
- uses: actions/checkout@v4
- name: Machete
uses: https://git.kemitix.net/kemitix/rust@v2.5.1
with:
args: cargo machete
- name: Format
uses: https://git.kemitix.net/kemitix/rust@v2.5.1
with:
args: ${{ matrix.toolchain.name }} cargo fmt --check
- name: Clippy
uses: https://git.kemitix.net/kemitix/rust@v2.5.1
with:
args: ${{ matrix.toolchain.name }} cargo clippy
- name: Test
uses: https://git.kemitix.net/kemitix/rust@v2.5.1
with:
args: ${{ matrix.toolchain.name }} cargo test
- name: Build
uses: https://git.kemitix.net/kemitix/rust@v2.5.1
with:
args: ${{ matrix.toolchain.name }} cargo build
- name: Run
uses: https://git.kemitix.net/kemitix/rust@v2.5.1
with:
args: ${{ matrix.toolchain.name }} cargo run

View file

@ -4,3 +4,5 @@ version = "0.1.0"
edition = "2021" edition = "2021"
[dependencies] [dependencies]
kxio = "5.1"
tokio = { version = "1.43", features = ["full"] }

View file

@ -1,20 +1,8 @@
FROM docker.io/rust:1.84.0-slim-bookworm FROM docker.io/rust:1.84.0-alpine3.21
LABEL org.opencontainers.image.source=https://git.kemitix.net/kemitix/rust LABEL org.opencontainers.image.source=https://git.kemitix.net/kemitix/rust
# nodejs - runtime used by forgejo/github actions RUN apk add --no-cache curl=8.11.1-r0
# curl - to download cargo-binstall
# clang-16 & mold - faster linkers for rust
# pkg-config - required to compile some rust `-sys` packages
# libssl-dev - build dependency for git-next
# libdbus-1-dev - linux os interop (e.g. desktop notifications)
# git - git
RUN apt-get update \
&& \
apt-get satisfy -y "nodejs (>=18.19.0), curl (>=7.88.1), pkg-config (>=1.8.1), libssl-dev (>=3.0.14), git (>=2.39.2), libdbus-1-dev (>= 1.14.10), libtag1-dev (>= 1.13), libtagc0-dev (>= 1.13), xorg-dev (>=7.7), libxcb-shape0-dev (>=0.15), libxcb-xfixes0-dev (>=0.15)" \
&& \
rm -r /var/lib/apt/lists/*
RUN curl -L https://github.com/cargo-bins/cargo-binstall/releases/download/v1.10.19/cargo-binstall-x86_64-unknown-linux-musl.tgz -o cargo-binstall.tgz && \ RUN curl -L https://github.com/cargo-bins/cargo-binstall/releases/download/v1.10.19/cargo-binstall-x86_64-unknown-linux-musl.tgz -o cargo-binstall.tgz && \
tar -xzf cargo-binstall.tgz && \ tar -xzf cargo-binstall.tgz && \
rm cargo-binstall.tgz && \ rm cargo-binstall.tgz && \
@ -28,17 +16,41 @@ RUN cargo binstall -y \
release-plz@0.3 release-plz@0.3
# should be a no-op if the FROM line is up-to-date # should be a no-op if the FROM line is up-to-date
RUN rustup update stable && rustup component add --toolchain stable rustfmt clippy RUN rustup update stable
RUN rustup component add --toolchain stable-x86_64-unknown-linux-musl rustfmt clippy
# install nightly # install nightly
RUN rustup install nightly && rustup component add --toolchain nightly rustfmt clippy RUN rustup install nightly && rustup component add --toolchain nightly rustfmt clippy
# install v1.74.1 # nodejs - runtime used by forgejo/github actions
RUN rustup install 1.74.1 && rustup component add --toolchain 1.74.1 rustfmt clippy # curl - to download cargo-binstall
# clang & mold - faster linkers for rust
# pkgconfig - required to compile some rust `-sys` packages
# openssl-dev - build dependency for git-next
# dbus-dev - linux os interop (e.g. desktop notifications)
# perl - native-tls(vendored)
# git - git
RUN apk add --no-cache \
bash \
nodejs \
build-base \
pkgconfig \
libssl3 \
openssl-dev \
perl \
dbus-dev \
git
COPY entrypoint.sh / # clang \
# mold \
# dbus-dev \
RUN git config --global user.email "action@git.kemitix.net" && \ RUN git config --global user.email "action@git.kemitix.net" && \
git config --global user.name "ForgeJo Action. See: https://git.kemitix.net/kemitix/rust" git config --global user.name "ForgeJo Action. See: https://git.kemitix.net/kemitix/rust"
ENTRYPOINT [ "/entrypoint.sh" ] COPY scripts/ /usr/local/bin/
WORKDIR /app

View file

@ -11,23 +11,25 @@ on: [push]
jobs: jobs:
test: test:
runs-on: docker runs-on: docker
container:
image:
git.kemitix.net/kemitix/rust:v3.0.0
steps: steps:
- uses: https://git.kemitix.net/kemitix/rust@v2.4.0 - name: test with nightly
with: run: cargo +nightly test
args: nightly cargo test - name: build with v1.74.1
- uses: https://git.kemitix.net/kemitix/rust@v2.4.0 run: cargo +1.74.1 cargo build
with: - name: test with stable
args: v1.79.0 cargo build run: cargo test
``` ```
The `args` is one of the following: ## Toolchains
- <COMMAND> The available toolchain in the image are:
- [ nightly | stable | v1.xx.x ] <COMMAND>
`COMMAND` is the command you want to run. The optional prefix is the Rust toolchain, or version. Allowed values are `nightly`, `stable` or a Rust version. - `nightly`
- stable
Where the optional prefix is not given, the `stable` toolchain will be used. The `stable` version is currently `1.82.0`. - 1.74.1
## Contents ## Contents
@ -40,13 +42,53 @@ Where the optional prefix is not given, the `stable` toolchain will be used. The
- cargo-chef - cargo-chef
- cargo-hack - cargo-hack
- release-plz - release-plz
- dbus-dev
- perl
## Binary size ### Scripts
To reduce the size of the debug binary, add the following to the `.cargo/config.toml` file in you project. - `check-for-ignored`
Checks for files that are being tracked by Git but should be ignored according
to the `.gitignore` file.
#### Usage
```yaml
steps:
- name: Check for Ignored Files
run: check-for-ignored
```
## Caveats
### openssl
The alpine linux install doesn't build with this dependency. You can either compile `native-tls` with the `vendored` feature, or not use `openssl`.
#### vendoered native-tls
This crate *must* use the `vendored` feature in order to compile in the Alpine Linux image.
```toml ```toml
[profile.dev] native-tls = { version = "0.2", features = ["vendored"] }
debug = 0 ```
strip = "debuginfo"
#### Don't use `openssl`
Check that none of your dependencies require `openssl`:
```bash
cargo tree --edges normal -i openssl
```
This will list the tree of dependencies that are bringing in `openssl`.
If you do need ssl/tls, try using `rustls`. e.g.
```toml
reqwest = { version = "0.12", default-features = false, features = [
"json",
"rustls-tls",
] }
``` ```

View file

@ -1,45 +0,0 @@
#!/usr/bin/env bash
set -e
echo "INPUT_ARGS: ${INPUT_ARGS}"
# split input into an array
read -ra ARGS <<<"${INPUT_ARGS}"
# default toolchain
TOOLCHAIN="stable"
echo "Default toolchain: ${TOOLCHAIN}"
# if first parameter is 'nightly'...
if test "${ARGS[0]}" == "nightly"; then
TOOLCHAIN="nightly"
ARGS=("${ARGS[@]:1}")
fi
if test "${ARGS[0]}" == "stable"; then
TOOLCHAIN="stable" # redundant as this is the default
ARGS=("${ARGS[@]:1}")
fi
if [[ "${ARGS[0]}" == v1* ]]; then
TOOLCHAIN="${ARGS[0]:1}"
ARGS=("${ARGS[@]:1}")
fi
echo "Selected toolchain: ${TOOLCHAIN}"
if test "${ARGS[0]}" == "cargo";then
PRE_COMMAND="cargo +${TOOLCHAIN} "
else
PRE_COMMAND="${ARGS[0]}"
fi
ARGS=("${ARGS[@]:1}")
# ensure toolchain is up-to-date
# recombine remaining arguments
COMMAND=$(
IFS=" "
echo "${ARGS[*]}"
)
# execute command
echo ">>> ${PRE_COMMAND} ${COMMAND}"
${PRE_COMMAND} ${COMMAND}

19
justfile Normal file
View file

@ -0,0 +1,19 @@
image := "git.kemitix.net/kemitix/rust:test"
build:
docker build . -t {{ image }}
test: build
docker run --rm -v $PWD:/app/ {{ image }} cargo test
clippy: build
docker run --rm -v $PWD:/app/ {{ image }} cargo clippy
run: build
docker run --rm -v $PWD:/app/ {{ image }} cargo run
fmt: build
docker run --rm -v $PWD:/app/ {{ image }} cargo fmt
shell: build
docker run --rm -it -v $PWD:/app/ {{ image }} bash

30
scripts/check-for-ignored Executable file
View file

@ -0,0 +1,30 @@
#!/usr/bin/env bash
# Exit on error
set -e
# Check if git is installed
if ! command -v git &> /dev/null; then
echo "Error: git is not installed"
exit 1
fi
# Check if we're in a git repository
if ! git rev-parse --is-inside-work-tree &> /dev/null; then
echo "Error: not in a git repository"
exit 1
fi
echo "Checking for tracked files that should be ignored..."
# Find files that are both tracked and ignored
ignored_files=$(git ls-files --cached -i --exclude-standard)
if [[ -z "$ignored_files" ]]; then
echo "No tracked files are marked as ignored"
exit 0
else
echo "The following tracked files are marked as ignored:"
echo "$ignored_files"
exit 1
fi

105
src/kxio.rs Normal file
View file

@ -0,0 +1,105 @@
/// This is an example to show fetching a file from a webiste and saving to a file
///
/// The example consts of:
///
/// - The main program, in `main()` - demonstrates how to setup `kxio` for use in prod
/// - A test module - demonstrates how to use `kxio` in tests
/// - sample functions - showing how to use `kxio` the body of your program, and be testable
///
/// NOTE: running this program with `cargo run --example get` will create and delete the file
/// `example-readme.md` in the current directory.
use std::path::Path;
use kxio::fs::FileHandle;
// #[tokio::main]
pub async fn main() -> kxio::Result<()> {
// Create a `Net` object for making real network requests.
let net: kxio::net::Net = kxio::net::new();
// Create a `FileSystem` object for accessing files within the current directory.
// The object created will return a `PathTraveral` error result if there is an attempt to\
// access a file outside of this directory.
let current_dir = std::env::current_dir().map_err(kxio::fs::Error::Io)?;
let fs: kxio::fs::FileSystem = kxio::fs::new(current_dir);
// The URL we will fetch - the readme for this library.
let url = "https://git.kemitix.net/kemitix/kxio/raw/branch/main/README.md";
// Create a PathBuf to a file within the directory that the `fs` object has access to.
let file_path = fs.base().join("example-readme.md");
// Create a generic handle for the file. This doesn't open the file, and always succeeds.
let path = fs.path(&file_path);
// Other options are;
// `fs.file(&file_path)` - for a file
// `fs.dir(&dir_path)` - for a directory
// Checks if the path exists (whether a file, directory, etc)
if path.exists()? {
eprintln!("The file {path} already exists. Aborting!");
return Ok(());
}
// Passes a reference to the `fs` and `net` objects for use by your program.
// Your programs should not know whether they are handling a mock or the real thing.
// Any file or network access should be made using these handlers to be properly testable.
let file = download_and_save_to_file(url, &file_path, &fs, &net).await?;
read_file(&file)?;
delete_file(file)?;
Ok(())
}
/// An function that uses a `FileSystem` and a `Net` object to interact with the outside world.
async fn download_and_save_to_file(
url: &str,
file_path: &Path,
// The filesystem abstraction
fs: &kxio::fs::FileSystem,
// The network abstraction
net: &kxio::net::Net,
) -> kxio::Result<FileHandle> {
println!("fetching: {url}");
// Makes a GET request that can be mocked in a test
let response = net.get(url).header("key", "value").send().await?;
// As you can see, we use [reqwest] under the hood.
//
// If you need to create a more complex request than the [kxio] fluent API allows, you
// can create a request using [reqwest] and pass it to [net.send(request)].
let body = response.text().await?;
println!("fetched {} bytes", body.bytes().len());
// Uses the file system abstraction to create a handle for a file.
let file: kxio::fs::PathReal<kxio::fs::FileMarker> = fs.file(file_path);
println!("writing file: {file}");
// Writes the body to the file.
file.write(body)?;
Ok(file)
}
/// A function that reads the file contents
fn read_file(file: &FileHandle) -> kxio::Result<()> {
println!("reading file: {file}");
// Creates a `Reader` which loaded the file into memory.
let reader: kxio::fs::Reader = file.reader()?;
let contents: &str = reader.as_str()?;
println!("{contents}");
Ok(())
}
/// A function that deletes the file
fn delete_file(file: FileHandle) -> kxio::Result<()> {
println!("deleting file: {file}");
file.remove()?;
Ok(())
}

View file

@ -1,3 +1,17 @@
fn main() { //
mod kxio;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Hello, world!"); println!("Hello, world!");
let rt = tokio::runtime::Runtime::new()?;
Ok(rt.block_on(crate::kxio::main())?)
}
#[cfg(test)]
mod tests {
#[test]
fn passes() {
println!("passes okay");
}
} }