-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrebase.go
66 lines (53 loc) · 1.12 KB
/
rebase.go
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
package rebase
import (
"errors"
"os"
tea "github.com/charmbracelet/bubbletea"
)
func Run(args []string) error {
if len(args) < 1 {
return errors.New("no input file specified")
}
inputPath := args[0]
commits, err := readCommits(inputPath)
if err != nil {
return err
}
commits, err = editCommits(commits)
if err != nil {
return err
}
if err := writeCommits(inputPath, commits); err != nil {
return err
}
return nil
}
func readCommits(filepath string) (Commits, error) {
file, err := os.Open(filepath)
if err != nil {
return nil, err
}
defer file.Close()
return ParseCommits(file), nil
}
func editCommits(commits Commits) ([]Commit, error) {
var m tea.Model = NewModel(commits)
p := tea.NewProgram(m, tea.WithAltScreen())
m, err := p.Run()
if err != nil {
return nil, err
}
return m.(Model).commits, nil
}
func writeCommits(filepath string, commits Commits) error {
file, err := os.OpenFile(filepath, os.O_WRONLY|os.O_TRUNC, os.ModePerm)
if err != nil {
return err
}
for _, c := range commits {
if _, err := file.WriteString(c.String() + "\n"); err != nil {
return err
}
}
return nil
}