Skip to content

Commit

Permalink
fix: edge cases for running interactive goal commands with --tty flag
Browse files Browse the repository at this point in the history
  • Loading branch information
aorumbayev committed Oct 29, 2024
1 parent 1394416 commit bea004a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
5 changes: 5 additions & 0 deletions docs/cli/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
- [goal](#goal)
- [Options](#options-11)
- [--console](#--console)
- [--interactive](#--interactive)
- [Arguments](#arguments-6)
- [GOAL_ARGS](#goal_args)
- [init](#init)
Expand Down Expand Up @@ -591,6 +592,10 @@ algokit goal [OPTIONS] [GOAL_ARGS]...
### --console
Open a Bash console so you can execute multiple goal commands and/or interact with a filesystem.


### --interactive
Force running the goal command in interactive mode.

### Arguments


Expand Down
23 changes: 22 additions & 1 deletion docs/features/goal.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,19 @@ $ ~ algokit goal
```

## Working with Files in the Container

When interacting with the container, especially if you're using tools like goal, you might need to reference files or directories. Here's how to efficiently deal with files and directories:

### Automatic File Mounting

When you specify a file or directory path in your `goal` command, the system will automatically mount that path from your local filesystem into the container. This way, you don't need to copy files manually each time.

For instance, if you want to compile a `teal` file:

```
algokit goal clerk compile /Path/to/inputfile/approval.teal -o /Path/to/outputfile/approval.compiled
```

Here, `/Path/to/inputfile/approval.teal` and `/Path/to/outputfile/approval.compiled` are paths on your local file system, and they will be automatically accessible to the `goal` command inside the container.

### Manual Copying of Files
Expand All @@ -98,6 +101,7 @@ In case you want to manually copy files into the container, you can do so using
```
docker cp foo.txt algokit_algod:/root
```

This command copies the `foo.txt` from your local system into the root directory of the `algokit_algod` container.

Note: Manual copying is optional and generally only necessary if you have specific reasons for doing so since the system will auto-mount paths specified in commands.
Expand All @@ -106,7 +110,7 @@ Note: Manual copying is optional and generally only necessary if you have specif

If you want to run multiple commands or interact with the filesystem you can execute `algokit goal --console`. This will open a [Bash](https://www.gnu.org/software/bash/) shell session on the `algod` Docker container and from there you can execute goal directly, e.g.:

```
```bash
$ algokit goal --console
Opening Bash console on the algod node; execute `exit` to return to original console
root@82d41336608a:~# goal account list
Expand All @@ -115,4 +119,21 @@ root@82d41336608a:~# goal account list
[online] 4BH5IKMDDHEJEOZ7T5LLT4I7EVIH5XCOTX3TPVQB3HY5TUBVT4MYXJOZVA 4BH5IKMDDHEJEOZ7T5LLT4I7EVIH5XCOTX3TPVQB3HY5TUBVT4MYXJOZVA 2000000000000000 microAlgos
```

## Interactive Mode

Some `goal` commands require interactive input from the user. By default, AlgoKit will attempt to run commands in non-interactive mode first, and automatically switch to interactive mode if needed. You can force a command to run in interactive mode by using the `--interactive` flag:

```bash
$ algokit goal --interactive wallet new algodev
Please choose a password for wallet 'algodev':
Please confirm the password:
Creating wallet...
Created wallet 'algodev'
Your new wallet has a backup phrase that can be used for recovery.
Keeping this backup phrase safe is extremely important.
Would you like to see it now? (Y/n): n
```

This is particularly useful when you know a command will require user input, such as creating new accounts, importing keys, or signing transactions.

For more details about the `AlgoKit goal` command, please refer to the [AlgoKit CLI reference documentation](../cli/index.md#goal).
33 changes: 25 additions & 8 deletions src/algokit/cli/goal.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@
help="Open a Bash console so you can execute multiple goal commands and/or interact with a filesystem.",
default=False,
)
@click.option(
"--interactive",
is_flag=True,
help="Force running the goal command in interactive mode.",
default=False,
)
@click.argument("goal_args", nargs=-1, type=click.UNPROCESSED)
def goal_command(*, console: bool, goal_args: list[str]) -> None:
def goal_command(*, console: bool, interactive: bool, goal_args: list[str]) -> None: # noqa: C901, PLR0912
"""
Run the Algorand goal CLI against the AlgoKit LocalNet.
Expand Down Expand Up @@ -80,17 +86,28 @@ def goal_command(*, console: bool, goal_args: list[str]) -> None:
logger.info("Opening Bash console on the algod node; execute `exit` to return to original console")
result = proc.run_interactive(f"{container_engine} exec -it -w /root algokit_{sandbox.name}_algod bash".split())
else:
cmd = f"{container_engine} exec --interactive --workdir /root algokit_{sandbox.name}_algod goal".split()
cmd = f"{container_engine} exec {'--tty' if interactive else ''} --interactive --workdir /root algokit_{sandbox.name}_algod goal".split() # noqa: E501
input_files, output_files, goal_args = preprocess_command_args(
goal_args, volume_mount_path_local, volume_mount_path_docker
)
cmd = cmd + goal_args
result = proc.run(
cmd,
stdout_log_level=logging.INFO,
prefix_process=False,
pass_stdin=True,
)

if interactive:
result = proc.run_interactive(cmd)
else:
# Try non-interactive first, fallback to interactive if it fails with input-related error
result = proc.run(
cmd,
stdout_log_level=logging.INFO,
prefix_process=False,
pass_stdin=True,
)
if result.exit_code != 0 and "inappropriate ioctl" in (result.output or ""):
# Fallback to interactive mode if we detect TTY-related errors
logger.debug("Command failed with TTY error, retrying in interactive mode")
cmd.insert(2, "--tty")
result = proc.run_interactive(cmd)

post_process(input_files, output_files, volume_mount_path_local)

if result.exit_code != 0:
Expand Down
7 changes: 4 additions & 3 deletions tests/goal/test_goal.test_goal_help.approved.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Usage: algokit goal [OPTIONS] [GOAL_ARGS]...
information.

Options:
--console Open a Bash console so you can execute multiple goal commands
and/or interact with a filesystem.
-h, --help Show this message and exit.
--console Open a Bash console so you can execute multiple goal commands
and/or interact with a filesystem.
--interactive Force running the goal command in interactive mode.
-h, --help Show this message and exit.

0 comments on commit bea004a

Please sign in to comment.