Skip to content

Commit eb65eb6

Browse files
authored
Feat/release (#2)
* fix: barry 2024-12-31 14:12:22 * fix: barry 2024-12-31 14:13:40 * fix: barry 2025-01-02 11:07:23 * refactor: Simplify git diff handling and add OpenAI client * docs: Add environment variables to README.md * ci: Add release workflow and GoReleaser config * feat: Add branch name to git push command * feat: Add tag command and update build configurations * fix: barry 2025-01-02 22:21:32 * fix: barry 2025-01-02 22:22:33 * fix: barry 2025-01-03 10:04:11 * chore: Update GoReleaser config and refactor tag utils
1 parent 8398513 commit eb65eb6

File tree

8 files changed

+169
-6
lines changed

8 files changed

+169
-6
lines changed

.github/workflows/release.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
name: Release
2+
on:
3+
push:
4+
tags: [ "v*.*.*" ]
5+
6+
jobs:
7+
release:
8+
name: Release on GitHub
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v4
13+
- name: Set up Go
14+
uses: actions/setup-go@v5
15+
- name: Run GoReleaser
16+
uses: goreleaser/goreleaser-action@v5
17+
with:
18+
version: latest
19+
args: release --clean --timeout 60m
20+
env:
21+
GITHUB_TOKEN: ${{ secrets.TOKEN }}

.goreleaser.yaml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
builds:
2+
- main: ./main.go
3+
id: fastcommit
4+
binary: fastcommit
5+
skip: false
6+
env:
7+
- CGO_ENABLED=0
8+
- GOPROXY=https://goproxy.io
9+
targets:
10+
- "darwin_amd64"
11+
- "windows_amd64"
12+
- "linux_amd64"
13+
- "linux_amd64"
14+
ldflags:
15+
- -X 'github.com/pubgo/funk/version.version={{ .Version }}'
16+
- -X 'github.com/pubgo/funk/version.project=fastcommit'
17+
- -X 'github.com/pubgo/funk/version.buildTime={{ .CommitDate }}'
18+
- -X 'github.com/pubgo/funk/version.commitID={{ .ShortCommit }}'
19+
flags:
20+
- -trimpath
21+
- -tags=releaser
22+
mod_timestamp: '{{ .CommitTimestamp }}'
23+
archives:
24+
- format: tar.gz
25+
name_template: >-
26+
{{ .ProjectName }}_
27+
{{- title .Os }}_
28+
{{- if eq .Arch "amd64" }}x86_64
29+
{{- else if eq .Arch "386" }}i386
30+
{{- else }}{{ .Arch }}{{ end }}
31+
{{- if .Arm }}v{{ .Arm }}{{ end }}
32+
format_overrides:
33+
- goos: windows
34+
format: zip
35+
report_sizes: true
36+
checksum:
37+
name_template: 'checksums.txt'
38+
snapshot:
39+
name_template: "{{ incpatch .Version }}-next"
40+
changelog:
41+
sort: asc
42+
filters:
43+
exclude:
44+
- '^docs:'
45+
- '^test:'

bootstrap/boot.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/charmbracelet/x/term"
1515
"github.com/pubgo/dix"
1616
"github.com/pubgo/dix/dix_internal"
17+
"github.com/pubgo/fastcommit/cmds/tagcmd"
1718
"github.com/pubgo/fastcommit/cmds/versioncmd"
1819
"github.com/pubgo/fastcommit/utils"
1920
"github.com/pubgo/funk/assert"
@@ -37,6 +38,8 @@ var defaultConfig []byte
3738
func Main() {
3839
defer recovery.Exit()
3940

41+
var branchName = string(assert.Exit1(utils.ShellOutput("git", "rev-parse", "--abbrev-ref", "HEAD")))
42+
4043
slog.Info("config path", "path", configPath)
4144
if pathutil.IsNotExist(configPath) {
4245
assert.Must(os.WriteFile(configPath, defaultConfig, 0644))
@@ -47,6 +50,7 @@ func Main() {
4750
dix_internal.SetLogLevel(zerolog.InfoLevel)
4851
var di = dix.New(dix.WithValuesNull())
4952
di.Provide(versioncmd.New)
53+
di.Provide(tagcmd.New)
5054
di.Provide(config.Load[Config])
5155
di.Provide(utils.NewOpenaiClient)
5256

@@ -129,7 +133,7 @@ func Main() {
129133

130134
msg := resp.Choices[0].Message.Content
131135
assert.Must(utils.Shell("git", "commit", "-m", fmt.Sprintf("'%s'", msg)).Run())
132-
assert.Must(utils.Shell("git", "push").Run())
136+
assert.Must(utils.Shell("git", "push", "origin", branchName).Run())
133137

134138
return nil
135139
},

cmds/tagcmd/cmd.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package tagcmd
2+
3+
import (
4+
"context"
5+
6+
"github.com/pubgo/fastcommit/utils"
7+
"github.com/pubgo/funk/recovery"
8+
"github.com/urfave/cli/v3"
9+
)
10+
11+
func New() *cli.Command {
12+
return &cli.Command{
13+
Name: "tag",
14+
Action: func(ctx context.Context, command *cli.Command) error {
15+
defer recovery.Exit()
16+
ver := utils.GetNextTag("alpha")
17+
utils.GitTag(ver.Original())
18+
return nil
19+
},
20+
}
21+
}

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ require (
66
github.com/adrg/xdg v0.5.3
77
github.com/charmbracelet/bubbletea v1.2.4
88
github.com/charmbracelet/x/term v0.2.1
9+
github.com/hashicorp/go-version v1.7.0
910
github.com/pubgo/dix v0.3.19
10-
github.com/pubgo/funk v0.5.63
11+
github.com/pubgo/funk v0.5.64-alpha.1
1112
github.com/rs/zerolog v1.33.0
13+
github.com/samber/lo v1.47.0
1214
github.com/sashabaranov/go-openai v1.36.1
1315
github.com/urfave/cli/v3 v3.0.0-alpha9.0.20240717192922-127cf54fac9f
1416
)
@@ -25,7 +27,6 @@ require (
2527
github.com/expr-lang/expr v1.16.9 // indirect
2628
github.com/goccy/go-json v0.10.2 // indirect
2729
github.com/golang/protobuf v1.5.4 // indirect
28-
github.com/hashicorp/go-version v1.7.0 // indirect
2930
github.com/joho/godotenv v1.5.1 // indirect
3031
github.com/k0kubun/pp/v3 v3.2.0 // indirect
3132
github.com/kr/text v0.2.0 // indirect
@@ -41,7 +42,6 @@ require (
4142
github.com/rivo/uniseg v0.4.7 // indirect
4243
github.com/rogpeppe/go-internal v1.12.0 // indirect
4344
github.com/rs/xid v1.5.0 // indirect
44-
github.com/samber/lo v1.47.0 // indirect
4545
github.com/valyala/bytebufferpool v1.0.0 // indirect
4646
github.com/valyala/fasttemplate v1.2.2 // indirect
4747
go.uber.org/atomic v1.11.0 // indirect

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
7171
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
7272
github.com/pubgo/dix v0.3.19 h1:a4RRmljw7ePUvc9uJKqXXs6JpYL8s3VqPP2NV2vxI9U=
7373
github.com/pubgo/dix v0.3.19/go.mod h1:J0PyuMm7mW6ZrN13l9xz0rtKbzliREFyvb7fuaGYyVw=
74-
github.com/pubgo/funk v0.5.63 h1:tZCwwx3YzdUvFhb9lXOwQ5kyvSmDBv+zWjq7iRk+/Qc=
75-
github.com/pubgo/funk v0.5.63/go.mod h1:VMNDJsIFOSMUZ2PRfyIyUQkDpfnFnOYZelFa1r/dmgs=
74+
github.com/pubgo/funk v0.5.64-alpha.1 h1:RPS5k4c7qcjN3VT42p7O9sJIL7Nv0gu4ZwQi5M4fIIY=
75+
github.com/pubgo/funk v0.5.64-alpha.1/go.mod h1:VMNDJsIFOSMUZ2PRfyIyUQkDpfnFnOYZelFa1r/dmgs=
7676
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
7777
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
7878
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=

utils/git.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os/exec"
88
"strings"
99

10+
"github.com/pubgo/funk/assert"
1011
"github.com/pubgo/funk/errors"
1112
)
1213

@@ -85,3 +86,14 @@ func Shell(args ...string) *exec.Cmd {
8586
cmd.Stderr = os.Stderr
8687
return cmd
8788
}
89+
90+
func ShellOutput(args ...string) ([]byte, error) {
91+
cmd := Shell(args...)
92+
cmd.Stdout = nil
93+
return cmd.Output()
94+
}
95+
96+
func GitTag(ver string) {
97+
assert.Must(Shell("git", "tag", ver).Run())
98+
assert.Must(Shell("git", "push", "origin", ver).Run())
99+
}

utils/util.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,70 @@ import (
55
"fmt"
66
"os"
77
"os/signal"
8+
"strconv"
89
"strings"
910
"syscall"
11+
12+
"github.com/pubgo/funk/typex"
13+
semver "github.com/hashicorp/go-version"
14+
"github.com/pubgo/funk/assert"
15+
"github.com/samber/lo"
1016
)
1117

18+
func GetGitTags() []*semver.Version {
19+
var tagText = strings.TrimSpace(string(assert.Exit1(ShellOutput("git", "tag"))))
20+
var tags = strings.Split(tagText, "\n")
21+
var versions = make([]*semver.Version, 0, len(tags))
22+
23+
for _, tag := range tags {
24+
tag = strings.TrimSpace(tag)
25+
vv, err := semver.NewSemver(tag)
26+
if err != nil {
27+
continue
28+
}
29+
versions = append(versions, vv)
30+
}
31+
return versions
32+
}
33+
34+
func GetNextTag(pre string) *semver.Version {
35+
var tags = GetGitTags()
36+
var maxVer = GetGitMaxTag(tags)
37+
var preData = fmt.Sprintf("-%s.", pre)
38+
var curMaxVer = typex.DoBlock1(func() *semver.Version {
39+
preTags := lo.Filter(tags, func(item *semver.Version, index int) bool { return strings.Contains(item.String(), preData) })
40+
var curMaxVer = lo.MaxBy(preTags, func(a *semver.Version, b *semver.Version) bool { return a.Compare(b) > 0 })
41+
return curMaxVer
42+
})
43+
44+
var ver string
45+
if curMaxVer != nil && curMaxVer.GreaterThan(maxVer) {
46+
ver = strings.ReplaceAll(curMaxVer.Prerelease(), fmt.Sprintf("%s.", pre), "")
47+
ver = fmt.Sprintf("v%s-%s.%d", curMaxVer.Core().String(), pre, assert.Must1(strconv.Atoi(ver))+1)
48+
} else {
49+
ver = fmt.Sprintf("v%s-%s.1", maxVer.Core().String(), pre)
50+
}
51+
return assert.Exit1(semver.NewSemver(ver))
52+
}
53+
54+
func GetGitMaxTag(tags []*semver.Version) *semver.Version {
55+
var maxVer = semver.Must(semver.NewVersion("v0.0.0"))
56+
57+
for _, tag := range tags {
58+
if strings.Contains(tag.String(), "-") {
59+
continue
60+
}
61+
62+
if maxVer.Compare(tag) >= 0 {
63+
continue
64+
}
65+
66+
maxVer = tag
67+
}
68+
69+
return maxVer
70+
}
71+
1272
func UsageDesc(format string, args ...interface{}) string {
1373
s := fmt.Sprintf(format, args...)
1474
return strings.ToUpper(s[0:1]) + s[1:]

0 commit comments

Comments
 (0)