Repository: oumi-ai/oumi
File: src/oumi/cli/fetch.py (Python)

Code before:
    ],
    output_dir: Annotated[
        Optional[Path],
        typer.Option(
            "--output-dir",
            "-o",
            help=(
                "Directory to save configs "
                "(defaults to OUMI_DIR env var or ~/.oumi/configs)"
            ),
        ),
    ] = None,
) -> None:
    """Fetch configuration files from GitHub repository."""
    # Remove oumi:// prefix if present
    if config_path.startswith("oumi://"):
        config_path, config_dir = resolve_oumi_prefix(config_path, output_dir)

    else:
        # raise error
        logger.error("Invalid config path")
        raise typer.Exit(1)

    try:
        # Fetch from GitHub
        github_url = f"{OUMI_GITHUB_RAW}/{config_path}"
        response = requests.get(github_url)
        response.raise_for_status()
        config_content = response.text

        # Validate YAML
        yaml.safe_load(config_content)

        # Save to destination
        local_path = (config_dir or Path(OUMI_DIR).expanduser()) / config_path
        local_path.parent.mkdir(parents=True, exist_ok=True)

        with open(local_path, "w") as f:
            f.write(config_content)

        logger.info(f"Successfully downloaded config to {local_path}")

    except requests.RequestException as e:
        logger.error(f"Failed to download config from GitHub: {e}")
        raise typer.Exit(1)
    except yaml.YAMLError:
        logger.error("Invalid YAML configuration")
        raise typer.Exit(1)


Review comment (suggestion):
```suggestion
        github_url = f"{OUMI_GITHUB_RAW}/{config_path.lstrip('/')}"
```
This lets us gracefully handle the user input if there are any leading slashes

Code after:
        ),
    ],
    output_dir: Annotated[
        Optional[Path],
        typer.Option(
            "--output-dir",
            "-o",
            help=(
                "Directory to save configs "
                "(defaults to OUMI_DIR env var or ~/.oumi/configs)"
            ),
        ),
    ] = None,
    force: Annotated[
        bool, typer.Option("--force", "-f", help="Overwrite existing config if present")
    ] = False,
) -> None:
    """Fetch configuration files from GitHub repository."""
    # Remove oumi:// prefix if present
    config_path, config_dir = resolve_oumi_prefix(config_path, output_dir)

    try:
        # Check destination first
        local_path = (config_dir or Path(OUMI_DIR).expanduser()) / config_path
        if local_path.exists() and not force:
            msg = f"Config already exists at {local_path}. Use --force to overwrite"
            logger.error(msg)
            typer.echo(msg, err=True)
            raise typer.Exit(code=1)

        # Fetch from GitHub
        github_url = f"{OUMI_GITHUB_RAW}/{config_path.lstrip('/')}"
        response = requests.get(github_url)
        response.raise_for_status()
        config_content = response.text

        # Validate YAML
        yaml.safe_load(config_content)

        # Save to destination
        if local_path.exists():
            logger.warning(f"Overwriting existing config at {local_path}")
        local_path.parent.mkdir(parents=True, exist_ok=True)

        with open(local_path, "w") as f:
            f.write(config_content)
        logger.info(f"Successfully downloaded config to {local_path}")

    except RequestException as e:
        logger.error(f"Failed to download config from GitHub: {e}")
        raise typer.Exit(1)