shiori/internal/cmd/export.go
Felipe Martin a60dbf3dc5
Run legacy API and new API at the same time. (#648)
* config: backwards comptabile dir

* remove duplicated frontend

* frontend: move assets to assets folder

* legacy routes handler in gin

* templates and asset in different embed

* new routes

* frontend routes serve old views

* added DTO for account object

* api auth calls legacy handler

* frontend: handle new error messages

* frontend: update urls

* frontend: login using new api

* updated frontend tests

* chore: remove debug route

* create shiori/gopher user if no owner is present

* server as default command

* serve -> server

* refactored database logic, allow database url

* removed unused configuration

* storage docs

* refactor cli to use cfg and deps

* check errors only in server

* log fatal instead of os exit

* dont default data directory to current dir

* fixed sqlite path

* trigger build on prs

* avoid releasing if lint/test fails

* pull request condition

* event -> event_name

* Get correct pull request number

* added workflow to delete dangling tags

* fix: nil error checking

* set gin mode first

* set gin mode before initialization

* fix logger

* allow version bump from custom ref

* Updated matrix link to workspace
2023-07-19 18:25:41 +02:00

97 lines
2.3 KiB
Go

package cmd
import (
"fmt"
"os"
fp "path/filepath"
"strings"
"time"
"github.com/go-shiori/shiori/internal/database"
"github.com/go-shiori/shiori/internal/model"
"github.com/spf13/cobra"
)
func exportCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "export target-file",
Short: "Export bookmarks into HTML file in Netscape Bookmark format",
Args: cobra.ExactArgs(1),
Run: exportHandler,
}
return cmd
}
func exportHandler(cmd *cobra.Command, args []string) {
_, deps := initShiori(cmd.Context(), cmd)
// Fetch bookmarks from database
bookmarks, err := deps.Database.GetBookmarks(cmd.Context(), database.GetBookmarksOptions{})
if err != nil {
cError.Printf("Failed to get bookmarks: %v\n", err)
os.Exit(1)
}
if len(bookmarks) == 0 {
cError.Println("No saved bookmarks yet")
return
}
// Make sure destination directory exist
dstDir := fp.Dir(args[0])
if err := os.MkdirAll(dstDir, model.DataDirPerm); err != nil {
cError.Printf("Error crating destination directory: %s", err)
}
// Create destination file
dstFile, err := os.Create(args[0])
if err != nil {
cError.Printf("Failed to create destination file: %v\n", err)
os.Exit(1)
}
defer dstFile.Close()
// Write exported bookmark to file
fmt.Fprintln(dstFile, ``+
`<!DOCTYPE NETSCAPE-Bookmark-file-1>`+
`<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">`+
`<TITLE>Bookmarks</TITLE>`+
`<H1>Bookmarks</H1>`+
`<DL>`)
for _, book := range bookmarks {
// Create Unix timestamp for bookmark
modifiedTime, err := time.Parse(model.DatabaseDateFormat, book.Modified)
if err != nil {
modifiedTime = time.Now()
}
unixTimestamp := modifiedTime.Unix()
// Create tags for bookmarks
tags := []string{}
for _, tag := range book.Tags {
tags = append(tags, tag.Name)
}
strTags := strings.Join(tags, ",")
// Make sure title is valid
book.Title = validateTitle(book.Title, book.URL)
// Write to file
exportLine := fmt.Sprintf(`<DT><A HREF="%s" ADD_DATE="%d" LAST_MODIFIED="%d" TAGS="%s">%s</A>`,
book.URL, unixTimestamp, unixTimestamp, strTags, book.Title)
fmt.Fprintln(dstFile, exportLine)
}
fmt.Fprintln(dstFile, "</DL>")
// Flush data to storage
err = dstFile.Sync()
if err != nil {
cError.Printf("Failed to export the bookmarks: %v\n", err)
os.Exit(1)
}
fmt.Println("Export finished")
}