Skip to content

Commit e280a7f

Browse files
authored
fix: edge cases for running interactive goal commands with --tty flag (#589)
1 parent 1394416 commit e280a7f

File tree

4 files changed

+56
-12
lines changed

4 files changed

+56
-12
lines changed

docs/cli/index.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
- [goal](#goal)
6868
- [Options](#options-11)
6969
- [--console](#--console)
70+
- [--interactive](#--interactive)
7071
- [Arguments](#arguments-6)
7172
- [GOAL_ARGS](#goal_args)
7273
- [init](#init)
@@ -591,6 +592,10 @@ algokit goal [OPTIONS] [GOAL_ARGS]...
591592
### --console
592593
Open a Bash console so you can execute multiple goal commands and/or interact with a filesystem.
593594

595+
596+
### --interactive
597+
Force running the goal command in interactive mode.
598+
594599
### Arguments
595600

596601

docs/features/goal.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,19 @@ $ ~ algokit goal
7979
```
8080

8181
## Working with Files in the Container
82+
8283
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:
8384

8485
### Automatic File Mounting
86+
8587
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.
8688

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

8991
```
9092
algokit goal clerk compile /Path/to/inputfile/approval.teal -o /Path/to/outputfile/approval.compiled
9193
```
94+
9295
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.
9396

9497
### Manual Copying of Files
@@ -98,6 +101,7 @@ In case you want to manually copy files into the container, you can do so using
98101
```
99102
docker cp foo.txt algokit_algod:/root
100103
```
104+
101105
This command copies the `foo.txt` from your local system into the root directory of the `algokit_algod` container.
102106

103107
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.
@@ -106,7 +110,7 @@ Note: Manual copying is optional and generally only necessary if you have specif
106110

107111
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.:
108112

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

122+
## Interactive Mode
123+
124+
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:
125+
126+
```bash
127+
$ algokit goal --interactive wallet new algodev
128+
Please choose a password for wallet 'algodev':
129+
Please confirm the password:
130+
Creating wallet...
131+
Created wallet 'algodev'
132+
Your new wallet has a backup phrase that can be used for recovery.
133+
Keeping this backup phrase safe is extremely important.
134+
Would you like to see it now? (Y/n): n
135+
```
136+
137+
This is particularly useful when you know a command will require user input, such as creating new accounts, importing keys, or signing transactions.
138+
118139
For more details about the `AlgoKit goal` command, please refer to the [AlgoKit CLI reference documentation](../cli/index.md#goal).

src/algokit/cli/goal.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@
2828
help="Open a Bash console so you can execute multiple goal commands and/or interact with a filesystem.",
2929
default=False,
3030
)
31+
@click.option(
32+
"--interactive",
33+
is_flag=True,
34+
help="Force running the goal command in interactive mode.",
35+
default=False,
36+
)
3137
@click.argument("goal_args", nargs=-1, type=click.UNPROCESSED)
32-
def goal_command(*, console: bool, goal_args: list[str]) -> None:
38+
def goal_command(*, console: bool, interactive: bool, goal_args: list[str]) -> None: # noqa: C901, PLR0912
3339
"""
3440
Run the Algorand goal CLI against the AlgoKit LocalNet.
3541
@@ -80,17 +86,28 @@ def goal_command(*, console: bool, goal_args: list[str]) -> None:
8086
logger.info("Opening Bash console on the algod node; execute `exit` to return to original console")
8187
result = proc.run_interactive(f"{container_engine} exec -it -w /root algokit_{sandbox.name}_algod bash".split())
8288
else:
83-
cmd = f"{container_engine} exec --interactive --workdir /root algokit_{sandbox.name}_algod goal".split()
89+
cmd = f"{container_engine} exec {'--tty' if interactive else ''} --interactive --workdir /root algokit_{sandbox.name}_algod goal".split() # noqa: E501
8490
input_files, output_files, goal_args = preprocess_command_args(
8591
goal_args, volume_mount_path_local, volume_mount_path_docker
8692
)
8793
cmd = cmd + goal_args
88-
result = proc.run(
89-
cmd,
90-
stdout_log_level=logging.INFO,
91-
prefix_process=False,
92-
pass_stdin=True,
93-
)
94+
95+
if interactive:
96+
result = proc.run_interactive(cmd)
97+
else:
98+
# Try non-interactive first, fallback to interactive if it fails with input-related error
99+
result = proc.run(
100+
cmd,
101+
stdout_log_level=logging.INFO,
102+
prefix_process=False,
103+
pass_stdin=True,
104+
)
105+
if result.exit_code != 0 and "inappropriate ioctl" in (result.output or ""):
106+
# Fallback to interactive mode if we detect TTY-related errors
107+
logger.debug("Command failed with TTY error, retrying in interactive mode")
108+
cmd.insert(2, "--tty")
109+
result = proc.run_interactive(cmd)
110+
94111
post_process(input_files, output_files, volume_mount_path_local)
95112

96113
if result.exit_code != 0:

tests/goal/test_goal.test_goal_help.approved.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Usage: algokit goal [OPTIONS] [GOAL_ARGS]...
66
information.
77

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

0 commit comments

Comments
 (0)