11package cmd
22
33import (
4- "log "
4+ "errors "
55 "os"
66 "strings"
77
@@ -27,17 +27,17 @@ func init() {
2727var commitCmd = & cobra.Command {
2828 Use : "commit" ,
2929 Short : "Auto generate commit message" ,
30- Run : func (cmd * cobra.Command , args []string ) {
30+ RunE : func (cmd * cobra.Command , args []string ) error {
3131 var message string
3232
3333 // check git command exist
3434 if ! util .IsCommandAvailable ("git" ) {
35- log . Fatal ("To use CodeGPT, you must have git on your PATH" )
35+ return errors . New ("To use CodeGPT, you must have git on your PATH" )
3636 }
3737
3838 diff , err := git .Diff ()
3939 if err != nil {
40- log . Fatal ( err )
40+ return err
4141 }
4242
4343 color .Green ("Summarize the commit message use " + viper .GetString ("openai.model" ) + " model" )
@@ -48,7 +48,7 @@ var commitCmd = &cobra.Command{
4848 viper .GetString ("openai.org_id" ),
4949 )
5050 if err != nil {
51- log . Fatal ( err )
51+ return err
5252 }
5353
5454 // Get summarize comment from diff datas
@@ -59,13 +59,13 @@ var commitCmd = &cobra.Command{
5959 },
6060 )
6161 if err != nil {
62- log . Fatal ( err )
62+ return err
6363 }
6464
6565 color .Cyan ("We are trying to summarize a git diff" )
6666 summarizeDiff , err := client .Completion (cmd .Context (), out )
6767 if err != nil {
68- log . Fatal ( err )
68+ return err
6969 }
7070
7171 out , err = util .GetTemplate (
@@ -75,13 +75,13 @@ var commitCmd = &cobra.Command{
7575 },
7676 )
7777 if err != nil {
78- log . Fatal ( err )
78+ return err
7979 }
8080
8181 color .Cyan ("We are trying to summarize a title for pull request" )
8282 summarizeTitle , err := client .Completion (cmd .Context (), out )
8383 if err != nil {
84- log . Fatal ( err )
84+ return err
8585 }
8686
8787 if prompt .GetLanguage (viper .GetString ("output.lang" )) != prompt .DefaultLanguage {
@@ -94,23 +94,26 @@ var commitCmd = &cobra.Command{
9494 },
9595 )
9696 if err != nil {
97- log . Fatal ( err )
97+ return err
9898 }
9999
100100 color .Cyan ("We are trying to translate a git commit message to " + prompt .GetLanguage (viper .GetString ("output.lang" )) + "language" )
101101 summarize , err := client .Completion (cmd .Context (), out )
102102 if err != nil {
103- log . Fatal ( err )
103+ return err
104104 }
105105 message = summarize
106106 } else {
107107 message = strings .TrimSpace (summarizeTitle ) + "\n \n " + strings .TrimSpace (summarizeDiff )
108108 }
109-
109+ color .Yellow ("================Commit Summary====================" )
110+ color .Yellow ("\n " + message + "\n \n " )
111+ color .Yellow ("==================================================" )
110112 color .Cyan ("Write the commit message to " + viper .GetString ("output.file" ) + " file" )
111113 err = os .WriteFile (viper .GetString ("output.file" ), []byte (message ), 0o644 )
112114 if err != nil {
113- log . Fatal ( err )
115+ return err
114116 }
117+ return nil
115118 },
116119}
0 commit comments