Skip to content

Commit fbf21b5

Browse files
authored
feat: log to state file (#21)
1 parent 690050a commit fbf21b5

File tree

3 files changed

+77
-79
lines changed

3 files changed

+77
-79
lines changed

src/git_draft/__main__.py

Lines changed: 70 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,103 +4,101 @@
44
import logging
55
import optparse
66
import sys
7-
import textwrap
87

98
from .bots import load_bot
10-
from .common import Store, open_editor
9+
from .common import Config, PROGRAM, Store, ensure_state_home, open_editor
1110
from .manager import Manager
1211

1312

14-
logging.basicConfig(level=logging.INFO)
15-
16-
17-
EPILOG = """\
18-
More information via `man git-draft` and https://mtth.github.io/git-draft.
19-
"""
13+
def new_parser() -> optparse.OptionParser:
14+
parser = optparse.OptionParser(
15+
prog=PROGRAM,
16+
version=importlib.metadata.version("git_draft"),
17+
)
2018

19+
parser.disable_interspersed_args()
2120

22-
parser = optparse.OptionParser(
23-
prog="git-draft",
24-
epilog=textwrap.dedent(EPILOG),
25-
version=importlib.metadata.version("git_draft"),
26-
)
21+
parser.add_option(
22+
"--log",
23+
help="show log path and exit",
24+
action="store_true",
25+
)
2726

28-
parser.disable_interspersed_args()
27+
def add_command(name: str, **kwargs) -> None:
28+
def callback(_option, _opt, _value, parser) -> None:
29+
parser.values.command = name
2930

31+
parser.add_option(
32+
f"-{name[0].upper()}",
33+
f"--{name}",
34+
action="callback",
35+
callback=callback,
36+
**kwargs,
37+
)
3038

31-
def add_command(name: str, **kwargs) -> None:
32-
def callback(_option, _opt, _value, parser) -> None:
33-
parser.values.command = name
39+
add_command("discard", help="discard the current draft")
40+
add_command("finalize", help="apply current draft to original branch")
41+
add_command("generate", help="start a new draft from a prompt")
3442

3543
parser.add_option(
36-
f"-{name[0].upper()}",
37-
f"--{name}",
38-
action="callback",
39-
callback=callback,
40-
**kwargs,
44+
"-b",
45+
"--bot",
46+
dest="bot",
47+
help="bot key",
48+
default="openai",
49+
)
50+
parser.add_option(
51+
"-c",
52+
"--checkout",
53+
help="check out generated changes",
54+
action="store_true",
55+
)
56+
parser.add_option(
57+
"-d",
58+
"--delete",
59+
help="delete draft after finalizing or discarding",
60+
action="store_true",
61+
)
62+
parser.add_option(
63+
"-p",
64+
"--prompt",
65+
dest="prompt",
66+
help="draft generation prompt, read from stdin if unset",
67+
)
68+
parser.add_option(
69+
"-r",
70+
"--reset",
71+
help="reset index before generating a new draft",
72+
action="store_true",
73+
)
74+
parser.add_option(
75+
"-s",
76+
"--sync",
77+
help="commit prior worktree changes separately",
78+
action="store_true",
4179
)
4280

43-
44-
add_command("discard", help="discard the current draft")
45-
add_command("finalize", help="apply the current draft to the original branch")
46-
add_command("generate", help="start a new draft from a prompt")
47-
48-
parser.add_option(
49-
"-a",
50-
"--bot",
51-
dest="bot",
52-
help="bot key",
53-
default="openai",
54-
)
55-
parser.add_option(
56-
"-c",
57-
"--checkout",
58-
help="check out generated changes",
59-
action="store_true",
60-
)
61-
parser.add_option(
62-
"-d",
63-
"--delete",
64-
help="delete draft after finalizing or discarding",
65-
action="store_true",
66-
)
67-
parser.add_option(
68-
"-p",
69-
"--prompt",
70-
dest="prompt",
71-
help="draft generation prompt, read from stdin if unset",
72-
)
73-
parser.add_option(
74-
"-r",
75-
"--reset",
76-
help="reset index before generating a new draft",
77-
action="store_true",
78-
)
79-
parser.add_option(
80-
"-s",
81-
"--sync",
82-
help="commit prior worktree changes separately",
83-
action="store_true",
84-
)
85-
86-
87-
EDITOR_PLACEHOLDER = """\
88-
Enter your prompt here...
89-
"""
81+
return parser
9082

9183

9284
def main() -> None:
93-
(opts, _args) = parser.parse_args()
85+
config = Config.load()
86+
(opts, _args) = new_parser().parse_args()
9487

95-
manager = Manager.create(Store.persistent())
88+
log_path = ensure_state_home() / "log"
89+
if opts.log:
90+
print(log_path)
91+
return
92+
logging.basicConfig(level=config.log_level, filename=str(log_path))
9693

94+
manager = Manager.create(Store.persistent())
9795
command = getattr(opts, "command", "generate")
9896
if command == "generate":
9997
bot = load_bot(opts.bot, {})
10098
prompt = opts.prompt
10199
if not prompt:
102100
if sys.stdin.isatty():
103-
prompt = open_editor(textwrap.dedent(EDITOR_PLACEHOLDER))
101+
prompt = open_editor("Enter your prompt here...")
104102
else:
105103
prompt = sys.stdin.read()
106104
manager.generate_draft(

src/git_draft/common.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import xdg_base_dirs
2020

2121

22-
NAMESPACE = "git-draft"
22+
PROGRAM = "git-draft"
2323

2424

2525
@dataclasses.dataclass(frozen=True)
@@ -34,7 +34,7 @@ def default(cls) -> Self:
3434

3535
@staticmethod
3636
def path() -> Path:
37-
return xdg_base_dirs.xdg_config_home() / NAMESPACE / "config.toml"
37+
return xdg_base_dirs.xdg_config_home() / PROGRAM / "config.toml"
3838

3939
@classmethod
4040
def load(cls) -> Self:
@@ -45,7 +45,7 @@ def load(cls) -> Self:
4545
except FileNotFoundError:
4646
return cls.default()
4747
else:
48-
bot_data = data["bots"] or {}
48+
bot_data = data.get("bots", {})
4949
return cls(
5050
log_level=logging.getLevelName(data["log_level"]),
5151
bots={k: BotConfig(**v) for k, v in bot_data.items()},
@@ -63,8 +63,8 @@ class BotConfig:
6363
pythonpath: str | None = None
6464

6565

66-
def _ensure_state_home() -> Path:
67-
path = xdg_base_dirs.xdg_state_home() / NAMESPACE
66+
def ensure_state_home() -> Path:
67+
path = xdg_base_dirs.xdg_state_home() / PROGRAM
6868
path.mkdir(parents=True, exist_ok=True)
6969
return path
7070

@@ -121,7 +121,7 @@ def __init__(self, conn: sqlite3.Connection) -> None:
121121

122122
@classmethod
123123
def persistent(cls) -> Store:
124-
path = _ensure_state_home() / cls._name
124+
path = ensure_state_home() / cls._name
125125
conn = sqlite3.connect(str(path), autocommit=False)
126126
return cls(conn)
127127

tests/git_draft/common_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def state_home(monkeypatch, tmp_path) -> None:
1313

1414

1515
def test_ensure_state_home() -> None:
16-
path = sut._ensure_state_home()
16+
path = sut.ensure_state_home()
1717
assert path.exists()
1818

1919

0 commit comments

Comments
 (0)