|
1 | 1 | #!/bin/bash |
2 | 2 |
|
| 3 | +# Run Oasis CLI as defined in $1 and store its output to $2. |
| 4 | +# CLI execution is terminated when interaction is required (e.g. "Sign this transaction?" question). |
| 5 | + |
3 | 6 | set -euo pipefail |
4 | 7 |
|
5 | 8 | OASIS_CMD=./oasis |
6 | 9 | IN=$1 |
7 | 10 | OUT=$2 |
| 11 | +EXAMPLE_NAME=$(echo $(basename $IN) | cut -d "." -f 1) |
| 12 | +CFG_DIR="/tmp/cli_examples/${EXAMPLE_NAME}" |
| 13 | +CFG_FLAG="--config /tmp/cli_examples/${EXAMPLE_NAME}/cli.toml" |
| 14 | +RESTORE_CFG_DIR="${HOME}/.config/oasis.backup" |
| 15 | + |
| 16 | +# Prepare clean config file for example or take the example-specific one, if it exists. |
| 17 | +function init_cfg() { |
| 18 | + # Init config in the first scenario step only. |
| 19 | + if [ "$(basename $IN)" != "${EXAMPLE_NAME}.in" ] && [ "$(basename $IN)" != "${EXAMPLE_NAME}.0.in" ]; then |
| 20 | + return |
| 21 | + fi |
| 22 | + |
| 23 | + rm -rf "${CFG_DIR}" |
| 24 | + mkdir -p "$(dirname ${CFG_DIR})" |
| 25 | + |
| 26 | + # Check for example-specific config and copy it over. |
| 27 | + CUSTOM_CFG_DIR="$(dirname $IN)/${EXAMPLE_NAME}" |
| 28 | + if [ -d "${CUSTOM_CFG_DIR}" ]; then |
| 29 | + cp -r "${CUSTOM_CFG_DIR}" "${CFG_DIR}" |
| 30 | + return |
| 31 | + fi |
| 32 | + |
| 33 | + # Otherwise, generate a clean config file. |
| 34 | + USER_CFG_DIR="${HOME}/.config/oasis" |
| 35 | + if [ -d "${USER_CFG_DIR}" ]; then |
| 36 | + if [ -d "${RESTORE_CFG_DIR}" ]; then |
| 37 | + echo "error: cannot initialize config: restore config directory ${RESTORE_CFG_DIR} already exists. Please restore it into your ${USER_CFG_DIR} or remove it" |
| 38 | + exit 1 |
| 39 | + fi |
| 40 | + mv "${USER_CFG_DIR}" "${RESTORE_CFG_DIR}" |
| 41 | + fi |
| 42 | + |
| 43 | + # XXX: What is the simplest Oasis CLI command to generate initial config file? |
| 44 | + $OASIS_CMD network ls >/dev/null |
| 45 | + wait |
| 46 | + |
| 47 | + # Use the fresh config for our example. |
| 48 | + mv "${USER_CFG_DIR}" "${CFG_DIR}" |
| 49 | + |
| 50 | + # Restore the original config, if it existed. |
| 51 | + if [ -d "${RESTORE_CFG_DIR}" ]; then |
| 52 | + mv "${RESTORE_CFG_DIR}" "${CFG_DIR}" |
| 53 | + fi |
| 54 | +} |
| 55 | + |
| 56 | +init_cfg |
8 | 57 |
|
9 | | -# Runs Oasis CLI with command line arguments passed as $1 and stores output |
10 | | -# to $2. Oasis CLI is automatically terminated when "Sign this transaction" |
11 | | -# occurs on the output. |
| 58 | +CMD="$(sed "s#oasis#$OASIS_CMD#" $IN) ${CFG_FLAG}" |
12 | 59 |
|
13 | | -${OASIS_CMD} $(cat $IN) > $OUT & |
| 60 | +# Execute the Oasis CLI and store the PID. |
| 61 | +$CMD > $OUT & |
14 | 62 | PID=$! |
15 | 63 | while ! test -f $OUT || ! grep -q "Sign this transaction" "$OUT" && ps -p ${PID} > /dev/null |
16 | 64 | do |
|
0 commit comments