Skip to content

Commit d49a07e

Browse files
committed
chore(cmd): Add version and commit information to build process.
- Modify LDFLAGS to include version and commit information - Add VERSION and COMMIT variables in version.go - Remove indirect dependency on github.com/fatih/color in go.mod
1 parent eacd020 commit d49a07e

File tree

4 files changed

+30
-17
lines changed

4 files changed

+30
-17
lines changed

Makefile

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,21 @@ GO ?= go
22
EXECUTABLE := codegpt
33
GOFILES := $(shell find . -type f -name "*.go")
44
TAGS ?=
5-
LDFLAGS ?= -X 'main.Version=$(VERSION)'
5+
LDFLAGS ?= -X 'github.com/appleboy/CodeGPT/cmd.Version=$(VERSION)' -X 'github.com/appleboy/CodeGPT/cmd.Commit=$(COMMIT)'
66

77
ifneq ($(shell uname), Darwin)
88
EXTLDFLAGS = -extldflags "-static" $(null)
99
else
1010
EXTLDFLAGS =
1111
endif
1212

13+
ifneq ($(DRONE_TAG),)
14+
VERSION ?= $(DRONE_TAG)
15+
else
16+
VERSION ?= $(shell git describe --tags --always || git rev-parse --short HEAD)
17+
endif
18+
COMMIT ?= $(shell git rev-parse --short HEAD)
19+
1320
build: $(EXECUTABLE)
1421

1522
$(EXECUTABLE): $(GOFILES)

cmd/commit.go

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cmd
22

33
import (
4-
"log"
4+
"errors"
55
"os"
66
"strings"
77

@@ -27,17 +27,17 @@ func init() {
2727
var 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
}

cmd/version.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ import (
66
"github.com/spf13/cobra"
77
)
88

9-
var version string = "0.0.1"
9+
var (
10+
Version string = ""
11+
Commit string = ""
12+
)
1013

1114
var versionCmd = &cobra.Command{
1215
Use: "version",
1316
Short: "Print the version number",
1417
Run: func(cmd *cobra.Command, args []string) {
15-
fmt.Println("version:", version)
18+
fmt.Println("version:", Version, "commit:", Commit)
1619
},
1720
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ go 1.20
44

55
require (
66
github.com/appleboy/com v0.1.6
7+
github.com/fatih/color v1.14.1
78
github.com/sashabaranov/go-openai v1.4.1
89
github.com/spf13/cobra v1.6.1
910
github.com/spf13/viper v1.15.0
1011
)
1112

1213
require (
13-
github.com/fatih/color v1.14.1 // indirect
1414
github.com/fsnotify/fsnotify v1.6.0 // indirect
1515
github.com/hashicorp/hcl v1.0.0 // indirect
1616
github.com/inconshreveable/mousetrap v1.0.1 // indirect

0 commit comments

Comments
 (0)