-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_script.go
107 lines (101 loc) · 2.8 KB
/
update_script.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
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
package main
import (
"fmt"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/cache"
"github.com/go-git/go-git/v5/storage/filesystem"
cp "github.com/otiai10/copy"
"log"
"os"
"os/exec"
"path"
)
func RunUpdateScript(scriptOutput string, config *UpdateScript, flakeRoot string) (UpdateResult, error) {
tmpDir, err := os.MkdirTemp("", "")
if err != nil {
return UpdateResult{}, fmt.Errorf("os.MkdirTemp: %w", err)
}
defer func() {
_ = os.RemoveAll(tmpDir)
}()
dotGitPath := path.Join(flakeRoot, ".git")
copyOpts := cp.Options{
Skip: func(srcinfo os.FileInfo, src, dest string) (bool, error) {
return src == dotGitPath, nil
},
}
if err := cp.Copy(flakeRoot, tmpDir, copyOpts); err != nil {
return UpdateResult{}, fmt.Errorf("cp.Copy: %w", err)
}
worktree, err := prepareGit(tmpDir)
if err != nil {
return UpdateResult{}, fmt.Errorf("prepareGit: %w", err)
}
executable := path.Join(scriptOutput, config.Executable)
cmd := exec.Cmd{
Path: executable,
Args: append([]string{executable}, config.Args...),
Dir: tmpDir,
Stdout: os.Stdout,
Stderr: os.Stderr,
}
if err := cmd.Run(); err != nil {
return UpdateResult{}, fmt.Errorf("exec.Cmd: %w", err)
}
out, err := diffGit(worktree)
if err != nil {
return UpdateResult{}, fmt.Errorf("diffGit: %w", err)
}
for _, changedPath := range out.getPathsChanged() {
log.Printf("Copying updated file=%s", changedPath)
if err := cp.Copy(path.Join(tmpDir, changedPath), path.Join(flakeRoot, changedPath)); err != nil {
return UpdateResult{}, fmt.Errorf("cp.Copy %s: %w", changedPath, err)
}
}
return out, nil
}
func prepareGit(root string) (*git.Worktree, error) {
if err := deleteGit(root); err != nil {
return nil, fmt.Errorf("deleteGit: %w", err)
}
fs := osfs.New(root)
fscache := cache.NewObjectLRU(10000)
storer := filesystem.NewStorage(fs, fscache)
repo, err := git.Init(storer, fs)
if err != nil {
return nil, fmt.Errorf("git.Init: %w", err)
}
wt, err := repo.Worktree()
if err != nil {
return nil, fmt.Errorf("git.Worktree: %w", err)
}
if err := wt.AddWithOptions(&git.AddOptions{All: true}); err != nil {
return nil, fmt.Errorf("git.AddWithOptions: %w", err)
}
return wt, nil
}
func deleteGit(root string) error {
gitRoot := path.Join(root, ".git")
if err := os.RemoveAll(gitRoot); err != nil {
return fmt.Errorf("os.RemoveAll: %w", err)
}
return nil
}
func diffGit(wt *git.Worktree) (UpdateResult, error) {
status, err := wt.Status()
if err != nil {
return UpdateResult{}, fmt.Errorf("git.Status: %w", err)
}
out := NewUpdateResult()
for filename, maybeFileStatus := range status {
if maybeFileStatus == nil {
continue
}
fileStatus := *maybeFileStatus
if fileStatus.Worktree == git.Modified {
out.addPath(filename)
}
}
return out, nil
}