Repository: marimo-team/marimo
Pull request: Ask for confirmation if file name already exist instead of overriding it 
Adding confirmation  during export and covert if the filename  already exist instead direct override. 

Resolves : #3015 

## 📋 Checklist

- [x] I have read the [contributor guidelines](https://github.com/marimo-team/marimo/blob/main/CONTRIBUTING.md).
- [x] I have run the code and verified that it works as expected.


@mscolnick


Problem statement:
export and convert with same filename lead to file overwrite
### Description

Hello,

export and convert marimo command can lead to overwrite existing file if (by mistake) user use same file name for input and output.

Best regards

### Suggested solution

if input and output file are the same marimo should ask confirmation from user y/N (default N)

if ouput file name is an ever existing file which will be overwriten it should also ask confirmation from user y/N


(we are all humans and make mistakes... if software can help to avoid some that will be great)

### Alternative

_No response_

### Additional context

_No response_

Hints from issue discussion:
@s-celles this would be nice to have. Any interest in contributing this one? 
Sorry @mscolnick I'm very busy this month and won't have the time to tackle this.

Commit message: Adding confirmation to override a file during export and convert operation in cli

Patch to review:
diff --git a/marimo/_cli/convert/commands.py b/marimo/_cli/convert/commands.py
index d3bd11a0972..eb3d0f03747 100644
--- a/marimo/_cli/convert/commands.py
+++ b/marimo/_cli/convert/commands.py
@@ -62,6 +62,15 @@ def convert(
         notebook = convert_from_md(text)
 
     if output:
+        output_path = Path(output)
+        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:
diff --git a/marimo/_cli/export/commands.py b/marimo/_cli/export/commands.py
index 2e5d7fe8a35..efb038c18a7 100644
--- a/marimo/_cli/export/commands.py
+++ b/marimo/_cli/export/commands.py
@@ -57,6 +57,16 @@ 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:


Code review comments:
diff_hunk: @@ -57,6 +57,16 @@ 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():
path: marimo/_cli/export/commands.py
text: @user1:
we should probably only check once when `watch` is enabled, or not check at all.

could we also check if `GLOBAL_SETTINGS.YES`, is set and skip?

maybe we could pull this out to a util:

```python
def maybe_prompt_to_overwrite(path: Path) -> bool:
    if GLOBAL_SETTINGS.YES:
        return True

    # Check if not in an interactive terminal
    # default to False
    if not sys.stdin.isatty():
        return True
    
    if output_path.exists():
         return True

    confirmed = click.confirm(
        f"Warning: The file '{output}' already exists. Overwrite?",
        default=False,
    )
    
    if not confirmed:
        click.exit()
```