diff --git a/snk/main.py b/snk/main.py index 01522b4..af0429d 100644 --- a/snk/main.py +++ b/snk/main.py @@ -243,6 +243,34 @@ def list( console = Console() console.print(table) +@app.command() +def create(path: Path, force: bool = typer.Option(False, "--force", "-f")): + """Create a default snk.yaml project that can be installed with snk""" + if path.exists(): + if not force: + typer.secho(f"Directory '{path}' already exists! Use --force to overwrite.", fg="red", err=True) + raise typer.Exit(1) + else: + typer.secho(f"Overwriting existing directory '{path}'.", fg="yellow", err=True) + import shutil + shutil.rmtree(path) + try: + path.mkdir(parents=True, exist_ok=False) + except FileExistsError: + typer.secho(f"Directory '{path}' already exists! Use --force to overwrite.", fg="red", err=True) + raise typer.Exit(1) + snk_config = SnkConfig.from_workflow_dir(path, create_if_not_exists=True) + snk_config.cli["msg"] = { + "help": "Print a help message.", + "default": "Hello, World!", + "required": False, + "short": "m", + } + snk_config.save() + typer.echo(f"Created snk.yaml at {snk_config._snk_config_path}") + with open(path / "Snakefile", "w") as f: + f.write("""rule all:\n shell: f"echo {config['msg']}"\n""") + @app.command() def edit( ctx: typer.Context, diff --git a/tests/test_snk.py b/tests/test_snk.py index 20fe4d3..a07136e 100644 --- a/tests/test_snk.py +++ b/tests/test_snk.py @@ -76,6 +76,12 @@ def test_snk_uninstall(local_workflow: Workflow): assert result.exit_code == 0 assert "Successfully uninstalled" in result.stdout +def test_snk_create(local_workflow: Workflow, tmp_path: Path): + result = runner.invoke(app, ["create", str(tmp_path), "--force"]) + print(result.stdout) + assert result.exit_code == 0 + assert (tmp_path / "snk.yaml").exists() + assert (tmp_path / "Snakefile").exists() def test_import_create_cli(capsys): from snk import create_cli