diff --git a/docs/cli/index.md b/docs/cli/index.md index 50337058..a2d99a28 100644 --- a/docs/cli/index.md +++ b/docs/cli/index.md @@ -67,6 +67,7 @@ - [goal](#goal) - [Options](#options-11) - [--console](#--console) + - [--interactive](#--interactive) - [Arguments](#arguments-6) - [GOAL_ARGS](#goal_args) - [init](#init) @@ -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 diff --git a/docs/features/goal.md b/docs/features/goal.md index c281d08e..b6adc234 100644 --- a/docs/features/goal.md +++ b/docs/features/goal.md @@ -79,9 +79,11 @@ $ ~ 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: @@ -89,6 +91,7 @@ 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 @@ -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. @@ -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 @@ -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). diff --git a/src/algokit/cli/goal.py b/src/algokit/cli/goal.py index 7eec1a68..2c440de8 100644 --- a/src/algokit/cli/goal.py +++ b/src/algokit/cli/goal.py @@ -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. @@ -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: diff --git a/tests/goal/test_goal.test_goal_help.approved.txt b/tests/goal/test_goal.test_goal_help.approved.txt index f88e2fa2..778cea4f 100644 --- a/tests/goal/test_goal.test_goal_help.approved.txt +++ b/tests/goal/test_goal.test_goal_help.approved.txt @@ -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.