-
Notifications
You must be signed in to change notification settings - Fork 26
/
main.go
371 lines (331 loc) · 8.05 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
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package main
import (
"fmt"
"os"
"path/filepath"
"regexp"
"sort"
"strings"
"time"
"github.com/urfave/cli/v2"
)
var errCommandHelp = fmt.Errorf("command help shown")
func newApp() *cli.App {
app := cli.NewApp()
app.Commands = []*cli.Command{
commandPull,
commandFetch,
commandPush,
commandPost,
commandList,
commandRemove,
}
app.Flags = []cli.Flag{
&cli.StringFlag{
Name: "C",
Usage: "Run as if blogsync was started in `PATH` instead of the current working directory. ",
EnvVars: []string{"BLOGSYNC_WORKDIR"},
Action: func(ctx *cli.Context, wdir string) error {
if wdir == "" {
return nil
}
return os.Chdir(wdir)
},
},
}
app.Version = fmt.Sprintf("%s (%s)", version, revision)
return app
}
func main() {
if err := newApp().Run(os.Args); err != nil {
if err != errCommandHelp {
logf("error", "%s", err)
}
os.Exit(1)
}
}
var commandPull = &cli.Command{
Name: "pull",
Usage: "Pull entries from remote",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "no-drafts"},
&cli.BoolFlag{Name: "only-drafts"},
},
Action: func(c *cli.Context) error {
conf, err := loadConfiguration()
if err != nil {
return err
}
blogs := c.Args().Slice()
if len(blogs) == 0 {
blogs = conf.localBlogIDs()
}
if len(blogs) == 0 {
cli.ShowCommandHelp(c, "pull")
return errCommandHelp
}
for _, blog := range blogs {
blogConfig := conf.Get(blog)
if blogConfig == nil {
return fmt.Errorf("blog not found: %s", blog)
}
b := newBroker(blogConfig, c.App.Writer)
remoteEntries, err := b.FetchRemoteEntries(
!c.Bool("only-drafts"), !c.Bool("no-drafts"))
if err != nil {
return err
}
for _, re := range remoteEntries {
path := b.LocalPath(re)
_, err := b.StoreFresh(re, path)
if err != nil {
return err
}
}
}
return nil
},
}
var commandFetch = &cli.Command{
Name: "fetch",
Usage: "Fetch entries from remote",
Action: func(c *cli.Context) error {
first := c.Args().First()
if first == "" {
cli.ShowCommandHelp(c, "fetch")
return errCommandHelp
}
conf, err := loadConfiguration()
if err != nil {
return err
}
for _, path := range c.Args().Slice() {
e, err := entryFromFile(path)
if err != nil {
return err
}
blogID, err := e.blogID()
if err != nil {
return err
}
bc := conf.Get(blogID)
if bc == nil {
return fmt.Errorf("cannot find blog for %s", path)
}
b := newBroker(bc, c.App.Writer)
re, err := asEntry(b.GetEntry(e.EditURL))
if err != nil {
return err
}
if _, err := b.StoreFresh(re, path); err != nil {
return err
}
}
return nil
},
}
var (
// 標準フォーマット: 2011/11/07/161845
defaultBlogPathReg = regexp.MustCompile(`^2[01][0-9]{2}/[01][0-9]/[0-3][0-9]/[0-9]{6}$`)
// はてなダイアリー風フォーマット: 20111107/1320650325
hatenaDiaryPathReg = regexp.MustCompile(`^2[01][0-9]{2}[01][0-9][0-3][0-9]/[0-9]{9,12}$`)
// タイトルフォーマット: 2011/11/07/週末は川に行きました
titlePathReg = regexp.MustCompile(`^2[01][0-9]{2}/[01][0-9]/[0-3][0-9]/.+$`)
draftDir = "_draft/"
)
func isLikelyGivenPath(p string) bool {
return defaultBlogPathReg.MatchString(p) ||
hatenaDiaryPathReg.MatchString(p) ||
titlePathReg.MatchString(p)
}
var commandPush = &cli.Command{
Name: "push",
Usage: "Push local entries to remote",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "publish"},
},
Action: func(c *cli.Context) error {
first := c.Args().First()
if first == "" {
cli.ShowCommandHelp(c, "push")
return errCommandHelp
}
publish := c.Bool("publish")
conf, err := loadConfiguration()
if err != nil {
return err
}
for _, path := range c.Args().Slice() {
if !filepath.IsAbs(path) {
var err error
path, err = filepath.Abs(path)
if err != nil {
return err
}
}
entry, err := entryFromFile(path)
if err != nil {
return err
}
if publish && entry.IsDraft {
entry.IsDraft = false
// Assume it has been edited and update modtime.
ti := time.Now()
entry.LastModified = &ti
}
if entry.EditURL == "" {
// post new entry
bc := conf.detectBlogConfig(path)
if bc == nil {
return fmt.Errorf("cannot find blog for %q", path)
}
// The entry directory is not always at the top of the localRoot, such as
// in the case of using subdirectory feature in BlogMedia. Therefore, the
// relative position from the entry directory is obtained as a custom path as below.
blogPath, _ := filepath.Rel(bc.localRoot(), path)
blogPath = "/" + filepath.ToSlash(blogPath)
_, entryPath := extractEntryPath(path)
if entryPath == "" {
return fmt.Errorf("%q is not a blog entry", path)
}
entry.CustomPath = entryPath
b := newBroker(bc, c.App.Writer)
err = b.PostEntry(entry, false)
if err != nil {
return err
}
continue
}
blogID, err := entry.blogID()
if err != nil {
return err
}
bc := conf.Get(blogID)
if bc == nil {
return fmt.Errorf("cannot find blog for %s", path)
}
blogPath, _ := filepath.Rel(bc.localRoot(), path)
blogPath = "/" + filepath.ToSlash(blogPath)
if _, entryPath := extractEntryPath(path); entryPath != "" {
if !isLikelyGivenPath(entryPath) && !strings.HasPrefix(entryPath, draftDir) {
entry.CustomPath = entryPath
}
}
_, err = newBroker(bc, c.App.Writer).UploadFresh(entry)
if err != nil {
return err
}
}
return nil
},
}
var commandPost = &cli.Command{
Name: "post",
Usage: "Post a new entry to remote",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "draft"},
&cli.StringFlag{Name: "title"},
&cli.StringFlag{Name: "custom-path"},
&cli.BoolFlag{Name: "page"},
},
Action: func(c *cli.Context) error {
blog := c.Args().First()
if blog == "" {
cli.ShowCommandHelp(c, "post")
return errCommandHelp
}
conf, err := loadConfiguration()
if err != nil {
return err
}
blogConfig := conf.Get(blog)
if blogConfig == nil {
return fmt.Errorf("blog not found: %s", blog)
}
entry, err := entryFromReader(c.App.Reader)
if err != nil {
return err
}
if c.Bool("draft") {
entry.IsDraft = true
}
if path := c.String("custom-path"); path != "" {
entry.CustomPath = path
}
if title := c.String("title"); title != "" {
entry.Title = title
}
b := newBroker(blogConfig, c.App.Writer)
err = b.PostEntry(entry, c.Bool("page"))
if err != nil {
return err
}
return nil
},
}
var commandList = &cli.Command{
Name: "list",
Usage: "List local blogs",
Action: func(c *cli.Context) error {
conf, err := loadConfiguration()
if err != nil {
return err
}
blogs := make([]*struct{ url, fullPath string }, 0, len(conf.Blogs))
maxURLLen := 0
for blogID := range conf.Blogs {
urlLen := len(blogID)
if urlLen > maxURLLen {
maxURLLen = urlLen
}
blogConfig := conf.Get(blogID)
var fullPath string
if blogConfig.OmitDomain == nil || !*blogConfig.OmitDomain {
fullPath = filepath.Join(blogConfig.LocalRoot, blogConfig.BlogID)
} else {
fullPath = blogConfig.LocalRoot
}
blogs = append(blogs, &struct{ url, fullPath string }{blogID, fullPath})
}
sort.Slice(blogs, func(i, j int) bool { return blogs[i].url < blogs[j].url })
for _, blog := range blogs {
del := strings.Repeat(" ", maxURLLen-len(blog.url)+1)
fmt.Printf("%s%s%s\n", blog.url, del, blog.fullPath)
}
return nil
},
}
var commandRemove = &cli.Command{
Name: "remove",
Usage: "Remove blog entries",
Action: func(c *cli.Context) error {
first := c.Args().First()
if first == "" {
cli.ShowCommandHelp(c, "remove")
return errCommandHelp
}
conf, err := loadConfiguration()
if err != nil {
return err
}
for _, path := range c.Args().Slice() {
entry, err := entryFromFile(path)
if err != nil {
return err
}
blogID, err := entry.blogID()
if err != nil {
return err
}
bc := conf.Get(blogID)
if bc == nil {
return fmt.Errorf("cannot find blog for %s", path)
}
err = newBroker(bc, c.App.Writer).RemoveEntry(entry)
if err != nil {
return err
}
}
return nil
},
}