11from dotenv import find_dotenv , load_dotenv
2+ from helpers import exec_and_get_output
23from langchain .chains import LLMChain
34from langchain .chat_models import ChatOpenAI
45from langchain .llms import OpenAI
78from rich .console import Console
89from rich .style import Style
910from 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
1614console = 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 ()
0 commit comments