-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
188 lines (175 loc) · 4.91 KB
/
main.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
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
package main
import (
"archive/zip"
"flag"
"fmt"
"github.com/buger/jsonparser"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"io"
"log"
"math/rand"
"os"
"path"
"time"
)
func doMain(input *string, output *string) {
zipReader, err := zip.OpenReader(*input)
if err != nil {
fmt.Println("Unable to open ", *input)
log.Fatal(err)
}
defer zipReader.Close()
err = os.Mkdir(*output, os.ModePerm)
needToInit := true
if err != nil {
fmt.Println("Unable to create existing directory, expecting a git repository", *output)
needToInit = false
}
var repo *git.Repository
if needToInit {
repo, err = git.PlainInit(*output, false)
if err != nil {
fmt.Println("Failed to init git repository")
log.Fatal(err)
}
} else {
repo, err = git.PlainOpen(*output)
if err != nil {
fmt.Println("Failed to open existing git repository")
log.Fatal(err)
}
}
w, err := repo.Worktree()
if err != nil {
fmt.Println("Failed to open existing git repository")
log.Fatal(err)
}
for _, file := range zipReader.File {
if file.Name == "source/notes.json" {
processNotes(output, w, file)
}
}
}
func processActiveNote(output *string, w *git.Worktree) func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
return func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
content, _ := jsonparser.GetString(value, "content")
creationDate, _ := jsonparser.GetString(value, "creationDate")
lastModified, _ := jsonparser.GetString(value, "lastModified")
filename := writeFile(&content, output, &creationDate, &lastModified)
if len(filename) > 0 {
commitFile(w, &filename, "Exported "+filename)
}
}
}
func processTrashedNote(folder *string, w *git.Worktree) func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
return func(value []byte, dataType jsonparser.ValueType, offset int, err error) {
content, _ := jsonparser.GetString(value, "content")
creationDate, _ := jsonparser.GetString(value, "creationDate")
lastModified, _ := jsonparser.GetString(value, "lastModified")
filename := writeFile(&content, folder, &creationDate, &lastModified)
if len(filename) > 0 {
commitFile(w, &filename, "Exported to delete "+filename)
err2 := os.Remove(path.Join(*folder, filename))
if err2 != nil {
fmt.Println("Unable to remove file from worktree: ", filename)
log.Fatal(err2)
}
commitFile(w, &filename, "Deleted "+filename)
}
}
}
func commitFile(w *git.Worktree, filename *string, commitMessage string) {
_, err := w.Add(*filename)
if err != nil {
fmt.Println("Unable to add file to worktree: ", filename)
log.Fatal(err)
}
_, err2 := w.Commit(commitMessage, &git.CommitOptions{
Author: &object.Signature{
Name: "GitJournal Exporter",
Email: "a@b.c",
When: time.Now(),
},
})
if err2 != nil {
fmt.Println("Unable to commit file:", filename)
log.Fatal(err)
}
}
func writeFile(content *string, folder *string, creationDate *string, lastModified *string) string {
extractedName := extractFileName(content)
if len(extractedName) == 0 {
return ""
}
filename := extractedName + ".md"
_, err := os.Stat(path.Join(*folder, filename))
if !os.IsNotExist(err) {
filename = extractedName + "_" + randomSuffix(5) + ".md"
}
file, err := os.Create(path.Join(*folder, filename))
if err != nil {
fmt.Println("Unable to create new file:", filename)
log.Fatal(err)
}
defer file.Close()
fmt.Fprintln(file, "---")
fmt.Fprintln(file, "created:", *creationDate)
fmt.Fprintln(file, "modified:", *lastModified)
fmt.Fprintln(file, "---")
fmt.Fprint(file, *content)
return filename
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randomSuffix(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func extractFileName(content *string) string {
size := 0
filename := make([]rune, 0, 128)
for _, char := range *content {
if size >= 64 {
break
}
if char == '\n' {
break
}
if char == os.PathSeparator || char == '\r' {
continue
}
filename = append(filename, char)
size++
}
return string(filename)
}
func processNotes(output *string, w *git.Worktree, file *zip.File) {
data, err := file.Open()
if err != nil {
fmt.Println("Unable to read source/notes.json from archive")
log.Fatal(err)
}
defer data.Close()
json, err := io.ReadAll(data)
if err != nil {
fmt.Println("Unable to read source/notes.json from archive")
log.Fatal(err)
}
_, err = jsonparser.ArrayEach(json, processActiveNote(output, w), "activeNotes")
if err != nil {
log.Fatal(err)
}
_, err = jsonparser.ArrayEach(json, processTrashedNote(output, w), "trashedNotes")
if err != nil {
log.Fatal(err)
}
}
func main() {
input := flag.String("input", "notes.zip", "Archive file, exported from simplenote.com")
output := flag.String("output", ".", "Output folder (GitJournal repo)")
flag.Parse()
doMain(input, output)
}