Skip to content

Commit 8072683

Browse files
committed
refactor: move branch logging to fastcommit cmd
1 parent c604f89 commit 8072683

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

bootstrap/boot.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package bootstrap
22

33
import (
4-
"log/slog"
54
"os"
65

76
_ "github.com/adrg/xdg"
@@ -27,7 +26,6 @@ import (
2726
func Main() {
2827
defer recovery.Exit()
2928

30-
slog.Info("config: " + configs.GetConfigPath())
3129
typex.DoBlock(func() {
3230
if pathutil.IsNotExist(configs.GetConfigPath()) {
3331
assert.Must(os.WriteFile(configs.GetConfigPath(), configs.GetDefaultConfig(), 0644))
@@ -50,9 +48,7 @@ func Main() {
5048
var di = dix.New(dix.WithValuesNull())
5149
di.Provide(versioncmd.New)
5250
di.Provide(func() *configs.Config {
53-
return &configs.Config{
54-
BranchName: configs.GetBranchName(),
55-
}
51+
return &configs.Config{}
5652
})
5753
di.Provide(tagcmd.New)
5854
di.Provide(config.Load[ConfigProvider])

cmds/fastcommit/cmd.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log/slog"
77
"os"
88
"sort"
9+
"strings"
910

1011
tea "github.com/charmbracelet/bubbletea"
1112
"github.com/charmbracelet/x/term"
@@ -47,10 +48,15 @@ func New(params Params) *Command {
4748
Sources: cli.EnvVars(env.Key("debug"), env.Key("enable_debug")),
4849
},
4950
},
51+
Before: func(ctx context.Context, command *cli.Command) (context.Context, error) {
52+
branchName := configs.GetBranchName()
53+
slog.Info("current branch: " + strings.TrimSpace(branchName))
54+
slog.Info("config: " + configs.GetConfigPath())
55+
return ctx, nil
56+
},
5057
Commands: params.Cmd,
5158
Action: func(ctx context.Context, command *cli.Command) error {
5259
defer recovery.Exit()
53-
var cfg = params.Cfg
5460
if utils.IsHelp() {
5561
return cli.ShowAppHelp(command)
5662
}
@@ -113,7 +119,7 @@ func New(params Params) *Command {
113119

114120
msg = mm.Value()
115121
assert.Must(utils.RunShell("git", "commit", "-m", fmt.Sprintf("'%s'", msg)))
116-
assert.Must(utils.RunShell("git", "push", "origin", cfg.BranchName))
122+
assert.Must(utils.RunShell("git", "push", "origin", configs.GetBranchName()))
117123

118124
return nil
119125
},

configs/config.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,14 @@ type EnvConfig struct {
1616
}
1717

1818
type Config struct {
19-
BranchName string
2019
}
2120

2221
type Version struct {
2322
Name string `yaml:"name"`
2423
}
2524

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

2928
//go:embed default.yaml
3029
var defaultConfig []byte
@@ -33,10 +32,20 @@ var defaultConfig []byte
3332
var envConfig []byte
3433

3534
func GetConfigPath() string {
35+
if configPath != "" {
36+
return configPath
37+
}
38+
39+
configPath = assert.Exit1(xdg.ConfigFile("fastcommit/config.yaml"))
3640
return configPath
3741
}
3842

3943
func GetBranchName() string {
44+
if branchName != "" {
45+
return branchName
46+
}
47+
48+
branchName = assert.Exit1(utils.RunOutput("git", "rev-parse", "--abbrev-ref", "HEAD"))
4049
return branchName
4150
}
4251

0 commit comments

Comments
 (0)