Skip to content

Commit

Permalink
Fix/release docs (#4)
Browse files Browse the repository at this point in the history
* fix: barry 2025-01-04 22:19:39

* refactor(tagcmd): improve tag generation logic and exit handling

* chore: remove duplicate linux_amd64 build target

* fix: barry 2025-01-05 09:56:33

* fix: barry 2025-01-05 10:00:42

* fix: barry 2025-01-05 10:01:13

* refactor: move config-related code to configs package

* fix: barry 2025-01-05 10:18:32

* refactor: improve logging and UI message clarity

* feat: add env command and config support

* fix: barry 2025-01-05 11:00:37

* fix: barry 2025-01-05 11:05:09
  • Loading branch information
kooksee authored Jan 5, 2025
1 parent 093df05 commit 8aed62c
Show file tree
Hide file tree
Showing 13 changed files with 154 additions and 40 deletions.
31 changes: 28 additions & 3 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ builds:
- "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'
Expand All @@ -37,9 +36,35 @@ checksum:
name_template: 'checksums.txt'
snapshot:
name_template: "{{ incpatch .Version }}-next"

changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
- '^docs'
- '^test'
- '^chore'
include:
- '^feat'
- '^fix'
- '^perf'
- '^refactor'
use: git
groups:
- title: "New Features"
regexp: "^feat"
- title: "Bug Fixes"
regexp: "^fix"
- title: "Performance Improvements"
regexp: "^perf"
- title: "Refactors"
regexp: "^refactor"

release:
github:
owner: pubgo
name: fastcommit
name_template: '{{ .ProjectName }} {{ .Tag }}'
footer: |
## Installation
Download the latest release from the [GitHub Releases](https://github.com/pubgo/fastcommit/releases) page.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
agentic git commit generate tool

## Refer

- https://github.com/Nutlope/aicommits
- https://chat.deepseek.com

Expand Down
18 changes: 10 additions & 8 deletions bootstrap/boot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
_ "github.com/charmbracelet/bubbletea"
"github.com/pubgo/dix"
"github.com/pubgo/dix/dix_internal"
"github.com/pubgo/fastcommit/cmds/envcmd"
"github.com/pubgo/fastcommit/cmds/fastcommit"
"github.com/pubgo/fastcommit/cmds/tagcmd"
"github.com/pubgo/fastcommit/cmds/versioncmd"
Expand All @@ -26,36 +27,37 @@ import (
func Main() {
defer recovery.Exit()

slog.Info("config path", "path", configPath)
slog.Info("config path", "path", configs.GetConfigPath())
typex.DoBlock(func() {
if pathutil.IsNotExist(configPath) {
assert.Must(os.WriteFile(configPath, defaultConfig, 0644))
if pathutil.IsNotExist(configs.GetConfigPath()) {
assert.Must(os.WriteFile(configs.GetConfigPath(), configs.GetDefaultConfig(), 0644))
return
}

var cfg ConfigProvider
config.LoadFromPath(&cfg, configPath)
config.LoadFromPath(&cfg, configs.GetConfigPath())

var defaultCfg ConfigProvider
assert.Exit(yaml.Unmarshal(defaultConfig, &defaultCfg))
assert.Exit(yaml.Unmarshal(configs.GetDefaultConfig(), &defaultCfg))
if cfg.Version == nil || cfg.Version.Name == "" || defaultCfg.Version.Name != cfg.Version.Name {
assert.Exit(os.WriteFile(configPath, defaultConfig, 0644))
assert.Exit(os.WriteFile(configs.GetConfigPath(), configs.GetDefaultConfig(), 0644))
}
})

config.SetConfigPath(configPath)
config.SetConfigPath(configs.GetConfigPath())
dix_internal.SetLogLevel(zerolog.InfoLevel)

var di = dix.New(dix.WithValuesNull())
di.Provide(versioncmd.New)
di.Provide(func() *configs.Config {
return &configs.Config{
BranchName: branchName,
BranchName: configs.GetBranchName(),
}
})
di.Provide(tagcmd.New)
di.Provide(config.Load[ConfigProvider])
di.Provide(utils.NewOpenaiClient)
di.Provide(envcmd.New)
di.Provide(fastcommit.New)
di.Inject(func(cmd *fastcommit.Command) { cmd.Run() })
}
10 changes: 0 additions & 10 deletions bootstrap/config.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
package bootstrap

import (
_ "embed"

"github.com/adrg/xdg"
"github.com/pubgo/fastcommit/configs"
"github.com/pubgo/fastcommit/utils"
"github.com/pubgo/funk/assert"
)

type ConfigProvider struct {
Version *configs.Version `yaml:"version"`
OpenaiConfig *utils.OpenaiConfig `yaml:"openai"`
}

var configPath = assert.Exit1(xdg.ConfigFile("fastcommit/config.yaml"))
var branchName = assert.Exit1(utils.RunOutput("git", "rev-parse", "--abbrev-ref", "HEAD"))

//go:embed default.yaml
var defaultConfig []byte
33 changes: 33 additions & 0 deletions cmds/envcmd/cmd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package envcmd

import (
"context"

"github.com/pubgo/fastcommit/configs"
"github.com/pubgo/funk/assert"
"github.com/pubgo/funk/pretty"
"github.com/pubgo/funk/recovery"
"github.com/samber/lo"
"github.com/urfave/cli/v3"
"gopkg.in/yaml.v3"
)

func New() *cli.Command {
return &cli.Command{
Name: "env",
Usage: "show all envs",
Action: func(ctx context.Context, command *cli.Command) error {
defer recovery.Exit()
var envData = configs.GetEnvConfig()
var envMap = make(map[string]*configs.EnvConfig)
assert.Must(yaml.Unmarshal(envData, &envMap))
for name := range envMap {
envMap[name].Name = name
}

pretty.Println(lo.Values(envMap))

return nil
},
}
}
6 changes: 3 additions & 3 deletions cmds/fastcommit/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"log/slog"
"os"
"sort"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/term"
"github.com/pubgo/dix"
Expand Down Expand Up @@ -49,6 +49,7 @@ func New(params Params) *Command {
},
Commands: params.Cmd,
Action: func(ctx context.Context, command *cli.Command) error {
defer recovery.Exit()
var cfg = params.Cfg
if utils.IsHelp() {
return cli.ShowAppHelp(command)
Expand Down Expand Up @@ -103,7 +104,7 @@ func New(params Params) *Command {
}

msg := resp.Choices[0].Message.Content
fmt.Println(msg)
slog.Info("openai response git message", "msg", msg)
var p1 = tea.NewProgram(InitialTextInputModel(msg))
mm := assert.Must1(p1.Run()).(model2)
if mm.isExit() {
Expand All @@ -128,6 +129,5 @@ type Command struct {

func (c *Command) Run() {
defer recovery.Exit()
sort.Sort(cli.FlagsByName(c.cmd.Flags))
assert.Exit(c.cmd.Run(utils.Context(), os.Args))
}
5 changes: 1 addition & 4 deletions cmds/fastcommit/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,7 @@ func (m model2) Update(msg tea.Msg) (tea.Model, tea.Cmd) {

// View is called to draw the textinput step
func (m model2) View() string {
return fmt.Sprintf(
"git message: %s\n",
m.textInput.View(),
)
return fmt.Sprintf("git message: %s\nPlease update and type key enter\n", m.textInput.View())
}

func (m model2) Value() string {
Expand Down
12 changes: 9 additions & 3 deletions cmds/tagcmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,25 @@ import (

func New() *cli.Command {
return &cli.Command{
Name: "tag",
Name: "tag",
Usage: "gen tag and push origin",
Action: func(ctx context.Context, command *cli.Command) error {
defer recovery.Exit()
var p = tea.NewProgram(initialModel())
m := assert.Must1(p.Run()).(model)
ver := utils.GetNextTag(m.selected)
var tags = utils.GetGitTags()
ver := utils.GetNextTag(m.selected, tags)
if m.selected == "release" {
ver = ver.Core()
ver = utils.GetNextReleaseTag(tags)
}

tagName := "v" + strings.TrimPrefix(ver.Original(), "v")
var p1 = tea.NewProgram(InitialTextInputModel(tagName))
m1 := assert.Must1(p1.Run()).(model2)
if m1.exit {
return nil
}

tagName = m1.Value()
_, err := semver.NewVersion(tagName)
if err != nil {
Expand Down
4 changes: 3 additions & 1 deletion cmds/tagcmd/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package tagcmd
import (
"fmt"
"log/slog"

"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
Expand Down Expand Up @@ -112,6 +112,7 @@ func (m model1) View() string {

type model2 struct {
textInput textinput.Model
exit bool
}

// sanitizeInput verifies that an input text string gets validated
Expand Down Expand Up @@ -151,6 +152,7 @@ func (m model2) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyEnter:
return m, tea.Quit
case tea.KeyCtrlC, tea.KeyEsc:
m.exit = true
return m, tea.Quit
}
}
Expand Down
40 changes: 40 additions & 0 deletions configs/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
package configs

import (
_ "embed"

"github.com/adrg/xdg"
"github.com/pubgo/fastcommit/utils"
"github.com/pubgo/funk/assert"
)

type EnvConfig struct {
Description string `yaml:"description"`
Default string `yaml:"default"`
Name string `yaml:"name"`
Required bool `yaml:"required"`
}

type Config struct {
BranchName string
}

type Version struct {
Name string `yaml:"name"`
}

var configPath = assert.Exit1(xdg.ConfigFile("fastcommit/config.yaml"))
var branchName = assert.Exit1(utils.RunOutput("git", "rev-parse", "--abbrev-ref", "HEAD"))

//go:embed default.yaml
var defaultConfig []byte

//go:embed env.yaml
var envConfig []byte

func GetConfigPath() string {
return configPath
}

func GetBranchName() string {
return branchName
}

func GetDefaultConfig() []byte {
return defaultConfig
}

func GetEnvConfig() []byte {
return envConfig
}
File renamed without changes.
9 changes: 9 additions & 0 deletions configs/env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
OPENAI_API_KEY:
description: "OpenAI API Key"
required: true
OPENAI_BASE_URL:
description: "OpenAI Base URL"
default: "https://api.deepseek.com/v1"
OPENAI_MODEL:
description: "OpenAI Model"
default: "deepseek-chat"
25 changes: 18 additions & 7 deletions utils/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,40 @@ func GetGitTags() []*semver.Version {
tag = strings.TrimSpace(tag)
vv, err := semver.NewSemver(tag)
if err != nil {
slog.Error("failed to parse git tag", "tag", tag, "err", err)
continue
}
versions = append(versions, vv)
}
return versions
}

func GetNextTag(pre string) *semver.Version {
var tags = GetGitTags()
func GetNextReleaseTag(tags []*semver.Version) *semver.Version {
var curMaxVer = typex.DoBlock1(func() *semver.Version {
return lo.MaxBy(tags, func(a *semver.Version, b *semver.Version) bool { return a.Compare(b) > 0 })
})

return curMaxVer.Core()
}

func GetNextTag(pre string, tags []*semver.Version) *semver.Version {
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 })
var curMaxVer = lo.MaxBy(tags, func(a *semver.Version, b *semver.Version) bool { return a.Compare(b) > 0 })
return curMaxVer
})

fmt.Println(curMaxVer.String(), maxVer.String())
var ver string
if curMaxVer != nil && curMaxVer.GreaterThan(maxVer) {
if curMaxVer != nil && curMaxVer.Core().GreaterThan(maxVer) {
ver = strings.ReplaceAll(curMaxVer.Prerelease(), fmt.Sprintf("%s.", pre), "")
if ver == "" {
ver = "1"
}

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)
ver = fmt.Sprintf("v%s-%s.1", maxVer.String(), pre)
}
return assert.Exit1(semver.NewSemver(ver))
}
Expand Down

0 comments on commit 8aed62c

Please sign in to comment.