-
Notifications
You must be signed in to change notification settings - Fork 6
/
migration.go
50 lines (40 loc) · 1.16 KB
/
migration.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
package service
import (
"fmt"
"path/filepath"
"github.com/catatsuy/movabletype"
"github.com/pkg/errors"
"github.com/stormcat24/hateblo2hugo/helper"
"github.com/stormcat24/hateblo2hugo/hugo"
)
type Migration interface {
Execute() error
OutputFilePath() string
}
type MigrationImpl struct {
entry *movabletype.Entry
outputDirRoot string
outputEntryRoot string
}
func NewMigration(entry *movabletype.Entry, outputDirRoot string) Migration {
return &MigrationImpl{
entry: entry,
outputDirRoot: outputDirRoot,
outputEntryRoot: fmt.Sprintf("%s/content/post/entry", outputDirRoot),
}
}
func (s *MigrationImpl) Execute() error {
outpath := filepath.Join(s.outputEntryRoot, s.OutputFilePath())
page := hugo.CreateHugoPage(s.entry)
content, err := page.Render()
if err != nil {
return errors.Wrapf(err, "render hugo markdown is failed. [%s]", s.entry.Basename)
}
if err := helper.WriteFileWithDirectory(outpath, content, 0644); err != nil {
return errors.Wrapf(err, "failed to write data file. path=%s", outpath)
}
return nil
}
func (s *MigrationImpl) OutputFilePath() string {
return fmt.Sprintf("%s.md", s.entry.Basename)
}