Skip to content

Commit

Permalink
Feat/release (#2)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kooksee authored Jan 3, 2025
1 parent 8398513 commit eb65eb6
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 6 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
name: Release
on:
push:
tags: [ "v*.*.*" ]

jobs:
release:
name: Release on GitHub
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: release --clean --timeout 60m
env:
GITHUB_TOKEN: ${{ secrets.TOKEN }}
45 changes: 45 additions & 0 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
builds:
- main: ./main.go
id: fastcommit
binary: fastcommit
skip: false
env:
- CGO_ENABLED=0
- GOPROXY=https://goproxy.io
targets:
- "darwin_amd64"
- "windows_amd64"
- "linux_amd64"
- "linux_amd64"
ldflags:
- -X 'github.com/pubgo/funk/version.version={{ .Version }}'
- -X 'github.com/pubgo/funk/version.project=fastcommit'
- -X 'github.com/pubgo/funk/version.buildTime={{ .CommitDate }}'
- -X 'github.com/pubgo/funk/version.commitID={{ .ShortCommit }}'
flags:
- -trimpath
- -tags=releaser
mod_timestamp: '{{ .CommitTimestamp }}'
archives:
- format: tar.gz
name_template: >-
{{ .ProjectName }}_
{{- title .Os }}_
{{- if eq .Arch "amd64" }}x86_64
{{- else if eq .Arch "386" }}i386
{{- else }}{{ .Arch }}{{ end }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
format_overrides:
- goos: windows
format: zip
report_sizes: true
checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"
changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
6 changes: 5 additions & 1 deletion bootstrap/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/charmbracelet/x/term"
"github.com/pubgo/dix"
"github.com/pubgo/dix/dix_internal"
"github.com/pubgo/fastcommit/cmds/tagcmd"
"github.com/pubgo/fastcommit/cmds/versioncmd"
"github.com/pubgo/fastcommit/utils"
"github.com/pubgo/funk/assert"
Expand All @@ -37,6 +38,8 @@ var defaultConfig []byte
func Main() {
defer recovery.Exit()

var branchName = string(assert.Exit1(utils.ShellOutput("git", "rev-parse", "--abbrev-ref", "HEAD")))

slog.Info("config path", "path", configPath)
if pathutil.IsNotExist(configPath) {
assert.Must(os.WriteFile(configPath, defaultConfig, 0644))
Expand All @@ -47,6 +50,7 @@ func Main() {
dix_internal.SetLogLevel(zerolog.InfoLevel)
var di = dix.New(dix.WithValuesNull())
di.Provide(versioncmd.New)
di.Provide(tagcmd.New)
di.Provide(config.Load[Config])
di.Provide(utils.NewOpenaiClient)

Expand Down Expand Up @@ -129,7 +133,7 @@ func Main() {

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

return nil
},
Expand Down
21 changes: 21 additions & 0 deletions cmds/tagcmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tagcmd

import (
"context"

"github.com/pubgo/fastcommit/utils"
"github.com/pubgo/funk/recovery"
"github.com/urfave/cli/v3"
)

func New() *cli.Command {
return &cli.Command{
Name: "tag",
Action: func(ctx context.Context, command *cli.Command) error {
defer recovery.Exit()
ver := utils.GetNextTag("alpha")
utils.GitTag(ver.Original())
return nil
},
}
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ require (
github.com/adrg/xdg v0.5.3
github.com/charmbracelet/bubbletea v1.2.4
github.com/charmbracelet/x/term v0.2.1
github.com/hashicorp/go-version v1.7.0
github.com/pubgo/dix v0.3.19
github.com/pubgo/funk v0.5.63
github.com/pubgo/funk v0.5.64-alpha.1
github.com/rs/zerolog v1.33.0
github.com/samber/lo v1.47.0
github.com/sashabaranov/go-openai v1.36.1
github.com/urfave/cli/v3 v3.0.0-alpha9.0.20240717192922-127cf54fac9f
)
Expand All @@ -25,7 +27,6 @@ require (
github.com/expr-lang/expr v1.16.9 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/hashicorp/go-version v1.7.0 // indirect
github.com/joho/godotenv v1.5.1 // indirect
github.com/k0kubun/pp/v3 v3.2.0 // indirect
github.com/kr/text v0.2.0 // indirect
Expand All @@ -41,7 +42,6 @@ require (
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/rs/xid v1.5.0 // indirect
github.com/samber/lo v1.47.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
go.uber.org/atomic v1.11.0 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pubgo/dix v0.3.19 h1:a4RRmljw7ePUvc9uJKqXXs6JpYL8s3VqPP2NV2vxI9U=
github.com/pubgo/dix v0.3.19/go.mod h1:J0PyuMm7mW6ZrN13l9xz0rtKbzliREFyvb7fuaGYyVw=
github.com/pubgo/funk v0.5.63 h1:tZCwwx3YzdUvFhb9lXOwQ5kyvSmDBv+zWjq7iRk+/Qc=
github.com/pubgo/funk v0.5.63/go.mod h1:VMNDJsIFOSMUZ2PRfyIyUQkDpfnFnOYZelFa1r/dmgs=
github.com/pubgo/funk v0.5.64-alpha.1 h1:RPS5k4c7qcjN3VT42p7O9sJIL7Nv0gu4ZwQi5M4fIIY=
github.com/pubgo/funk v0.5.64-alpha.1/go.mod h1:VMNDJsIFOSMUZ2PRfyIyUQkDpfnFnOYZelFa1r/dmgs=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
Expand Down
12 changes: 12 additions & 0 deletions utils/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"strings"

"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/errors"
)

Expand Down Expand Up @@ -85,3 +86,14 @@ func Shell(args ...string) *exec.Cmd {
cmd.Stderr = os.Stderr
return cmd
}

func ShellOutput(args ...string) ([]byte, error) {
cmd := Shell(args...)
cmd.Stdout = nil
return cmd.Output()
}

func GitTag(ver string) {
assert.Must(Shell("git", "tag", ver).Run())
assert.Must(Shell("git", "push", "origin", ver).Run())
}
60 changes: 60 additions & 0 deletions utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,70 @@ import (
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"

"github.com/pubgo/funk/typex"
semver "github.com/hashicorp/go-version"
"github.com/pubgo/funk/assert"
"github.com/samber/lo"
)

func GetGitTags() []*semver.Version {
var tagText = strings.TrimSpace(string(assert.Exit1(ShellOutput("git", "tag"))))
var tags = strings.Split(tagText, "\n")
var versions = make([]*semver.Version, 0, len(tags))

for _, tag := range tags {
tag = strings.TrimSpace(tag)
vv, err := semver.NewSemver(tag)
if err != nil {
continue
}
versions = append(versions, vv)
}
return versions
}

func GetNextTag(pre string) *semver.Version {
var tags = GetGitTags()
var maxVer = GetGitMaxTag(tags)
var preData = fmt.Sprintf("-%s.", pre)
var curMaxVer = typex.DoBlock1(func() *semver.Version {
preTags := lo.Filter(tags, func(item *semver.Version, index int) bool { return strings.Contains(item.String(), preData) })
var curMaxVer = lo.MaxBy(preTags, func(a *semver.Version, b *semver.Version) bool { return a.Compare(b) > 0 })
return curMaxVer
})

var ver string
if curMaxVer != nil && curMaxVer.GreaterThan(maxVer) {
ver = strings.ReplaceAll(curMaxVer.Prerelease(), fmt.Sprintf("%s.", pre), "")
ver = fmt.Sprintf("v%s-%s.%d", curMaxVer.Core().String(), pre, assert.Must1(strconv.Atoi(ver))+1)
} else {
ver = fmt.Sprintf("v%s-%s.1", maxVer.Core().String(), pre)
}
return assert.Exit1(semver.NewSemver(ver))
}

func GetGitMaxTag(tags []*semver.Version) *semver.Version {
var maxVer = semver.Must(semver.NewVersion("v0.0.0"))

for _, tag := range tags {
if strings.Contains(tag.String(), "-") {
continue
}

if maxVer.Compare(tag) >= 0 {
continue
}

maxVer = tag
}

return maxVer
}

func UsageDesc(format string, args ...interface{}) string {
s := fmt.Sprintf(format, args...)
return strings.ToUpper(s[0:1]) + s[1:]
Expand Down

0 comments on commit eb65eb6

Please sign in to comment.