Skip to content

Commit fa190f7

Browse files
committed
Adds new subcommand edit, adjust Justfile
1 parent d268a25 commit fa190f7

4 files changed

Lines changed: 90 additions & 20 deletions

File tree

Justfile

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
1-
@test *options: format
2-
uv run pytest {{options}}
1+
test *options:
2+
DEVCLI_LOGLEVEL=debug uv run pytest {{options}}
33

4-
@lint:
5-
uv run black . --check --diff --color
4+
lint:
5+
uv run black . --check --diff --color
66

7-
@format:
8-
uv run black .
7+
format:
8+
uv run black .
99

10-
@update:
11-
uv sync --upgrade
10+
update:
11+
uv sync --upgrade
1212

13-
@build:
14-
uv build --wheel
13+
build:
14+
uv build --wheel
1515

16-
@install: build
17-
pipx install --force dist/devcli*.whl
16+
install: build
17+
pipx install --force dist/devcli*.whl
1818

19-
@check: install
20-
devcli example ping
21-
-devcli show-version
22-
-devcli show-config
19+
check: install
20+
devcli example ping
21+
-devcli show-version
22+
-devcli show-config
2323

24-
@bundle:
25-
cd docs; bundle
24+
bundle:
25+
cd docs; bundle
2626

27-
@docs: bundle
27+
docs: bundle
2828
cd docs; rake
29+
30+
@run +commands:
31+
- uv run python bin/devcli {{commands}}

devcli/commands/edit.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import os
2+
from typer import Context
3+
from pathlib import Path
4+
5+
import devcli.framework as cmd
6+
7+
cli = cmd.new("Edit different files using text editor")
8+
9+
logger = cmd.logger()
10+
11+
# closure to create partial func
12+
def prep_editor(ctx: Context):
13+
def run_editor(file: Path):
14+
"""Open editor on file"""
15+
c_editor = ctx.obj["devcli.commands.edit.editor"]
16+
e_editor = os.getenv("EDITOR", "vim")
17+
18+
# config first, env and last default
19+
editor = c_editor if c_editor else e_editor
20+
21+
os.execvp(editor, [editor, file])
22+
23+
def open_editor(file: Path):
24+
run_editor(file)
25+
26+
return open_editor
27+
28+
29+
@cli.command()
30+
def config(ctx: Context):
31+
editor = prep_editor(ctx)
32+
configs = ctx.obj.files()
33+
if not configs:
34+
cmd.stop("No configuration files were found.")
35+
36+
# last file is the most specific one
37+
editor(configs[-1])
38+
39+
40+
@cli.command("command")
41+
def edit_command(ctx: Context, name: str):
42+
editor = prep_editor(ctx)
43+
configs = ctx.obj.files()
44+
p = Path(configs[-1]).parent
45+
subcmd = p / f"{name}.py"
46+
logger.info(f"command search path: {subcmd}")
47+
if subcmd.exists():
48+
logger.info(f"command exists opening editor...")
49+
editor(subcmd)
50+
else:
51+
logger.info(f"command does not exist in {subcmd}, stopping.")
52+
cmd.stop(f"cmd: {subcmd.name} not found in path: {p}")

tests/commands/test_edit.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
import pytest
22

3+
from unittest.mock import patch, ANY
4+
5+
6+
@pytest.fixture
7+
def mock_exec():
8+
with patch("devcli.commands.edit.os.execvp") as mock:
9+
yield mock
10+
311

412
@pytest.fixture(autouse=True)
513
def setup(devcli_cmd):
614
global edit
715
edit = devcli_cmd("edit")
816

917

10-
def test_simple():
18+
def test_edit_config_calls_editor(mock_exec):
1119
result = edit("config")
20+
mock_exec.assert_called_once_with("nvim", ["nvim", ANY])
21+
22+
23+
def test_edit_command_calls_editor(mock_exec):
24+
result = edit("command", "placeholder")
25+
mock_exec.assert_called_once_with("nvim", ["nvim", ANY])

tests/fixtures/placeholder.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Just a place holder for 'edit command' test in test_edit.py

0 commit comments

Comments
 (0)