owl-blogs/cmd/owl/import_v1.go

205 lines
5.0 KiB
Go
Raw Permalink Normal View History

2023-07-09 19:27:59 +00:00
package main
import (
2023-07-09 20:12:06 +00:00
"fmt"
2023-07-13 18:39:24 +00:00
"os"
"owl-blogs/config"
2023-07-09 20:12:06 +00:00
"owl-blogs/domain/model"
2023-07-16 19:48:39 +00:00
entrytypes "owl-blogs/entry_types"
2023-07-09 20:12:06 +00:00
"owl-blogs/importer"
"owl-blogs/infra"
2023-07-13 18:39:24 +00:00
"path"
2023-07-09 20:12:06 +00:00
2023-07-09 19:27:59 +00:00
"github.com/spf13/cobra"
2023-07-18 19:03:33 +00:00
"gopkg.in/yaml.v2"
2023-07-09 19:27:59 +00:00
)
2023-07-09 20:12:06 +00:00
var userPath string
2023-07-13 19:55:08 +00:00
var author string
2023-07-09 20:12:06 +00:00
2023-07-09 19:27:59 +00:00
func init() {
rootCmd.AddCommand(importCmd)
2023-07-09 20:12:06 +00:00
importCmd.Flags().StringVarP(&userPath, "path", "p", "", "Path to the user folder")
2023-07-09 19:27:59 +00:00
importCmd.MarkFlagRequired("path")
2023-07-13 19:55:08 +00:00
importCmd.Flags().StringVarP(&author, "author", "a", "", "The author name")
importCmd.MarkFlagRequired("author")
2023-07-09 19:27:59 +00:00
}
var importCmd = &cobra.Command{
Use: "import",
Short: "Import data from v1",
Long: `Import data from v1`,
Run: func(cmd *cobra.Command, args []string) {
2023-07-09 20:12:06 +00:00
db := infra.NewSqliteDB(DbPath)
app := App(db)
posts, err := importer.AllUserPosts(userPath)
if err != nil {
panic(err)
}
2023-07-18 19:03:33 +00:00
// import config
bytes, err := os.ReadFile(path.Join(userPath, "meta/config.yml"))
if err != nil {
panic(err)
}
v1Config := importer.V1UserConfig{}
yaml.Unmarshal(bytes, &v1Config)
mes := []model.MeLinks{}
for _, me := range v1Config.Me {
mes = append(mes, model.MeLinks{
Name: me.Name,
Url: me.Url,
})
}
lists := []model.EntryList{}
for _, list := range v1Config.Lists {
lists = append(lists, model.EntryList{
Id: list.Id,
Title: list.Title,
Include: importer.ConvertTypeList(list.Include, app.Registry),
ListType: list.ListType,
})
}
headerMenu := []model.MenuItem{}
for _, item := range v1Config.HeaderMenu {
headerMenu = append(headerMenu, model.MenuItem{
Title: item.Title,
List: item.List,
Url: item.Url,
Post: item.Post,
})
}
footerMenu := []model.MenuItem{}
for _, item := range v1Config.FooterMenu {
footerMenu = append(footerMenu, model.MenuItem{
Title: item.Title,
List: item.List,
Url: item.Url,
Post: item.Post,
})
}
v2Config := &model.SiteConfig{}
err = app.SiteConfigRepo.Get(config.SITE_CONFIG, v2Config)
if err != nil {
panic(err)
}
2023-07-18 19:03:33 +00:00
v2Config.Title = v1Config.Title
v2Config.SubTitle = v1Config.SubTitle
v2Config.AuthorName = v1Config.AuthorName
v2Config.Me = mes
v2Config.Lists = lists
2023-07-19 18:45:42 +00:00
v2Config.PrimaryListInclude = importer.ConvertTypeList(v1Config.PrimaryListInclude, app.Registry)
2023-07-18 19:03:33 +00:00
v2Config.HeaderMenu = headerMenu
v2Config.FooterMenu = footerMenu
err = app.SiteConfigRepo.Update(config.SITE_CONFIG, v2Config)
2023-07-18 19:03:33 +00:00
if err != nil {
panic(err)
}
2023-07-09 20:12:06 +00:00
for _, post := range posts {
2023-07-13 18:39:24 +00:00
existing, _ := app.EntryService.FindById(post.Id)
if existing != nil {
continue
}
2023-07-09 20:12:06 +00:00
fmt.Println(post.Meta.Type)
2023-07-13 18:39:24 +00:00
// import assets
mediaDir := path.Join(userPath, post.MediaDir())
println(mediaDir)
files := importer.ListDir(mediaDir)
for _, file := range files {
// mock entry to pass to binary service
2023-07-16 19:48:39 +00:00
entry := &entrytypes.Article{}
2023-07-13 18:39:24 +00:00
entry.SetID(post.Id)
fileData, err := os.ReadFile(path.Join(mediaDir, file))
if err != nil {
panic(err)
}
app.BinaryService.CreateEntryFile(file, fileData, entry)
}
2023-07-13 19:55:08 +00:00
var entry model.Entry
2023-07-09 20:12:06 +00:00
switch post.Meta.Type {
case "article":
2023-07-16 19:48:39 +00:00
entry = &entrytypes.Article{}
2023-07-13 19:55:08 +00:00
entry.SetID(post.Id)
entry.SetPublishedAt(&post.Meta.Date)
2023-07-16 19:48:39 +00:00
entry.SetMetaData(&entrytypes.ArticleMetaData{
2023-07-09 20:12:06 +00:00
Title: post.Meta.Title,
Content: post.Content,
})
case "bookmark":
2023-07-18 19:03:33 +00:00
entry = &entrytypes.Bookmark{}
entry.SetID(post.Id)
entry.SetPublishedAt(&post.Meta.Date)
entry.SetMetaData(&entrytypes.BookmarkMetaData{
Url: post.Meta.Bookmark.Url,
Title: post.Meta.Bookmark.Text,
Content: post.Content,
})
2023-07-09 20:12:06 +00:00
case "reply":
2023-07-18 19:03:33 +00:00
entry = &entrytypes.Reply{}
entry.SetID(post.Id)
entry.SetPublishedAt(&post.Meta.Date)
entry.SetMetaData(&entrytypes.ReplyMetaData{
Url: post.Meta.Reply.Url,
Title: post.Meta.Reply.Text,
Content: post.Content,
})
2023-07-09 20:12:06 +00:00
case "photo":
2023-07-16 19:48:39 +00:00
entry = &entrytypes.Image{}
2023-07-13 19:55:08 +00:00
entry.SetID(post.Id)
entry.SetPublishedAt(&post.Meta.Date)
2023-07-16 19:48:39 +00:00
entry.SetMetaData(&entrytypes.ImageMetaData{
2023-07-13 18:39:24 +00:00
Title: post.Meta.Title,
Content: post.Content,
ImageId: post.Meta.PhotoPath,
})
2023-07-09 20:12:06 +00:00
case "note":
2023-07-16 19:48:39 +00:00
entry = &entrytypes.Note{}
2023-07-13 19:55:08 +00:00
entry.SetID(post.Id)
entry.SetPublishedAt(&post.Meta.Date)
2023-07-16 19:48:39 +00:00
entry.SetMetaData(&entrytypes.NoteMetaData{
2023-07-13 18:39:24 +00:00
Content: post.Content,
})
2023-07-09 20:12:06 +00:00
case "recipe":
2023-07-16 19:48:39 +00:00
entry = &entrytypes.Recipe{}
2023-07-13 19:55:08 +00:00
entry.SetID(post.Id)
entry.SetPublishedAt(&post.Meta.Date)
2023-07-16 19:48:39 +00:00
entry.SetMetaData(&entrytypes.RecipeMetaData{
2023-07-13 18:39:24 +00:00
Title: post.Meta.Title,
Yield: post.Meta.Recipe.Yield,
Duration: post.Meta.Recipe.Duration,
Ingredients: post.Meta.Recipe.Ingredients,
Content: post.Content,
})
2023-07-09 20:12:06 +00:00
case "page":
2023-07-16 19:48:39 +00:00
entry = &entrytypes.Page{}
2023-07-13 19:55:08 +00:00
entry.SetID(post.Id)
entry.SetPublishedAt(&post.Meta.Date)
2023-07-16 19:48:39 +00:00
entry.SetMetaData(&entrytypes.PageMetaData{
2023-07-13 18:39:24 +00:00
Title: post.Meta.Title,
Content: post.Content,
})
2023-07-09 20:12:06 +00:00
default:
panic("Unknown type")
}
2023-07-13 19:55:08 +00:00
if entry != nil {
entry.SetAuthorId(author)
app.EntryService.Create(entry)
}
2023-07-09 20:12:06 +00:00
}
2023-07-09 19:27:59 +00:00
},
}