-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
250 lines (217 loc) · 6.46 KB
/
Copy pathmain.go
File metadata and controls
250 lines (217 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package main
import (
"errors"
"flag"
"fmt"
"io"
"os"
"strconv"
"strings"
"github.com/charmbracelet/huh"
"github.com/tombenner/leancommit/internal/codex"
"github.com/tombenner/leancommit/internal/gitx"
)
const defaultSessionFiles = 5
var demoTitles = []string{
"Add Role-Based Access Controls",
"Improve Invoice Export Workflow",
"Refactor Background Job Retry Handling",
"Add Audit Log Filtering",
"Fix Mobile Navigation State",
}
var (
version = "dev"
commit = "none"
date = "unknown"
)
type config struct {
sessionsDir string
sessionFiles int
dryRun bool
noPush bool
printOnly bool
version bool
demo bool
limit int
}
func main() {
if err := run(); err != nil {
fmt.Fprintf(os.Stderr, "leancommit: %v\n", err)
os.Exit(1)
}
}
func run() error {
return runWith(os.Args[1:], os.LookupEnv, os.Stdout, os.Stdin, os.Stderr, version, selectTitle, gitx.EnsureWorkTree, runGitCommit)
}
type titleSelector func([]string, *string) error
type workTreeChecker func(string) error
type gitCommitter func(gitx.Runner, string, bool) error
func runWith(args []string, getenv func(string) (string, bool), stdout io.Writer, stdin io.Reader, stderr io.Writer, buildVersion string, selectFn titleSelector, ensureWorkTree workTreeChecker, commitFn gitCommitter) error {
cfg, err := parseConfig(args, getenv)
if err != nil {
return err
}
if cfg.version {
fmt.Fprintf(stdout, "leancommit %s (%s, %s)\n", buildVersion, commit, date)
return nil
}
if localDemoMode(buildVersion, cfg) {
return runDemo(cfg, stdout, selectFn)
}
files, err := codex.FindLatestPlanFiles(cfg.sessionsDir, cfg.sessionFiles)
if err != nil {
return err
}
plans, err := codex.ParsePlansFiles(files)
if err != nil {
return err
}
titles := codex.UniqueTitles(plans, cfg.limit)
if len(titles) == 0 {
return fmt.Errorf("%w: %s", codex.ErrNoPlanTitles, cfg.sessionsDir)
}
if cfg.printOnly {
fmt.Fprintln(stdout, titles[0])
return nil
}
if err := ensureWorkTree("."); err != nil {
return err
}
selected := titles[0]
if err := selectTitle(titles, &selected); err != nil {
return err
}
runner := gitx.Runner{
Dir: ".",
Stdin: stdin,
Stdout: stdout,
Stderr: stderr,
DryRun: cfg.dryRun,
}
return commitFn(runner, selected, !cfg.noPush)
}
func runDemo(cfg config, stdout io.Writer, selectFn titleSelector) error {
if cfg.printOnly {
fmt.Fprintln(stdout, demoTitles[0])
return nil
}
selected := demoTitles[0]
if err := selectFn(demoTitles, &selected); err != nil {
return err
}
printDemoGitLogs(stdout, selected)
return nil
}
func localDemoMode(buildVersion string, cfg config) bool {
return buildVersion == "dev" && cfg.demo
}
func printDemoGitLogs(stdout io.Writer, message string) {
fmt.Fprintf(stdout, "[main 543b71c] %s\n", message)
fmt.Fprint(stdout, ` 6 files changed, 163 insertions(+), 93 deletions(-)
Enumerating objects: 25, done.
Counting objects: 100% (25/25), done.
Delta compression using up to 8 threads
Compressing objects: 100% (12/12), done.
Writing objects: 100% (13/13), 3.95 KiB | 3.95 MiB/s, done.
Total 13 (delta 9), reused 0 (delta 0), pack-reused 0 (from 0)
remote: Resolving deltas: 100% (9/9), completed with 9 local objects.
To github.com:myuser/myrepo.git
ebfefaf..543b71c main -> main
`)
}
func runGitCommit(runner gitx.Runner, message string, push bool) error {
return runner.CommitAndMaybePush(message, push)
}
func parseConfig(args []string, getenv func(string) (string, bool)) (config, error) {
home, err := os.UserHomeDir()
if err != nil {
return config{}, fmt.Errorf("find home directory: %w", err)
}
defaultSessionsDir := home + string(os.PathSeparator) + ".codex" + string(os.PathSeparator) + "sessions"
sessionsDirDefault := envString(getenv, "LEANCOMMIT_SESSIONS_DIR", defaultSessionsDir)
sessionFilesDefault, err := envInt(getenv, "LEANCOMMIT_SESSION_FILES", defaultSessionFiles)
if err != nil {
return config{}, err
}
demoDefault, err := envBool(getenv, "LEANCOMMIT_DEMO", false)
if err != nil {
return config{}, err
}
fs := flag.NewFlagSet("leancommit", flag.ContinueOnError)
sessionsDir := fs.String("sessions-dir", sessionsDirDefault, "Codex sessions directory")
sessionFiles := fs.Int("session-files", sessionFilesDefault, "number of newest Codex session files to scan")
dryRun := fs.Bool("dry-run", false, "print git commands without running them")
noPush := fs.Bool("no-push", false, "skip git push")
printOnly := fs.Bool("print", false, "print the newest deduped plan title and exit")
versionFlag := fs.Bool("version", false, "print version information and exit")
limit := fs.Int("limit", 50, "maximum number of unique plan titles to show")
if err := fs.Parse(args); err != nil {
return config{}, err
}
if *limit <= 0 {
return config{}, fmt.Errorf("--limit must be greater than 0")
}
if *sessionFiles <= 0 {
return config{}, fmt.Errorf("--session-files must be greater than 0")
}
return config{
sessionsDir: *sessionsDir,
sessionFiles: *sessionFiles,
dryRun: *dryRun,
noPush: *noPush,
printOnly: *printOnly,
version: *versionFlag,
demo: demoDefault,
limit: *limit,
}, nil
}
func envString(getenv func(string) (string, bool), key, fallback string) string {
value, ok := getenv(key)
if !ok || value == "" {
return fallback
}
return value
}
func envInt(getenv func(string) (string, bool), key string, fallback int) (int, error) {
value, ok := getenv(key)
if !ok || value == "" {
return fallback, nil
}
parsed, err := strconv.Atoi(value)
if err != nil {
return 0, fmt.Errorf("%s must be an integer: %w", key, err)
}
return parsed, nil
}
func envBool(getenv func(string) (string, bool), key string, fallback bool) (bool, error) {
value, ok := getenv(key)
if !ok || value == "" {
return fallback, nil
}
switch strings.ToLower(value) {
case "1", "true", "yes", "on":
return true, nil
case "0", "false", "no", "off":
return false, nil
default:
return false, fmt.Errorf("%s must be one of 1, true, yes, on, 0, false, no, or off", key)
}
}
func selectTitle(titles []string, selected *string) error {
options := make([]huh.Option[string], 0, len(titles))
for _, title := range titles {
options = append(options, huh.NewOption(title, title))
}
err := huh.NewSelect[string]().
Title("Commit message").
Options(options...).
Value(selected).
Run()
if err == nil {
return nil
}
if errors.Is(err, huh.ErrUserAborted) {
return fmt.Errorf("selector canceled")
}
return fmt.Errorf("selector failed: %w", err)
}