|
4 | 4 | import logging |
5 | 5 | import optparse |
6 | 6 | import sys |
7 | | -import textwrap |
8 | 7 |
|
9 | 8 | from .bots import load_bot |
10 | | -from .common import Store, open_editor |
| 9 | +from .common import Config, PROGRAM, Store, ensure_state_home, open_editor |
11 | 10 | from .manager import Manager |
12 | 11 |
|
13 | 12 |
|
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 | + ) |
20 | 18 |
|
| 19 | + parser.disable_interspersed_args() |
21 | 20 |
|
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 | + ) |
27 | 26 |
|
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 |
29 | 30 |
|
| 31 | + parser.add_option( |
| 32 | + f"-{name[0].upper()}", |
| 33 | + f"--{name}", |
| 34 | + action="callback", |
| 35 | + callback=callback, |
| 36 | + **kwargs, |
| 37 | + ) |
30 | 38 |
|
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") |
34 | 42 |
|
35 | 43 | 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", |
41 | 79 | ) |
42 | 80 |
|
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 |
90 | 82 |
|
91 | 83 |
|
92 | 84 | def main() -> None: |
93 | | - (opts, _args) = parser.parse_args() |
| 85 | + config = Config.load() |
| 86 | + (opts, _args) = new_parser().parse_args() |
94 | 87 |
|
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)) |
96 | 93 |
|
| 94 | + manager = Manager.create(Store.persistent()) |
97 | 95 | command = getattr(opts, "command", "generate") |
98 | 96 | if command == "generate": |
99 | 97 | bot = load_bot(opts.bot, {}) |
100 | 98 | prompt = opts.prompt |
101 | 99 | if not prompt: |
102 | 100 | if sys.stdin.isatty(): |
103 | | - prompt = open_editor(textwrap.dedent(EDITOR_PLACEHOLDER)) |
| 101 | + prompt = open_editor("Enter your prompt here...") |
104 | 102 | else: |
105 | 103 | prompt = sys.stdin.read() |
106 | 104 | manager.generate_draft( |
|
0 commit comments