diff --git a/.forgejo/workflows/test.yaml b/.forgejo/workflows/test.yaml index 1fca279..6e0536f 100644 --- a/.forgejo/workflows/test.yaml +++ b/.forgejo/workflows/test.yaml @@ -1,14 +1,21 @@ on: [workflow_dispatch] jobs: integration-test--firefox: - runs-on: playwright-latest + runs-on: docker + container: + image: rust:trixie steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@v6 + - run: apt update && apt install sqlite3 + - run: echo test | ./Taskfile _cargo run -- set-password test + - run: ./Taskfile dev & + - run: bash ./e2e/wait-for-it.bash localhost:3000 --timeout 5 + - uses: actions/setup-node@v6 with: node-version: 24 - - uses: pnpm/action-setup@v4 + - uses: pnpm/action-setup@v6 with: version: 11.0.0-dev.1005 - run: cd e2e && pnpm install - run: cd e2e && env PROJECT_FILTER=firefox ./Taskfile _test + - run: kill $(pgrep -f 'Taskfile dev') diff --git a/Cargo.lock b/Cargo.lock index 79b598d..fb6500e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -141,6 +141,17 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.5.0" @@ -1043,6 +1054,15 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hex" version = "0.4.3" @@ -1563,6 +1583,7 @@ name = "mascarpone" version = "0.2.0" dependencies = [ "anyhow", + "atty", "axum", "axum-extra", "axum-htmx", diff --git a/Cargo.toml b/Cargo.toml index b348f60..a8cfc4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,7 @@ lto = true [dependencies] anyhow = "1.0.100" +atty = "0.2.14" axum = { version = "0.8.6", features = ["macros", "form"] } axum-extra = { version = "0.10.3", features = ["form"] } axum-htmx = "0.8.1" diff --git a/README.md b/README.md index fad7cb6..1e9edc7 100644 --- a/README.md +++ b/README.md @@ -51,13 +51,13 @@ If you want an account, contact me directly or use the "self-hosting" instructio ### Example systemd service file ``` [Unit] -Description=Mascarpone CRM +Description=Entretien Task Manager After=network.target [Service] Type=simple -WorkingDirectory=/var/local/mascarpone/ -ExecStart=/usr/bin/mascarpone serve +WorkingDirectory=/var/local/entretien/ +ExecStart=/usr/bin/entretien serve --port 6234 Restart=on-failure RestartSec=5 diff --git a/e2e/wait-for-it.bash b/e2e/wait-for-it.bash new file mode 100755 index 0000000..397f57e --- /dev/null +++ b/e2e/wait-for-it.bash @@ -0,0 +1,164 @@ +#!/usr/bin/env bash +# Use this script to test if a given TCP host/port are available + +WAITFORIT_cmdname=${0##*/} + +echoerr() { if [[ $WAITFORIT_QUIET -ne 1 ]]; then echo "$@" 1>&2; fi } + +usage() +{ + cat << USAGE >&2 +Usage: + $WAITFORIT_cmdname host:port [-s] [-t timeout] [-- command args] + -s | --strict Only execute subcommand if the test succeeds + -q | --quiet Don't output any status messages + -t TIMEOUT | --timeout=TIMEOUT + Timeout in seconds, zero for no timeout + -- COMMAND ARGS Execute command with args after the test finishes +USAGE + exit 1 +} + +wait_for() +{ + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + echoerr "$WAITFORIT_cmdname: waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + else + echoerr "$WAITFORIT_cmdname: waiting for $WAITFORIT_HOST:$WAITFORIT_PORT without a timeout" + fi + local start_ts + local result + local end_ts + start_ts=$(date +%s) + while :; do + if [[ $WAITFORIT_ISBUSY -eq 1 ]]; then + nc -z "$WAITFORIT_HOST" "$WAITFORIT_PORT" + result=$? + else + (echo -n > /dev/tcp/"$WAITFORIT_HOST"/"$WAITFORIT_PORT") >/dev/null 2>&1 + result=$? + fi + if [[ $result -eq 0 ]]; then + end_ts=$(date +%s) + echoerr "$WAITFORIT_cmdname: $WAITFORIT_HOST:$WAITFORIT_PORT is available after $((end_ts - start_ts)) seconds" + break + fi + sleep 1 + done + return "$result" +} + +wait_for_wrapper() +{ + # In order to support SIGINT during timeout: http://unix.stackexchange.com/a/57692 + if [[ $WAITFORIT_QUIET -eq 1 ]]; then + timeout $WAITFORIT_BUSYTIMEFLAG "$WAITFORIT_TIMEOUT" "$0" --quiet --child "$WAITFORIT_HOST:$WAITFORIT_PORT" --timeout="$WAITFORIT_TIMEOUT" & + else + timeout $WAITFORIT_BUSYTIMEFLAG "$WAITFORIT_TIMEOUT" "$0" --child "$WAITFORIT_HOST:$WAITFORIT_PORT" --timeout="$WAITFORIT_TIMEOUT" & + fi + WAITFORIT_PID=$! + # shellcheck disable=SC2064 + trap "kill -INT -$WAITFORIT_PID" INT + wait $WAITFORIT_PID + WAITFORIT_RESULT=$? + if [[ $WAITFORIT_RESULT -ne 0 ]]; then + echoerr "$WAITFORIT_cmdname: timeout occurred after waiting $WAITFORIT_TIMEOUT seconds for $WAITFORIT_HOST:$WAITFORIT_PORT" + fi + return $WAITFORIT_RESULT +} + +# process arguments +while [[ $# -gt 0 ]] +do + case "$1" in + *:* ) + IFS=: read -ra WAITFORIT_hostport <<< "$1" + WAITFORIT_HOST=${WAITFORIT_hostport[0]} + WAITFORIT_PORT=${WAITFORIT_hostport[1]} + shift 1 + ;; + --child) + WAITFORIT_CHILD=1 + shift 1 + ;; + -q | --quiet) + WAITFORIT_QUIET=1 + shift 1 + ;; + -s | --strict) + WAITFORIT_STRICT=1 + shift 1 + ;; + -t) + WAITFORIT_TIMEOUT="$2" + if [[ $WAITFORIT_TIMEOUT == "" ]]; then break; fi + shift 2 + ;; + --timeout=*) + WAITFORIT_TIMEOUT="${1#*=}" + shift 1 + ;; + --) + shift + WAITFORIT_CLI=("$@") + break + ;; + --help) + usage + ;; + *) + echoerr "Unknown argument: $1" + usage + ;; + esac +done + +if [[ "$WAITFORIT_HOST" == "" || "$WAITFORIT_PORT" == "" ]]; then + echoerr "Error: you need to provide a host and port to test." + usage +fi + +WAITFORIT_TIMEOUT=${WAITFORIT_TIMEOUT:-15} +WAITFORIT_STRICT=${WAITFORIT_STRICT:-0} +WAITFORIT_CHILD=${WAITFORIT_CHILD:-0} +WAITFORIT_QUIET=${WAITFORIT_QUIET:-0} + +# Check to see if timeout is from busybox? +WAITFORIT_TIMEOUT_PATH=$(type -p timeout) +WAITFORIT_TIMEOUT_PATH=$(realpath $WAITFORIT_TIMEOUT_PATH 2>/dev/null || readlink -f $WAITFORIT_TIMEOUT_PATH) + +WAITFORIT_BUSYTIMEFLAG="" +if [[ $WAITFORIT_TIMEOUT_PATH =~ "busybox" ]]; then + WAITFORIT_ISBUSY=1 + # Check if busybox timeout uses -t flag + # (recent Alpine versions don't support -t anymore) + if timeout &>/dev/stdout | grep -q -e '-t '; then + WAITFORIT_BUSYTIMEFLAG="-t" + fi +else + WAITFORIT_ISBUSY=0 +fi + +if [[ $WAITFORIT_CHILD -gt 0 ]]; then + wait_for + WAITFORIT_RESULT=$? + exit $WAITFORIT_RESULT +else + if [[ $WAITFORIT_TIMEOUT -gt 0 ]]; then + wait_for_wrapper + WAITFORIT_RESULT=$? + else + wait_for + WAITFORIT_RESULT=$? + fi +fi + +if [[ $WAITFORIT_CLI != "" ]]; then + if [[ $WAITFORIT_RESULT -ne 0 && $WAITFORIT_STRICT -eq 1 ]]; then + echoerr "$WAITFORIT_cmdname: strict mode, refusing to execute subprocess" + exit $WAITFORIT_RESULT + fi + exec "${WAITFORIT_CLI[@]}" +else + exit $WAITFORIT_RESULT +fi diff --git a/src/main.rs b/src/main.rs index 330c4a7..964a931 100644 --- a/src/main.rs +++ b/src/main.rs @@ -229,8 +229,13 @@ async fn main() -> Result<(), anyhow::Error> { db }; - let password = - rpassword::prompt_password(format!("New password for {}: ", username)).unwrap(); + let password: String = if atty::is(atty::Stream::Stdin) { + rpassword::prompt_password(format!("New password for {}: ", username)).unwrap() + } else { + let out = std::io::stdin().lines().next().unwrap().unwrap(); + println!("Received new password from stdin."); + out + }; let update = sqlx::query( "insert into users (username, password) values ($1, $2) on conflict do update set password=excluded.password",