Skip to content

Commit

Permalink
feat: allow users to run custom commands before exit
Browse files Browse the repository at this point in the history
  • Loading branch information
m90 committed Jul 4, 2024
1 parent d6a0383 commit c891b39
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM node:20-alpine

RUN apk add --no-cache jq=1.7.1-r0 && \
RUN apk add --no-cache jq=1.7.1-r0 curl=8.8.0-r0 && \
npm install -g wikibase-cli@17.0.8

COPY --chmod=755 ./transferbot.sh /usr/bin/transferbot
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ A Docker image providing a command that transfers entities from a source to a ta

```
docker run --rm ghcr.io/wbstack/transferbot \
-e CALLBACK_ON_SUCCESS="echo 'this worked'" -e CALLBACK_ON_FAILURE="echo 'this failed'" \
https://source.wikibase.cloud https://target.wikibase.cloud Q1 P1 Q2
```

Expand Down Expand Up @@ -36,6 +37,18 @@ The OAuth access token used for authenticating against the target wiki.

The OAuth access secret used for authenticating against the target wiki.

## Running commands after the script has finished

It's possible to provide commands that should be run when the script has finished.

### `CALLBACK_ON_SUCCESS` (optional)

If a value is set for this key, its content will be executed in case the script exits 0.

### `CALLBACK_ON_FAILURE` (optional)

If a value is set for this key, its content will be executed in case the script exits non-zero.

---

This work is distributed under the [BSD 3-Clause license](./LICENSE).
11 changes: 11 additions & 0 deletions transferbot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@
set -eu
set -o pipefail

trap finish EXIT INT TERM

finish () {
exit_code=$?;
if [ ! -z "${CALLBACK_ON_SUCCESS:-}" ] && [ "$exit_code" = 0 ]; then
sh -c "$CALLBACK_ON_SUCCESS"
elif [ ! -z "${CALLBACK_ON_FAILURE:-}" ] && [ "$exit_code" != 0 ]; then
sh -c "$CALLBACK_ON_FAILURE"
fi
}

source_wiki_origin="$1"
shift
target_wiki_origin="$1"
Expand Down

0 comments on commit c891b39

Please sign in to comment.