owl-blogs/cmd/owl/import_v1.go

124 lines
2.9 KiB
Go
Raw 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"
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-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)
}
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":
case "reply":
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
},
}