Skip to content

Commit

Permalink
fix: adding confirmation prompt prior to execution of algokit generat…
Browse files Browse the repository at this point in the history
…ors (#366)

* fix: adding confirmation prompt prior to execution of algokit generators

* chore: adding force flag
  • Loading branch information
aorumbayev authored Dec 8, 2023
1 parent e59313f commit eeb5bae
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions docs/features/generate.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,7 @@ $ algokit generate smart-contract

$ algokit generate smart-contract -a contract_name "MyCoolContract"
```

#### Third Party Generators

It is important to understand that by default, AlgoKit will always prompt you before executing a generator to ensure it's from a trusted source. If you are confident about the source of the generator, you can use the `--force` or `-f` option to execute the generator without this confirmation prompt. Be cautious while using this option and ensure the generator is from a trusted source. At the moment, a trusted source for a generator is defined as _a generator that is included in the official AlgoKit templates (e.g. `smart-contract` generator in `algokit-beaker-default-template`)_
24 changes: 23 additions & 1 deletion src/algokit/cli/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,21 @@ def _load_custom_generate_commands(project_dir: Path) -> dict[str, click.Command
type=click.Path(exists=True),
default=generator.path,
)
def command(answers: list[tuple[str, str]], path: Path) -> None:
@click.option(
"--force",
"-f",
is_flag=True,
required=False,
default=False,
type=click.BOOL,
help="Executes generator without confirmation. Use with trusted sources only.",
)
def command(
*,
answers: list[tuple[str, str]],
path: Path,
force: bool,
) -> None:
if not shutil.which("git"):
raise click.ClickException(
"Git not found; please install git and add to path.\n"
Expand All @@ -52,6 +66,14 @@ def command(answers: list[tuple[str, str]], path: Path) -> None:

answers_dict = dict(answers)

if not force and not click.confirm(
"You are about to run a generator. Please make sure it's from a "
"trusted source (for example, official AlgoKit Templates). Do you want to proceed?",
default=False,
):
logger.warning("Generator execution cancelled.")
return None

return run_generator(answers_dict, path)

commands_table[generator.name] = command
Expand Down
1 change: 1 addition & 0 deletions src/algokit/core/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def run_generator(answers: dict, path: Path) -> None:
dst_path=cwd,
data=answers,
quiet=True,
unsafe=True,
) as copier_worker:
logger.debug(f"Running generator in {copier_worker.src_path}")
copier_worker.run_copy()
Expand Down
2 changes: 1 addition & 1 deletion tests/generate/test_generate_custom_generate_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_generate_custom_generate_commands_valid_generator_run(
mock_copier_worker_cls = mocker.patch("copier.main.Worker")
mock_copier_worker_cls.return_value.__enter__.return_value.src_path = str(cwd / "smart_contract")

result = invoke("generate smart-contract", cwd=cwd)
result = invoke("generate smart-contract", cwd=cwd, input="y\n")

assert result.exit_code == 0
assert mock_copier_worker_cls.call_args.kwargs["src_path"] == str(cwd / "smart_contract")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
DEBUG: Attempting to load project config from {current_working_directory}/.algokit.toml
You are about to run a generator. Please make sure it's from a trusted source (for example, official AlgoKit Templates). Do you want to proceed? [y/N]: y
DEBUG: Running generator in {current_working_directory}/smart_contract

1 comment on commit eeb5bae

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage

Coverage Report
FileStmtsMissCoverMissing
src/algokit
   __init__.py15753%6–13, 17–24, 32–34
   __main__.py220%1–3
src/algokit/cli
   completions.py108298%83, 98
   deploy.py72790%44, 46, 92–94, 158, 182
   dispenser.py118199%75
   doctor.py48394%142–144
   explore.py501276%34–39, 41–46
   generate.py67396%74–75, 140
   goal.py39197%57
   init.py1901692%273–274, 324, 327–329, 340, 384, 410, 450, 459–461, 464–469, 482
   localnet.py93397%162, 183–184
src/algokit/cli/common
   utils.py26292%120, 123
src/algokit/cli/tasks
   assets.py821384%65–66, 72, 74–75, 105, 119, 125–126, 132, 134, 136–137
   ipfs.py541180%54, 84, 89–91, 96, 98–99, 109–111
   mint.py66494%48, 70, 91, 250
   send_transaction.py651085%52–53, 57, 89, 158, 170–174
   sign_transaction.py59886%21, 28–30, 71–72, 109, 123
   transfer.py37392%25, 89, 116
   utils.py994555%26–34, 40–43, 75–76, 100–101, 125–133, 152–162, 209, 258–259, 279–290, 297–299
   vanity_address.py561082%41, 45–48, 112, 114, 121–123
   wallet.py79495%21, 66, 136, 162
src/algokit/core
   bootstrap.py1612485%103–104, 126, 149, 214, 217, 223–237, 246–251
   conf.py54885%10, 24, 28, 36, 38, 71–73
   deploy.py691184%61–64, 73–75, 79, 84, 91–93
   dispenser.py2022687%91, 123–124, 141–149, 191–192, 198–200, 218–219, 259–260, 318, 332–334, 345–346, 356, 369, 384
   doctor.py65789%67–69, 92–94, 134
   generate.py41295%69, 87
   goal.py60395%30–31, 41
   log_handlers.py68790%50–51, 63, 112–116, 125
   proc.py45198%98
   sandbox.py1991493%70, 116–123, 134, 429, 445, 470, 478
   typed_client_generation.py80594%55–57, 70, 75
   utils.py18289%27–28
   version_prompt.py72889%26–27, 39, 58–61, 79, 108
src/algokit/core/tasks
   ipfs.py57493%133, 137, 139, 145
   nfd.py491373%25, 31, 34–41, 70–72, 99–101
   vanity_address.py903462%49–50, 54, 59–75, 92–108, 128–131
   wallet.py71593%37, 129, 155–157
src/algokit/core/tasks/mint
   mint.py781087%123–133, 187
   models.py901188%50, 52, 57, 71–74, 85–88
TOTAL318836289% 

Tests Skipped Failures Errors Time
382 0 💤 0 ❌ 0 🔥 13.551s ⏱️

Please sign in to comment.