-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (63 loc) · 1.62 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
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/weakish/gosugar"
)
type post struct {
PostId int `json:"post_id"`
DateAdded int64 `json:"date_added"`
Body string `json:"body"`
Meta string `json:"meta"`
}
func main() {
bytes, _ := ioutil.ReadFile("data.json")
var user map[string]json.RawMessage
_ = json.Unmarshal(bytes, &user)
var posts []post
_ = json.Unmarshal(user["post"], &posts)
var images []string
images, _ = filepath.Glob("*.jpg")
var imageIds gosugar.StringSet = gosugar.NewStringSet()
for _, image := range images {
imageIds.Add(strings.TrimSuffix(image, ".jpg"))
}
prefix := flag.String("p", "", "site prefix")
repo := flag.String("r", ".", "repository directory")
name := flag.String("n", "zerome", "git author name")
email := flag.String("e", "", "git author email")
flag.Parse()
for _, p := range posts {
var id string = strconv.Itoa(p.PostId)
var date time.Time = time.Unix(p.DateAdded, 0).UTC()
if *prefix == "" {
fmt.Println(p.Body + "\n")
if imageIds.Contains(id) {
fmt.Println("![attached image](" + id + ".jpg)\n")
}
fmt.Println("-- " + date.Format("2006-01-02") + "\n\n")
} else {
body := p.Body + "\n"
if imageIds.Contains(id) {
body += "![attached image](" + *prefix + "/" + id + ".jpg)\n"
}
r, _ := git.PlainOpen(*repo)
w, _ := r.Worktree()
w.Commit(body, &git.CommitOptions{
Author: &object.Signature{
Name: *name,
Email: *email,
When: date,
},
})
}
}
}