Skip to content

Commit 6419fc5

Browse files
committed
feat(examples): Add multiple configs support
1 parent b7d2d4e commit 6419fc5

File tree

8 files changed

+98
-16
lines changed

8 files changed

+98
-16
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ build:
1717

1818
examples: build $(EXAMPLES)
1919

20-
examples/%.out: examples/%.in
20+
examples/%.out: examples/%.in $(shell find -name "*.go" -type f) go.sum go.mod
2121
@rm -f $@
2222
@scripts/gen_example.sh $< $@
2323

examples/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Oasis CLI Examples
2+
3+
This folder contains a number of Oasis CLI invocations and expected outputs.
4+
The snippets are included in the documentation and serve as a check for
5+
potential regressions in the CI.
6+
7+
## Input files
8+
9+
The invocation parameters of each Oasis CLI example consists of one or more
10+
`<EXAMPLE_NAME>[.NUMBER].in` files. Each file begins with `oasis` command,
11+
which will be replaced with the actual path of the Oasis CLI command during
12+
execution. If multiple files exist for the same example name, they will be
13+
executed sequentially based on NUMBER and they will all share the config
14+
file. This way, you can prepare and execute a scenario of Oasis CLI
15+
invocations.
16+
17+
## Output files
18+
19+
The Oasis CLI output for the given input is stored in the corresponding
20+
`<EXAMPLE_NAME>[.NUMBER].out` file.
21+
22+
## Custom config files
23+
24+
Sometimes, you want to use the existing config file for the Oasis CLI. Put
25+
your desired Oasis CLI config folder inside `examples/<EXAMPLE_NAME>`. Upon
26+
example invocation, the folder will be copied over to a temporary location and
27+
provided to CLI with `--config` parameter.
28+
29+
## Example output generation
30+
31+
To run the examples and generate outputs, invoke
32+
33+
```sh
34+
make examples
35+
```

examples/account-deposit.0.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
oasis account deposit 10 test:dave --account test:alice
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ Format: plain
33
Method: consensus.Deposit
44
Body:
55
To: test:dave (oasis1qrk58a6j2qn065m6p06jgjyt032f7qucy5wqeqpt)
6-
Amount: 10.0 TEST
6+
Amount: 10.0 ROSE
77
Authorized signer(s):
88
1. NcPzNW3YU2T+ugNUtUWtoQnRvbOL9dYSaBfbjHLP1pE= (ed25519)
99
Nonce: 0
1010
Fee:
11-
Amount: 0.001131 TEST
11+
Amount: 0.001131 ROSE
1212
Gas limit: 11310
13-
(gas price: 0.0000001 TEST per gas unit)
13+
(gas price: 0.0000001 ROSE per gas unit)
1414

15-
Network: testnet
16-
ParaTime: sapphire
15+
Network: mainnet
16+
ParaTime: emerald
1717
Account: test:alice

examples/account-deposit.in

Lines changed: 0 additions & 1 deletion
This file was deleted.

examples/first-run.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
oasis

examples/first-run.out

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@ Available Commands:
99
completion Generate the autocompletion script for the specified shell
1010
contract WebAssembly smart contracts operations
1111
help Help about any command
12-
inspect Inspect the network
13-
network Manage network endpoints
14-
paratime Manage paratimes
15-
registry Registry operations
16-
tx Raw transaction operations
12+
network Consensus layer operations
13+
paratime ParaTime layer operations
14+
transaction Raw transaction operations
1715
wallet Manage accounts in the local wallet
1816

1917
Flags:

scripts/gen_example.sh

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,64 @@
11
#!/bin/bash
22

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+
36
set -euo pipefail
47

58
OASIS_CMD=./oasis
69
IN=$1
710
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
857

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}"
1259

13-
${OASIS_CMD} $(cat $IN) > $OUT &
60+
# Execute the Oasis CLI and store the PID.
61+
$CMD > $OUT &
1462
PID=$!
1563
while ! test -f $OUT || ! grep -q "Sign this transaction" "$OUT" && ps -p ${PID} > /dev/null
1664
do

0 commit comments

Comments
 (0)