Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
229 lines (193 loc) · 5.18 KB
/
Copy pathmain.go
File metadata and controls
229 lines (193 loc) · 5.18 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
package main
import (
"archive/zip"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/gogo/protobuf/jsonpb"
"github.com/gogo/protobuf/proto"
"github.com/anyproto/anytype-heart/pb"
"github.com/anyproto/anytype-heart/util/constant"
)
var (
jsonM = jsonpb.Marshaler{Indent: " "}
)
func main() {
// command line flags
unpack := flag.String("unpack", "", "Unpack zip archive with pb files to the directory with json files")
pack := flag.String("pack", "", "Convert json files in a directory to pb and write to a zip file")
flag.Parse()
// check input and output are specified
if (*pack == "" && *unpack == "") || (*pack != "" && *unpack != "") {
log.Fatalf("You should specify either -pack or -unpack")
}
if *pack != "" {
createZipFromDirectory(*pack, *pack+".zip")
return
}
if *unpack != "" {
output := strings.TrimSuffix(*unpack, filepath.Ext(*unpack))
// check if dir exists and create if not
if _, err := os.Stat(output); os.IsNotExist(err) {
if err := os.MkdirAll(output, 0755); err != nil {
log.Fatalf("Failed to create output directory: %v", err)
}
} else {
log.Fatalf("Output directory already exists: %v", output)
}
handleZip(*unpack, output)
}
}
type File struct {
Name string
RC io.ReadCloser
}
func processFile(file File, outputFile string) {
defer file.RC.Close()
if !strings.HasSuffix(file.Name, ".pb") && !strings.HasPrefix(file.Name, constant.ProfileFile) {
// write file as is
if err := os.MkdirAll(filepath.Dir(outputFile), 0755); err != nil {
log.Fatalf("Failed to create output subdirectory: %v", err)
}
// write from reader to file
outFile, err := os.Create(outputFile)
if err != nil {
log.Fatalf("Failed to create output file: %v", err)
}
defer outFile.Close()
if _, err := io.Copy(outFile, file.RC); err != nil {
log.Fatalf("Failed to copy file: %v", err)
}
return
}
// assuming Snapshot is a protobuf message
var snapshot proto.Message = &pb.ChangeSnapshot{}
if strings.HasPrefix(file.Name, constant.ProfileFile) {
snapshot = &pb.Profile{}
}
content, err := ioutil.ReadAll(file.RC)
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
if err := proto.Unmarshal(content, snapshot); err != nil {
snapshot = &pb.SnapshotWithType{}
if err := proto.Unmarshal(content, snapshot); err != nil {
log.Fatalf("Failed to parse protobuf message: %v", err)
}
}
// convert to jsonpb and write to outputFile
jsonData, err := jsonM.MarshalToString(snapshot)
if err != nil {
log.Fatalf("Failed to marshal protobuf message to json: %v", err)
}
outputFile = strings.TrimSuffix(outputFile, filepath.Ext(outputFile)) + ".json"
if err := ioutil.WriteFile(outputFile, []byte(jsonData), 0644); err != nil {
log.Fatalf("Failed to write json file: %v", err)
}
}
func handleZip(input, output string) {
r, err := zip.OpenReader(input)
if err != nil {
log.Fatalf("Failed to open zip: %v", err)
}
defer r.Close()
for _, f := range r.File {
dir := filepath.Dir(f.Name)
if dir != "." {
// nolint: gosec
outputDir := filepath.Join(output, dir)
if _, err := os.Stat(outputDir); os.IsNotExist(err) {
if err := os.MkdirAll(outputDir, 0755); err != nil {
log.Printf("Failed to create output subdirectory: %v\n", err)
return
}
}
}
// assuming we are only working with files, not directories
if f.FileInfo().IsDir() {
continue
}
rc, err := f.Open()
if err != nil {
fmt.Printf("Failed to open file in zip: %v", err)
continue
}
processFile(File{
Name: f.Name,
RC: rc,
}, filepath.Join(output, f.Name))
}
}
func createZipFromDirectory(input, output string) {
// create a new zip file
newZipFile, err := os.Create(output)
if err != nil {
log.Fatalf("Failed to create new zip file: %v", err)
}
defer newZipFile.Close()
w := zip.NewWriter(newZipFile)
defer w.Close()
err = filepath.Walk(input, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
// Get relative path
rel, err := filepath.Rel(input, path)
if err != nil {
return err
}
data, err := ioutil.ReadFile(path)
if err != nil {
return err
}
if !strings.HasSuffix(info.Name(), ".json") {
// pass original file
fw, err := w.Create(rel)
if err != nil {
return err
}
_, err = fw.Write(data)
return nil
}
isProfile := strings.HasPrefix(info.Name(), constant.ProfileFile)
// assuming Snapshot is a protobuf message
var snapshot proto.Message = &pb.ChangeSnapshot{}
if isProfile {
snapshot = &pb.Profile{}
}
err = jsonpb.UnmarshalString(string(data), snapshot)
if err != nil {
snapshot = &pb.SnapshotWithType{}
if err = jsonpb.UnmarshalString(string(data), snapshot); err != nil {
return err
}
}
pbData, err := proto.Marshal(snapshot)
if err != nil {
return err
}
name := strings.TrimSuffix(rel, ".json") + ".pb"
if isProfile {
name = strings.TrimSuffix(name, ".pb")
}
fw, err := w.Create(name)
if err != nil {
return err
}
_, err = fw.Write(pbData)
if err != nil {
return err
}
}
return nil
})
if err != nil {
log.Fatalf("Failed to process directory: %v", err)
}
}