-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnap.go
150 lines (125 loc) · 2.64 KB
/
snap.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
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
package main
import (
"encoding/json"
"fmt"
"io"
"io/fs"
"log"
"os"
"path/filepath"
"slices"
"strings"
"time"
"github.com/google/uuid"
)
type Snapshot struct {
Name string
Paths []string
Time string
Parent string
}
// CreateIndex returns a slice after recursively walking through
// the directory tree at and below a given path. It omits the ".gcm" and
// the ".git" paths.
func CreateIndex(target string) ([]string, error) {
var paths []string
err := filepath.WalkDir(target, func(path string, d fs.DirEntry, err error) error {
paths = append(paths, path)
return nil
})
if err != nil {
log.Fatal(err)
}
if target == "." {
paths = slices.DeleteFunc(paths, func(s string) bool {
if strings.Contains(s, ".gcm") || strings.Contains(s, ".git") {
return true
}
return false
})
}
return paths, nil
}
func MakeSnapshot() bool {
paths, err := CreateIndex(".")
if err != nil {
log.Fatal(err)
}
snapshotName := uuid.NewString()
for _, path := range paths {
stat, err := os.Stat(path)
if err != nil {
log.Fatal(err)
}
srcPath := path
dstPath := filepath.Join(".gcm/snapshots/"+snapshotName+"/", srcPath)
srcFile, err := os.Open(srcPath)
if err != nil {
log.Fatal(err)
}
defer srcFile.Close()
if stat.IsDir() {
err = os.Mkdir(dstPath, os.ModePerm)
if err != nil {
log.Fatal(err)
}
} else {
err = os.MkdirAll(filepath.Dir(dstPath), os.ModePerm)
if err != nil {
log.Fatal(err)
}
dstFile, err := os.Create(dstPath)
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(dstFile, srcFile)
if err != nil {
log.Fatal(err)
}
}
}
t := time.Now().Format("Mon Jan 2 15:04:05 MST 2006")
AppendSnapshotLog(snapshotName, paths, t, HEAD())
fmt.Printf("Current version saved as %s", snapshotName)
return true
}
func AppendSnapshotLog(snapshotName string, paths []string, t string, parent string) {
snapshots := getSnapshots()
snapshots = append(snapshots, Snapshot{
Name: snapshotName,
Paths: paths,
Time: t,
Parent: HEAD(),
})
data, err := json.Marshal(snapshots)
if err != nil {
log.Fatal(err)
}
err = os.WriteFile(INDEX_PATH, data, 0644)
if err != nil {
log.Fatal(err)
}
err = os.WriteFile(".gcm/HEAD", []byte(snapshotName), 0644)
if err != nil {
log.Fatal(err)
}
}
func getSnapshots() []Snapshot {
var snapshots []Snapshot
jsonFile, err := os.Open(INDEX_PATH)
if err != nil {
log.Fatal(err)
}
byteValue, err := io.ReadAll(jsonFile)
if err != nil {
log.Fatal(err)
}
if string(byteValue) == "" {
return []Snapshot{}
}
err = json.Unmarshal(byteValue, &snapshots)
if err != nil {
log.Fatal(err)
}
return snapshots
}