Skip to content

Commit

Permalink
Adding confirmation to override a file during export and convert oper…
Browse files Browse the repository at this point in the history
…ation in cli
  • Loading branch information
xeniumcode committed Mar 7, 2025
1 parent 0b58bf7 commit 03c7208
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
8 changes: 4 additions & 4 deletions marimo/_cli/convert/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from marimo._cli.convert.markdown import convert_from_md
from marimo._cli.convert.utils import load_external_file
from marimo._cli.print import echo
from marimo._cli.utils import prompt_to_overwrite
from marimo._convert.ipynb import convert_from_ipynb
from marimo._utils.paths import maybe_make_dirs

Expand Down Expand Up @@ -63,13 +64,12 @@ def convert(

if output:
output_path = Path(output)
if output_path.exists():
overwrite = click.confirm(
if prompt_to_overwrite(output_path):
confirmed = click.confirm(
f"Warning: The file '{output}' already exists. Overwrite?",
default=False,
)
if not overwrite:
echo("No changes made. File was not overwritten.")
if not confirmed:
return
# Make dirs if needed
maybe_make_dirs(output)
Expand Down
21 changes: 11 additions & 10 deletions marimo/_cli/export/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from marimo._cli.parse_args import parse_args
from marimo._cli.print import echo, green
from marimo._cli.utils import prompt_to_overwrite
from marimo._dependencies.dependencies import DependencyManager
from marimo._server.export import (
ExportResult,
Expand Down Expand Up @@ -57,16 +58,6 @@ def watch_and_export(

def write_data(data: str) -> None:
if output:
output_path = Path(output)
# Check if the file exists
if output_path.exists():
overwrite = click.confirm(
f"Warning: The file '{output}' already exists. Overwrite?",
default=False,
)
if not overwrite:
echo("No changes made. File was not overwritten.")
return
# Make dirs if needed
maybe_make_dirs(output)
with open(output, "w", encoding="utf-8") as f:
Expand All @@ -76,6 +67,16 @@ def write_data(data: str) -> None:
echo(data)
return

if output:
output_path = Path(output)
if prompt_to_overwrite(output_path):
confirmed = click.confirm(
f"Warning: The file '{output_path}' already exists. Overwrite?",
default=False,
)
if not confirmed:
return

# No watch, just run once
if not watch:
result = export_callback(marimo_path)
Expand Down
21 changes: 21 additions & 0 deletions marimo/_cli/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2024 Marimo. All rights reserved.

from pathlib import Path
from sys import stdin

from marimo._config.settings import GLOBAL_SETTINGS


def prompt_to_overwrite(path: Path) -> bool:
if GLOBAL_SETTINGS.YES:
return False

# Check if not in an interactive terminal
# default to False
if not stdin.isatty():
return False

if path.exists():
return True

return False

0 comments on commit 03c7208

Please sign in to comment.