Skip to content

Commit 9e7cf45

Browse files
committed
Fix up naughty subprocess calls and centralize how we exec
1 parent 0b14cad commit 9e7cf45

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

cli.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from dotenv import find_dotenv, load_dotenv
2+
from helpers import exec_and_get_output
23
from langchain.chains import LLMChain
34
from langchain.chat_models import ChatOpenAI
45
from langchain.llms import OpenAI
@@ -7,10 +8,7 @@
78
from rich.console import Console
89
from rich.style import Style
910
from setup import __version__
10-
import click, datetime, openai, os, random, sys, webbrowser
11-
import tempfile
12-
import subprocess
13-
11+
import click, datetime, openai, os, random, subprocess, sys, tempfile, webbrowser
1412

1513
# Create a Console object
1614
console = Console()
@@ -49,10 +47,7 @@ def setup_environment():
4947
console.print("[bold green]Created .env file with your OpenAI API key. You're all set![/bold green]")
5048
sys.exit(0)
5149

52-
console.print(
53-
"[bold red]Please set an API key in the OPENAI_API_KEY environment variable or in a .env file.[/bold red]"
54-
)
55-
sys.exit(1)
50+
raise click.ClickException("Please set an API key in the OPENAI_API_KEY environment variable or in a .env file.")
5651

5752

5853
@click.group()
@@ -79,14 +74,14 @@ def commit(verbose, max_tokens):
7974
chat_chain = LLMChain(llm=llm, prompt=prompt, verbose=verbose)
8075

8176
# Get the changes from git
82-
diff = os.popen("git diff").read()
77+
diff = exec_and_get_output(["git", "diff"])
8378

8479
with console.status("Thinking", spinner="point"):
8580
response = chat_chain.run(diff)
8681
console.print(response, style=bot_style)
8782

8883
# List the files that will be committed
89-
files = os.popen("git diff --name-only").read()
84+
files = exec_and_get_output(["git", "diff", "--name-only"])
9085
console.print("The following files will be committed:\n" + files)
9186

9287
# Write the commit message to a temporary file
@@ -96,15 +91,15 @@ def commit(verbose, max_tokens):
9691

9792
# Open the temporary file in the user's editor
9893
editor = os.getenv("EDITOR", "vim")
99-
subprocess.call([editor, temp_file_name])
94+
subprocess.call([editor, temp_file_name]) # noqa: S603
10095

10196
# Ask the user if they want to commit the changes
10297
if click.confirm("Do you want to commit the changes?"):
10398
# Commit the changes using the temporary file for the commit message
104-
os.system(f"git commit -a -F {temp_file_name}")
99+
exec_and_get_output(["git", "commit", "-a", "-F", temp_file_name])
105100

106101
# Delete the temporary file
107-
os.unlink(temp_file_name)
102+
Path.unlink(temp_file_name)
108103

109104

110105
@cli.command()

helpers.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import subprocess
2+
3+
4+
def exec_and_get_output(command):
5+
"""Execute a command and return its output as a string."""
6+
result = subprocess.run(command, capture_output=True, text=True) # noqa: S603
7+
if result.returncode != 0:
8+
raise Exception(f"Command '{' '.join(command)}' failed with error:\n{result.stderr}") # noqa: TRY002
9+
return result.stdout

0 commit comments

Comments
 (0)