From baa6bd0fca342f968c8c038c09dbe63f7631b072 Mon Sep 17 00:00:00 2001 From: Jonathan Hope Date: Wed, 29 May 2024 20:41:25 -0700 Subject: [PATCH] refactor: consolidate the two public packages (#52) --- Taskfile.yml | 4 +- cmd/cli/internal/cmd.go | 73 ++++++----- cmd/cli/internal/formatters.go | 2 +- cmd/cli/test/test_helpers.go | 11 +- cmd/cli/tui/views/booksview/books.go | 101 ++++++++------- cmd/host/internal/messaging/book_dto.go | 2 +- cmd/host/internal/messaging/handlers.go | 43 ++++--- cmd/host/internal/test/add_book_test.go | 6 +- cmd/host/internal/test/add_folder_test.go | 6 +- cmd/host/internal/test/add_tags_test.go | 6 +- cmd/host/internal/test/list_books_test.go | 91 +++++++------- cmd/host/internal/test/list_tags_test.go | 27 ++-- cmd/host/internal/test/remove_book_test.go | 6 +- cmd/host/internal/test/remove_folder_test.go | 6 +- cmd/host/internal/test/remove_tags_test.go | 6 +- cmd/host/internal/test/update_book_test.go | 71 ++++++----- cmd/host/internal/test/update_folder_test.go | 55 ++++----- cmd/seeder/main.go | 26 ++-- internal/config/doc.go | 3 + internal/config/errors.go | 6 + {pkg/api => internal/config}/get_config.go | 17 +-- .../config.go => internal/config/model.go | 2 +- internal/config/update_callback.go | 4 + {pkg/api => internal/config}/update_config.go | 27 +--- internal/db/book_dto.go | 31 +---- internal/db/db.go | 57 ++++----- {pkg/model => internal/db}/direction.go | 2 +- {pkg/model => internal/db}/order.go | 2 +- internal/manifest/chrome_manifest.go | 10 ++ internal/manifest/doc.go | 3 + internal/manifest/firefox_manifest.go | 10 ++ internal/manifest/install.go | 66 ++++++++++ internal/manifest/manifest_type.go | 10 ++ pkg/{api => }/add_book.go | 50 ++++---- pkg/{api => }/add_folder.go | 32 +++-- pkg/{api => }/add_tags.go | 32 ++--- pkg/api/doc.go | 5 - pkg/api/install_manfiest.go | 95 -------------- pkg/{model => }/book.go | 0 pkg/config.go | 5 + pkg/direction.go | 8 ++ pkg/doc.go | 2 + pkg/{model => }/errors.go | 6 +- pkg/{api => }/get_book.go | 17 ++- pkg/get_config.go | 19 +++ pkg/{api => }/get_parent_names.go | 7 +- pkg/install_manfiest.go | 50 ++++++++ pkg/{api => }/list_books.go | 34 +++-- pkg/{api => }/list_tags.go | 18 ++- pkg/model/doc.go | 4 - pkg/model/manifest.go | 19 --- pkg/order.go | 9 ++ pkg/{api => }/remove_book.go | 10 +- pkg/{api => }/remove_folder.go | 10 +- pkg/{api => }/remove_tags.go | 31 ++--- pkg/to_book.go | 40 ++++++ .../book_dto_test.go => pkg/to_book_test.go | 2 +- pkg/{api => }/update_book.go | 50 ++++---- pkg/update_config.go | 26 ++++ pkg/{api => }/update_folder.go | 42 +++---- {internal/validate => pkg}/validators.go | 116 +++++++++--------- {internal/validate => pkg}/validators_test.go | 73 ++++++----- 62 files changed, 835 insertions(+), 769 deletions(-) create mode 100644 internal/config/doc.go create mode 100644 internal/config/errors.go rename {pkg/api => internal/config}/get_config.go (65%) rename pkg/model/config.go => internal/config/model.go (85%) create mode 100644 internal/config/update_callback.go rename {pkg/api => internal/config}/update_config.go (59%) rename {pkg/model => internal/db}/direction.go (90%) rename {pkg/model => internal/db}/order.go (91%) create mode 100644 internal/manifest/chrome_manifest.go create mode 100644 internal/manifest/doc.go create mode 100644 internal/manifest/firefox_manifest.go create mode 100644 internal/manifest/install.go create mode 100644 internal/manifest/manifest_type.go rename pkg/{api => }/add_book.go (57%) rename pkg/{api => }/add_folder.go (55%) rename pkg/{api => }/add_tags.go (59%) delete mode 100644 pkg/api/doc.go delete mode 100644 pkg/api/install_manfiest.go rename pkg/{model => }/book.go (100%) create mode 100644 pkg/config.go create mode 100644 pkg/direction.go create mode 100644 pkg/doc.go rename pkg/{model => }/errors.go (95%) rename pkg/{api => }/get_book.go (63%) create mode 100644 pkg/get_config.go rename pkg/{api => }/get_parent_names.go (88%) create mode 100644 pkg/install_manfiest.go rename pkg/{api => }/list_books.go (78%) rename pkg/{api => }/list_tags.go (80%) delete mode 100644 pkg/model/doc.go delete mode 100644 pkg/model/manifest.go create mode 100644 pkg/order.go rename pkg/{api => }/remove_book.go (86%) rename pkg/{api => }/remove_folder.go (85%) rename pkg/{api => }/remove_tags.go (59%) create mode 100644 pkg/to_book.go rename internal/db/book_dto_test.go => pkg/to_book_test.go (96%) rename pkg/{api => }/update_book.go (64%) create mode 100644 pkg/update_config.go rename pkg/{api => }/update_folder.go (62%) rename {internal/validate => pkg}/validators.go (56%) rename {internal/validate => pkg}/validators_test.go (68%) diff --git a/Taskfile.yml b/Taskfile.yml index 2a68adf..ccdfc81 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -7,11 +7,11 @@ tasks: test-internal: dir: internal cmds: - - go test ./... + - go test -count=1 ./... test-cmd: dir: cmd cmds: - - go test --tags "fts5" ./... + - go test -count=1 --tags "fts5" ./... test: cmds: - task: test-internal diff --git a/cmd/cli/internal/cmd.go b/cmd/cli/internal/cmd.go index 6cab581..cfa140d 100644 --- a/cmd/cli/internal/cmd.go +++ b/cmd/cli/internal/cmd.go @@ -6,8 +6,7 @@ import ( "time" "github.com/jonathanhope/armaria/cmd/cli/tui" - "github.com/jonathanhope/armaria/pkg/api" - "github.com/jonathanhope/armaria/pkg/model" + "github.com/jonathanhope/armaria/pkg" ) // RootCmd is the top level CLI command for Armaria. @@ -107,7 +106,7 @@ type AddBookCmd struct { func (r *AddBookCmd) Run(ctx *Context) error { start := time.Now() - options := armariaapi.DefaultAddBookOptions() + options := armaria.DefaultAddBookOptions() if ctx.DB != nil { options.WithDB(*ctx.DB) } @@ -124,7 +123,7 @@ func (r *AddBookCmd) Run(ctx *Context) error { options.WithTags(r.Tag) } - book, err := armariaapi.AddBook(r.URL, options) + book, err := armaria.AddBook(r.URL, options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) @@ -150,7 +149,7 @@ type AddFolderCmd struct { func (r *AddFolderCmd) Run(ctx *Context) error { start := time.Now() - options := armariaapi.DefaultAddFolderOptions() + options := armaria.DefaultAddFolderOptions() if ctx.DB != nil { options.WithDB(*ctx.DB) } @@ -158,7 +157,7 @@ func (r *AddFolderCmd) Run(ctx *Context) error { options.WithParentID(*r.Folder) } - book, err := armariaapi.AddFolder(r.Name, options) + book, err := armaria.AddFolder(r.Name, options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) @@ -184,12 +183,12 @@ type AddTagsCmd struct { func (r *AddTagsCmd) Run(ctx *Context) error { start := time.Now() - options := armariaapi.DefaultAddTagsOptions() + options := armaria.DefaultAddTagsOptions() if ctx.DB != nil { options.WithDB(*ctx.DB) } - book, err := armariaapi.AddTags(r.ID, r.Tag, options) + book, err := armaria.AddTags(r.ID, r.Tag, options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) @@ -226,7 +225,7 @@ func (r *ListAllCmd) Run(ctx *Context) error { return nil } - options := armariaapi.DefaultListBooksOptions() + options := armaria.DefaultListBooksOptions() options.WithFolders(true) options.WithBooks(true) if ctx.DB != nil { @@ -257,7 +256,7 @@ func (r *ListAllCmd) Run(ctx *Context) error { options.WithFirst(*r.First) } - books, err := armariaapi.ListBooks(options) + books, err := armaria.ListBooks(options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) @@ -294,7 +293,7 @@ func (r *ListBooksCmd) Run(ctx *Context) error { return nil } - options := armariaapi.DefaultListBooksOptions() + options := armaria.DefaultListBooksOptions() options.WithFolders(false) options.WithBooks(true) if ctx.DB != nil { @@ -325,7 +324,7 @@ func (r *ListBooksCmd) Run(ctx *Context) error { options.WithFirst(*r.First) } - books, err := armariaapi.ListBooks(options) + books, err := armaria.ListBooks(options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) @@ -362,7 +361,7 @@ func (r *ListFoldersCmd) Run(ctx *Context) error { return nil } - options := armariaapi.DefaultListBooksOptions() + options := armaria.DefaultListBooksOptions() options.WithFolders(true) options.WithBooks(false) if ctx.DB != nil { @@ -393,7 +392,7 @@ func (r *ListFoldersCmd) Run(ctx *Context) error { options.WithFirst(*r.First) } - books, err := armariaapi.ListBooks(options) + books, err := armaria.ListBooks(options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) @@ -420,7 +419,7 @@ type ListTagsCmd struct { func (r *ListTagsCmd) Run(ctx *Context) error { start := time.Now() - options := armariaapi.DefaultListTagsOptions() + options := armaria.DefaultListTagsOptions() if ctx.DB != nil { options.WithDB(*ctx.DB) } @@ -437,7 +436,7 @@ func (r *ListTagsCmd) Run(ctx *Context) error { options.WithFirst(*r.First) } - tags, err := armariaapi.ListTags(options) + tags, err := armaria.ListTags(options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) @@ -461,12 +460,12 @@ type ListParentNamesCmd struct { func (r *ListParentNamesCmd) Run(ctx *Context) error { start := time.Now() - options := armariaapi.DefaultGetParentNameOptions() + options := armaria.DefaultGetParentNameOptions() if ctx.DB != nil { options.WithDB(*ctx.DB) } - names, err := armariaapi.GetParentNames(r.ID, options) + names, err := armaria.GetParentNames(r.ID, options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) @@ -511,7 +510,7 @@ func (r *UpdateBookCmd) Run(ctx *Context) error { return nil } - options := armariaapi.DefaultUpdateBookOptions() + options := armaria.DefaultUpdateBookOptions() if ctx.DB != nil { options.WithDB(*ctx.DB) } @@ -540,7 +539,7 @@ func (r *UpdateBookCmd) Run(ctx *Context) error { options.WithOrderAfter(*r.After) } - book, err := armariaapi.UpdateBook(r.ID, options) + book, err := armaria.UpdateBook(r.ID, options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) @@ -576,7 +575,7 @@ func (r *UpdateFolderCmd) Run(ctx *Context) error { return nil } - options := armariaapi.DefaultUpdateFolderOptions() + options := armaria.DefaultUpdateFolderOptions() if ctx.DB != nil { options.WithDB(*ctx.DB) } @@ -596,7 +595,7 @@ func (r *UpdateFolderCmd) Run(ctx *Context) error { options.WithOrderAfter(*r.After) } - book, err := armariaapi.UpdateFolder(r.ID, options) + book, err := armaria.UpdateFolder(r.ID, options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) @@ -620,12 +619,12 @@ type RemoveBookCmd struct { func (r *RemoveBookCmd) Run(ctx *Context) error { start := time.Now() - options := armariaapi.DefaultRemoveBookOptions() + options := armaria.DefaultRemoveBookOptions() if ctx.DB != nil { options.WithDB(*ctx.DB) } - err := armariaapi.RemoveBook(r.ID, options) + err := armaria.RemoveBook(r.ID, options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) @@ -648,12 +647,12 @@ type RemoveFolderCmd struct { func (r *RemoveFolderCmd) Run(ctx *Context) error { start := time.Now() - options := armariaapi.DefaultRemoveFolderOptions() + options := armaria.DefaultRemoveFolderOptions() if ctx.DB != nil { options.WithDB(*ctx.DB) } - err := armariaapi.RemoveFolder(r.ID, options) + err := armaria.RemoveFolder(r.ID, options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) @@ -678,12 +677,12 @@ type RemoveTagsCmd struct { func (r *RemoveTagsCmd) Run(ctx *Context) error { start := time.Now() - options := armariaapi.DefaultRemoveTagsOptions() + options := armaria.DefaultRemoveTagsOptions() if ctx.DB != nil { options.WithDB(*ctx.DB) } - book, err := armariaapi.RemoveTags(r.ID, r.Tag, options) + book, err := armaria.RemoveTags(r.ID, r.Tag, options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) @@ -708,12 +707,12 @@ type GetAllCmd struct { func (r *GetAllCmd) Run(ctx *Context) error { start := time.Now() - options := armariaapi.DefaultGetBookOptions() + options := armaria.DefaultGetBookOptions() if ctx.DB != nil { options.WithDB(*ctx.DB) } - book, err := armariaapi.GetBook(r.ID, options) + book, err := armaria.GetBook(r.ID, options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) @@ -736,7 +735,7 @@ type GetDBConfigCmd struct { func (r *GetDBConfigCmd) Run(ctx *Context) error { start := time.Now() - config, err := armariaapi.GetConfig() + config, err := armaria.GetConfig() if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { formatError(ctx.Writer, ctx.Formatter, err) @@ -761,7 +760,7 @@ type SetDBConfigCmd struct { func (r *SetDBConfigCmd) Run(ctx *Context) error { start := time.Now() - err := armariaapi.UpdateConfig(func(config *armaria.Config) { + err := armaria.UpdateConfig(func(config *armaria.Config) { config.DB = r.DB }) @@ -786,7 +785,7 @@ type InstallFirefoxManifestCmd struct { func (r *InstallFirefoxManifestCmd) Run(ctx *Context) error { start := time.Now() - err := armariaapi.InstallManifestFirefox() + err := armaria.InstallManifestFirefox() if err != nil { formatError(ctx.Writer, ctx.Formatter, err) @@ -809,7 +808,7 @@ type InstallChromeManifestCmd struct { func (r *InstallChromeManifestCmd) Run(ctx *Context) error { start := time.Now() - err := armariaapi.InstallManifestChrome() + err := armaria.InstallManifestChrome() if err != nil { formatError(ctx.Writer, ctx.Formatter, err) @@ -832,7 +831,7 @@ type InstallChromiumManifestCmd struct { func (r *InstallChromiumManifestCmd) Run(ctx *Context) error { start := time.Now() - err := armariaapi.InstallManifestChromium() + err := armaria.InstallManifestChromium() if err != nil { formatError(ctx.Writer, ctx.Formatter, err) @@ -869,7 +868,7 @@ type QueryCmd struct { func (r *QueryCmd) Run(ctx *Context) error { start := time.Now() - options := armariaapi.DefaultListBooksOptions() + options := armaria.DefaultListBooksOptions() options.WithFolders(true) options.WithBooks(true) if ctx.DB != nil { @@ -878,7 +877,7 @@ func (r *QueryCmd) Run(ctx *Context) error { options.WithFirst(r.First) options.WithQuery(r.Query) - books, err := armariaapi.ListBooks(options) + books, err := armaria.ListBooks(options) if err != nil { formatError(ctx.Writer, ctx.Formatter, err) ctx.ReturnCode(1) diff --git a/cmd/cli/internal/formatters.go b/cmd/cli/internal/formatters.go index 5cf6e9c..6721018 100644 --- a/cmd/cli/internal/formatters.go +++ b/cmd/cli/internal/formatters.go @@ -10,7 +10,7 @@ import ( "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/lipgloss/table" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/model" + "github.com/jonathanhope/armaria/pkg" "github.com/nathan-fiscaletti/consolesize-go" "github.com/samber/lo" ) diff --git a/cmd/cli/test/test_helpers.go b/cmd/cli/test/test_helpers.go index 592c84e..944b1be 100644 --- a/cmd/cli/test/test_helpers.go +++ b/cmd/cli/test/test_helpers.go @@ -14,8 +14,7 @@ import ( "github.com/google/uuid" "github.com/jonathanhope/armaria/cmd/cli/internal" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/api" - "github.com/jonathanhope/armaria/pkg/model" + "github.com/jonathanhope/armaria/pkg" ) // invokeCli runs the Armaria CLI with the provided args. @@ -114,7 +113,7 @@ func insert(args insertArgs) error { var book armaria.Book if !isFolder { - options := armariaapi.DefaultAddBookOptions() + options := armaria.DefaultAddBookOptions() options.WithDB(args.db) if parentID.Valid { options.WithParentID(parentID.String) @@ -129,18 +128,18 @@ func insert(args insertArgs) error { options.WithTags(tags) } - book, err = armariaapi.AddBook(URL.String, options) + book, err = armaria.AddBook(URL.String, options) if err != nil { return err } } else { - options := armariaapi.DefaultAddFolderOptions() + options := armaria.DefaultAddFolderOptions() options.WithDB(args.db) if parentID.Valid { options.WithParentID(parentID.String) } - book, err = armariaapi.AddFolder(name, options) + book, err = armaria.AddFolder(name, options) if err != nil { return err } diff --git a/cmd/cli/tui/views/booksview/books.go b/cmd/cli/tui/views/booksview/books.go index ededbc3..240be8c 100644 --- a/cmd/cli/tui/views/booksview/books.go +++ b/cmd/cli/tui/views/booksview/books.go @@ -14,8 +14,7 @@ import ( "github.com/jonathanhope/armaria/cmd/cli/tui/controls/scrolltable" "github.com/jonathanhope/armaria/cmd/cli/tui/controls/typeahead" "github.com/jonathanhope/armaria/cmd/cli/tui/msgs" - "github.com/jonathanhope/armaria/pkg/api" - "github.com/jonathanhope/armaria/pkg/model" + "github.com/jonathanhope/armaria/pkg" "github.com/samber/lo" ) @@ -486,8 +485,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { IncludeInput: true, MinFilterChars: 3, UnfilteredQuery: func() ([]typeahead.TypeaheadItem, error) { - options := armariaapi.DefaultListTagsOptions() - tags, err := armariaapi.ListTags(options) + options := armaria.DefaultListTagsOptions() + tags, err := armaria.ListTags(options) if err != nil { return nil, err @@ -500,8 +499,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return items, nil }, FilteredQuery: func(query string) ([]typeahead.TypeaheadItem, error) { - options := armariaapi.DefaultListTagsOptions().WithQuery(query) - tags, err := armariaapi.ListTags(options) + options := armaria.DefaultListTagsOptions().WithQuery(query) + tags, err := armaria.ListTags(options) if err != nil { return nil, err @@ -558,8 +557,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { IncludeInput: false, MinFilterChars: 3, UnfilteredQuery: func() ([]typeahead.TypeaheadItem, error) { - options := armariaapi.DefaultListBooksOptions().WithFolders(true).WithBooks(false) - books, err := armariaapi.ListBooks(options) + options := armaria.DefaultListBooksOptions().WithFolders(true).WithBooks(false) + books, err := armaria.ListBooks(options) if err != nil { return nil, err @@ -572,12 +571,12 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { return items, nil }, FilteredQuery: func(query string) ([]typeahead.TypeaheadItem, error) { - options := armariaapi. + options := armaria. DefaultListBooksOptions(). WithFolders(true). WithBooks(false). WithQuery(query) - books, err := armariaapi.ListBooks(options) + books, err := armaria.ListBooks(options) if err != nil { return nil, err @@ -645,7 +644,7 @@ func (m model) Init() tea.Cmd { // getBooksCmd is a command to get books from the bookmarks database. func (m *model) getBooksCmd(move msgs.Direction) tea.Cmd { return func() tea.Msg { - options := armariaapi. + options := armaria. DefaultListBooksOptions(). WithoutParentID() @@ -658,7 +657,7 @@ func (m *model) getBooksCmd(move msgs.Direction) tea.Cmd { options.WithQuery(m.query) } - books, err := armariaapi.ListBooks(options) + books, err := armaria.ListBooks(options) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -696,8 +695,8 @@ func (m model) openURLCmd() tea.Cmd { // getBreadcrumbsCmd gets breadcrumbs for the currently selected book. func (m model) getBreadcrumbsCmd() tea.Cmd { return func() tea.Msg { - options := armariaapi.DefaultGetParentNameOptions() - parents, err := armariaapi.GetParentNames(m.table.Selection().ID, options) + options := armaria.DefaultGetParentNameOptions() + parents, err := armaria.GetParentNames(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -709,7 +708,7 @@ func (m model) getBreadcrumbsCmd() tea.Cmd { // getParentCmd is a command to go one level up in the folder structure. func (m model) getParentCmd() tea.Cmd { return func() tea.Msg { - book, err := armariaapi.GetBook(m.folder, armariaapi.DefaultGetBookOptions()) + book, err := armaria.GetBook(m.folder, armaria.DefaultGetBookOptions()) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -726,12 +725,12 @@ func (m model) getParentCmd() tea.Cmd { func (m model) deleteBookCmd() tea.Cmd { return func() tea.Msg { if m.table.Selection().IsFolder { - err := armariaapi.RemoveFolder(m.table.Selection().ID, armariaapi.DefaultRemoveFolderOptions()) + err := armaria.RemoveFolder(m.table.Selection().ID, armaria.DefaultRemoveFolderOptions()) if err != nil { return msgs.ErrorMsg{Err: err} } } else { - err := armariaapi.RemoveBook(m.table.Selection().ID, armariaapi.DefaultRemoveBookOptions()) + err := armaria.RemoveBook(m.table.Selection().ID, armaria.DefaultRemoveBookOptions()) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -744,11 +743,11 @@ func (m model) deleteBookCmd() tea.Cmd { // updateURLCmd updates an URL for a bookmark. func (m model) updateURLCmd(URL string) tea.Cmd { return func() tea.Msg { - options := armariaapi. + options := armaria. DefaultUpdateBookOptions(). WithURL(URL) - _, err := armariaapi.UpdateBook(m.table.Selection().ID, options) + _, err := armaria.UpdateBook(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -761,20 +760,20 @@ func (m model) updateURLCmd(URL string) tea.Cmd { func (m model) updateNameCmd(name string) tea.Cmd { return func() tea.Msg { if m.table.Selection().IsFolder { - options := armariaapi. + options := armaria. DefaultUpdateFolderOptions(). WithName(name) - _, err := armariaapi.UpdateFolder(m.table.Selection().ID, options) + _, err := armaria.UpdateFolder(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } } else { - options := armariaapi. + options := armaria. DefaultUpdateBookOptions(). WithName(name) - _, err := armariaapi.UpdateBook(m.table.Selection().ID, options) + _, err := armaria.UpdateBook(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -787,12 +786,12 @@ func (m model) updateNameCmd(name string) tea.Cmd { // addFolderCmd adds a folder to the bookmarks database. func (m model) addFolderCmd(name string) tea.Cmd { return func() tea.Msg { - options := armariaapi.DefaultAddFolderOptions() + options := armaria.DefaultAddFolderOptions() if m.folder != "" { options.WithParentID(m.folder) } - _, err := armariaapi.AddFolder(name, options) + _, err := armaria.AddFolder(name, options) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -804,12 +803,12 @@ func (m model) addFolderCmd(name string) tea.Cmd { // addBookmarkCmd adds a bookmark to the bookmarks database. func (m model) addBookmarkCmd(url string) tea.Cmd { return func() tea.Msg { - options := armariaapi.DefaultAddBookOptions() + options := armaria.DefaultAddBookOptions() if m.folder != "" { options.WithParentID(m.folder) } - _, err := armariaapi.AddBook(url, options) + _, err := armaria.AddBook(url, options) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -822,20 +821,20 @@ func (m model) addBookmarkCmd(url string) tea.Cmd { func (m model) moveToEndCmd(previous string, move msgs.Direction) tea.Cmd { return func() tea.Msg { if m.table.Selection().IsFolder { - options := armariaapi. + options := armaria. DefaultUpdateFolderOptions(). WithOrderAfter(previous) - _, err := armariaapi.UpdateFolder(m.table.Selection().ID, options) + _, err := armaria.UpdateFolder(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } } else { - options := armariaapi. + options := armaria. DefaultUpdateBookOptions(). WithOrderAfter(previous) - _, err := armariaapi.UpdateBook(m.table.Selection().ID, options) + _, err := armaria.UpdateBook(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -849,20 +848,20 @@ func (m model) moveToEndCmd(previous string, move msgs.Direction) tea.Cmd { func (m model) moveToStartCmd(next string, move msgs.Direction) tea.Cmd { return func() tea.Msg { if m.table.Selection().IsFolder { - options := armariaapi. + options := armaria. DefaultUpdateFolderOptions(). WithOrderBefore(next) - _, err := armariaapi.UpdateFolder(m.table.Selection().ID, options) + _, err := armaria.UpdateFolder(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } } else { - options := armariaapi. + options := armaria. DefaultUpdateBookOptions(). WithOrderBefore(next) - _, err := armariaapi.UpdateBook(m.table.Selection().ID, options) + _, err := armaria.UpdateBook(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -876,22 +875,22 @@ func (m model) moveToStartCmd(next string, move msgs.Direction) tea.Cmd { func (m model) moveBetweenCmd(previous string, next string, move msgs.Direction) tea.Cmd { return func() tea.Msg { if m.table.Selection().IsFolder { - options := armariaapi. + options := armaria. DefaultUpdateFolderOptions(). WithOrderAfter(previous). WithOrderBefore(next) - _, err := armariaapi.UpdateFolder(m.table.Selection().ID, options) + _, err := armaria.UpdateFolder(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } } else { - options := armariaapi. + options := armaria. DefaultUpdateBookOptions(). WithOrderAfter(previous). WithOrderBefore(next) - _, err := armariaapi.UpdateBook(m.table.Selection().ID, options) + _, err := armaria.UpdateBook(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -905,14 +904,14 @@ func (m model) moveBetweenCmd(previous string, next string, move msgs.Direction) func (m model) removeParentCmd() tea.Cmd { return func() tea.Msg { if m.table.Selection().IsFolder { - options := armariaapi.DefaultUpdateFolderOptions().WithoutParentID() - _, err := armariaapi.UpdateFolder(m.table.Selection().ID, options) + options := armaria.DefaultUpdateFolderOptions().WithoutParentID() + _, err := armaria.UpdateFolder(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } } else { - options := armariaapi.DefaultUpdateBookOptions().WithoutParentID() - _, err := armariaapi.UpdateBook(m.table.Selection().ID, options) + options := armaria.DefaultUpdateBookOptions().WithoutParentID() + _, err := armaria.UpdateBook(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -925,8 +924,8 @@ func (m model) removeParentCmd() tea.Cmd { // addTagCmd adds a tag to a bookmark. func (m model) addTagCmd(tag string) tea.Cmd { return func() tea.Msg { - options := armariaapi.DefaultAddTagsOptions() - _, err := armariaapi.AddTags(m.table.Selection().ID, []string{tag}, options) + options := armaria.DefaultAddTagsOptions() + _, err := armaria.AddTags(m.table.Selection().ID, []string{tag}, options) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -937,8 +936,8 @@ func (m model) addTagCmd(tag string) tea.Cmd { // removeTagCmd removes a tag from a bookmark. func (m model) removeTagCmd(tag string) tea.Cmd { return func() tea.Msg { - options := armariaapi.DefaultRemoveTagsOptions() - _, err := armariaapi.RemoveTags(m.table.Selection().ID, []string{tag}, options) + options := armaria.DefaultRemoveTagsOptions() + _, err := armaria.RemoveTags(m.table.Selection().ID, []string{tag}, options) if err != nil { return msgs.ErrorMsg{Err: err} } @@ -950,14 +949,14 @@ func (m model) removeTagCmd(tag string) tea.Cmd { func (m model) changeParentCmd(parentID string) tea.Cmd { return func() tea.Msg { if m.table.Selection().IsFolder { - options := armariaapi.DefaultUpdateFolderOptions().WithParentID(parentID) - _, err := armariaapi.UpdateFolder(m.table.Selection().ID, options) + options := armaria.DefaultUpdateFolderOptions().WithParentID(parentID) + _, err := armaria.UpdateFolder(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } } else { - options := armariaapi.DefaultUpdateBookOptions().WithParentID(parentID) - _, err := armariaapi.UpdateBook(m.table.Selection().ID, options) + options := armaria.DefaultUpdateBookOptions().WithParentID(parentID) + _, err := armaria.UpdateBook(m.table.Selection().ID, options) if err != nil { return msgs.ErrorMsg{Err: err} } diff --git a/cmd/host/internal/messaging/book_dto.go b/cmd/host/internal/messaging/book_dto.go index 6a86866..a0ed679 100644 --- a/cmd/host/internal/messaging/book_dto.go +++ b/cmd/host/internal/messaging/book_dto.go @@ -2,7 +2,7 @@ package messaging import ( "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/model" + "github.com/jonathanhope/armaria/pkg" ) // BookDTO is a bookmark or folder that can be marshalled into JSON. diff --git a/cmd/host/internal/messaging/handlers.go b/cmd/host/internal/messaging/handlers.go index a67d8ab..31ac9cb 100644 --- a/cmd/host/internal/messaging/handlers.go +++ b/cmd/host/internal/messaging/handlers.go @@ -1,8 +1,7 @@ package messaging import ( - "github.com/jonathanhope/armaria/pkg/api" - "github.com/jonathanhope/armaria/pkg/model" + "github.com/jonathanhope/armaria/pkg" "github.com/samber/lo" ) @@ -16,7 +15,7 @@ func addBookHandler(in NativeMessage) (NativeMessage, error) { return NativeMessage{}, err } - options := armariaapi.DefaultAddBookOptions() + options := armaria.DefaultAddBookOptions() if payload.DB.Valid { options.WithDB(payload.DB.String) } @@ -33,7 +32,7 @@ func addBookHandler(in NativeMessage) (NativeMessage, error) { options.WithTags(payload.Tags) } - book, err := armariaapi.AddBook(payload.URL, options) + book, err := armaria.AddBook(payload.URL, options) if err != nil { return NativeMessage{}, err } @@ -55,7 +54,7 @@ func addFolderHandler(in NativeMessage) (NativeMessage, error) { return NativeMessage{}, err } - options := armariaapi.DefaultAddFolderOptions() + options := armaria.DefaultAddFolderOptions() if payload.DB.Valid { options.WithDB(payload.DB.String) } @@ -63,7 +62,7 @@ func addFolderHandler(in NativeMessage) (NativeMessage, error) { options.WithParentID(payload.ParentID.String) } - book, err := armariaapi.AddFolder(payload.Name, options) + book, err := armaria.AddFolder(payload.Name, options) if err != nil { return NativeMessage{}, err } @@ -85,11 +84,11 @@ func addTagsHandler(in NativeMessage) (NativeMessage, error) { return NativeMessage{}, err } - options := armariaapi.DefaultAddTagsOptions() + options := armaria.DefaultAddTagsOptions() if payload.DB.Valid { options.WithDB(payload.DB.String) } - book, err := armariaapi.AddTags(payload.ID, payload.Tags, options) + book, err := armaria.AddTags(payload.ID, payload.Tags, options) if err != nil { return NativeMessage{}, err } @@ -111,7 +110,7 @@ func listBooksHandler(in NativeMessage) (NativeMessage, error) { return NativeMessage{}, err } - options := armariaapi.DefaultListBooksOptions() + options := armaria.DefaultListBooksOptions() if payload.DB.Valid { options.WithDB(payload.DB.String) } @@ -142,7 +141,7 @@ func listBooksHandler(in NativeMessage) (NativeMessage, error) { options.WithFirst(payload.First.Int64) } - books, err := armariaapi.ListBooks(options) + books, err := armaria.ListBooks(options) if err != nil { return NativeMessage{}, err } @@ -166,7 +165,7 @@ func listTagsHandler(in NativeMessage) (NativeMessage, error) { return NativeMessage{}, err } - options := armariaapi.DefaultListTagsOptions() + options := armaria.DefaultListTagsOptions() if payload.DB.Valid { options.WithDB(payload.DB.String) } @@ -183,7 +182,7 @@ func listTagsHandler(in NativeMessage) (NativeMessage, error) { options.WithFirst(payload.First.Int64) } - tags, err := armariaapi.ListTags(options) + tags, err := armaria.ListTags(options) if err != nil { return NativeMessage{}, err } @@ -205,12 +204,12 @@ func removeBookHandler(in NativeMessage) (NativeMessage, error) { return NativeMessage{}, err } - options := armariaapi.DefaultRemoveBookOptions() + options := armaria.DefaultRemoveBookOptions() if payload.DB.Valid { options.WithDB(payload.DB.String) } - err = armariaapi.RemoveBook(payload.ID, options) + err = armaria.RemoveBook(payload.ID, options) if err != nil { return NativeMessage{}, err } @@ -230,12 +229,12 @@ func removeFolderHandler(in NativeMessage) (NativeMessage, error) { return NativeMessage{}, err } - options := armariaapi.DefaultRemoveFolderOptions() + options := armaria.DefaultRemoveFolderOptions() if payload.DB.Valid { options.WithDB(payload.DB.String) } - err = armariaapi.RemoveFolder(payload.ID, options) + err = armaria.RemoveFolder(payload.ID, options) if err != nil { return NativeMessage{}, err } @@ -255,12 +254,12 @@ func removeTagsHandler(in NativeMessage) (NativeMessage, error) { return NativeMessage{}, err } - options := armariaapi.DefaultRemoveTagsOptions() + options := armaria.DefaultRemoveTagsOptions() if payload.DB.Valid { options.WithDB(payload.DB.String) } - book, err := armariaapi.RemoveTags(payload.ID, payload.Tags, options) + book, err := armaria.RemoveTags(payload.ID, payload.Tags, options) if err != nil { return NativeMessage{}, err } @@ -282,7 +281,7 @@ func updateBookHandler(in NativeMessage) (NativeMessage, error) { return NativeMessage{}, err } - options := armariaapi.DefaultUpdateBookOptions() + options := armaria.DefaultUpdateBookOptions() if payload.DB.Valid { options.WithDB(payload.DB.String) } @@ -311,7 +310,7 @@ func updateBookHandler(in NativeMessage) (NativeMessage, error) { options.WithOrderAfter(payload.PreviousBook.String) } - book, err := armariaapi.UpdateBook(payload.ID, options) + book, err := armaria.UpdateBook(payload.ID, options) if err != nil { return NativeMessage{}, err } @@ -333,7 +332,7 @@ func updateFolderHandler(in NativeMessage) (NativeMessage, error) { return NativeMessage{}, err } - options := armariaapi.DefaultUpdateFolderOptions() + options := armaria.DefaultUpdateFolderOptions() if payload.DB.Valid { options.WithDB(payload.DB.String) } @@ -353,7 +352,7 @@ func updateFolderHandler(in NativeMessage) (NativeMessage, error) { options.WithOrderAfter(payload.PreviousBook.String) } - book, err := armariaapi.UpdateFolder(payload.ID, options) + book, err := armaria.UpdateFolder(payload.ID, options) if err != nil { return NativeMessage{}, err } diff --git a/cmd/host/internal/test/add_book_test.go b/cmd/host/internal/test/add_book_test.go index 74b5254..07ba84b 100644 --- a/cmd/host/internal/test/add_book_test.go +++ b/cmd/host/internal/test/add_book_test.go @@ -9,7 +9,7 @@ import ( "github.com/google/uuid" "github.com/jonathanhope/armaria/cmd/host/internal/messaging" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/api" + "github.com/jonathanhope/armaria/pkg" ) func TestAddBook(t *testing.T) { @@ -51,9 +51,9 @@ func TestAddBookToFolder(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - options := armariaapi.DefaultAddFolderOptions() + options := armaria.DefaultAddFolderOptions() options.WithDB(db) - folder, err := armariaapi.AddFolder("Blogs", options) + folder, err := armaria.AddFolder("Blogs", options) if err != nil { t.Errorf("unexpected error: %s", err) } diff --git a/cmd/host/internal/test/add_folder_test.go b/cmd/host/internal/test/add_folder_test.go index 1db72ae..b4687ae 100644 --- a/cmd/host/internal/test/add_folder_test.go +++ b/cmd/host/internal/test/add_folder_test.go @@ -9,7 +9,7 @@ import ( "github.com/google/uuid" "github.com/jonathanhope/armaria/cmd/host/internal/messaging" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/api" + "github.com/jonathanhope/armaria/pkg" ) func TestAddFolder(t *testing.T) { @@ -51,9 +51,9 @@ func TestAddFolderToFolder(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - options := armariaapi.DefaultAddFolderOptions() + options := armaria.DefaultAddFolderOptions() options.WithDB(db) - folder, err := armariaapi.AddFolder("Programming", options) + folder, err := armaria.AddFolder("Programming", options) if err != nil { t.Errorf("unexpected error: %s", err) } diff --git a/cmd/host/internal/test/add_tags_test.go b/cmd/host/internal/test/add_tags_test.go index 34b1c8a..552302d 100644 --- a/cmd/host/internal/test/add_tags_test.go +++ b/cmd/host/internal/test/add_tags_test.go @@ -9,16 +9,16 @@ import ( "github.com/google/uuid" "github.com/jonathanhope/armaria/cmd/host/internal/messaging" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/api" + "github.com/jonathanhope/armaria/pkg" ) func TestAddTags(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - options := armariaapi.DefaultAddBookOptions() + options := armaria.DefaultAddBookOptions() options.WithDB(db) - book, err := armariaapi.AddBook("https://jho.pe", options) + book, err := armaria.AddBook("https://jho.pe", options) if err != nil { t.Errorf("unexpected error: %s", err) } diff --git a/cmd/host/internal/test/list_books_test.go b/cmd/host/internal/test/list_books_test.go index d6bfab4..66439c9 100644 --- a/cmd/host/internal/test/list_books_test.go +++ b/cmd/host/internal/test/list_books_test.go @@ -9,24 +9,23 @@ import ( "github.com/google/uuid" "github.com/jonathanhope/armaria/cmd/host/internal/messaging" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/api" - "github.com/jonathanhope/armaria/pkg/model" + "github.com/jonathanhope/armaria/pkg" ) func TestListBooksFolders(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - folderOptions := armariaapi.DefaultAddFolderOptions() + folderOptions := armaria.DefaultAddFolderOptions() folderOptions.WithDB(db) - folder, err := armariaapi.AddFolder("Blogs", folderOptions) + folder, err := armaria.AddFolder("Blogs", folderOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) - book, err := armariaapi.AddBook("https://jho.pe", bookOptions) + book, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -72,16 +71,16 @@ func TestListBooks(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - folderOptions := armariaapi.DefaultAddFolderOptions() + folderOptions := armaria.DefaultAddFolderOptions() folderOptions.WithDB(db) - _, err := armariaapi.AddFolder("Blogs", folderOptions) + _, err := armaria.AddFolder("Blogs", folderOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) - book, err := armariaapi.AddBook("https://jho.pe", bookOptions) + book, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -121,16 +120,16 @@ func TestListFolder(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - folderOptions := armariaapi.DefaultAddFolderOptions() + folderOptions := armaria.DefaultAddFolderOptions() folderOptions.WithDB(db) - folder, err := armariaapi.AddFolder("Blogs", folderOptions) + folder, err := armaria.AddFolder("Blogs", folderOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) - _, err = armariaapi.AddBook("https://jho.pe", bookOptions) + _, err = armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -170,16 +169,16 @@ func TestListBooksFoldersWithLimit(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - folderOptions := armariaapi.DefaultAddFolderOptions() + folderOptions := armaria.DefaultAddFolderOptions() folderOptions.WithDB(db) - folder, err := armariaapi.AddFolder("Blogs", folderOptions) + folder, err := armaria.AddFolder("Blogs", folderOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) - _, err = armariaapi.AddBook("https://jho.pe", bookOptions) + _, err = armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -220,16 +219,16 @@ func TestListBooksFoldersNamAsc(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - folderOptions := armariaapi.DefaultAddFolderOptions() + folderOptions := armaria.DefaultAddFolderOptions() folderOptions.WithDB(db) - folder, err := armariaapi.AddFolder("Blogs", folderOptions) + folder, err := armaria.AddFolder("Blogs", folderOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) - book, err := armariaapi.AddBook("https://jho.pe", bookOptions) + book, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -275,16 +274,16 @@ func TestListBooksFoldersNamDesc(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - folderOptions := armariaapi.DefaultAddFolderOptions() + folderOptions := armaria.DefaultAddFolderOptions() folderOptions.WithDB(db) - folder, err := armariaapi.AddFolder("Blogs", folderOptions) + folder, err := armaria.AddFolder("Blogs", folderOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) - book, err := armariaapi.AddBook("https://jho.pe", bookOptions) + book, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -330,16 +329,16 @@ func TestListBooksFoldersWithAfter(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - folderOptions := armariaapi.DefaultAddFolderOptions() + folderOptions := armaria.DefaultAddFolderOptions() folderOptions.WithDB(db) - folder, err := armariaapi.AddFolder("Blogs", folderOptions) + folder, err := armaria.AddFolder("Blogs", folderOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) - book, err := armariaapi.AddBook("https://jho.pe", bookOptions) + book, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -380,17 +379,17 @@ func TestListBooksFoldersWithParentID(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - folderOptions := armariaapi.DefaultAddFolderOptions() + folderOptions := armaria.DefaultAddFolderOptions() folderOptions.WithDB(db) - folder, err := armariaapi.AddFolder("Blogs", folderOptions) + folder, err := armaria.AddFolder("Blogs", folderOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) bookOptions.WithParentID(folder.ID) - book, err := armariaapi.AddBook("https://jho.pe", bookOptions) + book, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -433,17 +432,17 @@ func TestListBooksFoldersWithoutParentID(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - folderOptions := armariaapi.DefaultAddFolderOptions() + folderOptions := armaria.DefaultAddFolderOptions() folderOptions.WithDB(db) - folder, err := armariaapi.AddFolder("Blogs", folderOptions) + folder, err := armaria.AddFolder("Blogs", folderOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) bookOptions.WithParentID(folder.ID) - _, err = armariaapi.AddBook("https://jho.pe", bookOptions) + _, err = armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -484,16 +483,16 @@ func TestListBooksFoldersWithQuery(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - folderOptions := armariaapi.DefaultAddFolderOptions() + folderOptions := armaria.DefaultAddFolderOptions() folderOptions.WithDB(db) - _, err := armariaapi.AddFolder("Blogs", folderOptions) + _, err := armaria.AddFolder("Blogs", folderOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) - book, err := armariaapi.AddBook("https://jho.pe", bookOptions) + book, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -534,17 +533,17 @@ func TestListBooksFoldersWithTags(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - folderOptions := armariaapi.DefaultAddFolderOptions() + folderOptions := armaria.DefaultAddFolderOptions() folderOptions.WithDB(db) - _, err := armariaapi.AddFolder("Blogs", folderOptions) + _, err := armaria.AddFolder("Blogs", folderOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) bookOptions.WithTags([]string{"blog", "programming"}) - book, err := armariaapi.AddBook("https://jho.pe", bookOptions) + book, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } diff --git a/cmd/host/internal/test/list_tags_test.go b/cmd/host/internal/test/list_tags_test.go index 2a8ac2a..da9577c 100644 --- a/cmd/host/internal/test/list_tags_test.go +++ b/cmd/host/internal/test/list_tags_test.go @@ -9,18 +9,17 @@ import ( "github.com/google/uuid" "github.com/jonathanhope/armaria/cmd/host/internal/messaging" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/api" - "github.com/jonathanhope/armaria/pkg/model" + "github.com/jonathanhope/armaria/pkg" ) func TestListTags(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) bookOptions.WithTags([]string{"blog", "programming"}) - _, err := armariaapi.AddBook("https://jho.pe", bookOptions) + _, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -49,10 +48,10 @@ func TestListTagsAsc(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) bookOptions.WithTags([]string{"blog", "programming"}) - _, err := armariaapi.AddBook("https://jho.pe", bookOptions) + _, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -82,10 +81,10 @@ func TestListTagsDesc(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) bookOptions.WithTags([]string{"blog", "programming"}) - _, err := armariaapi.AddBook("https://jho.pe", bookOptions) + _, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -115,10 +114,10 @@ func TestListTagsWithLimit(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) bookOptions.WithTags([]string{"blog", "programming"}) - _, err := armariaapi.AddBook("https://jho.pe", bookOptions) + _, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -148,10 +147,10 @@ func TestListTagsWithAfter(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) bookOptions.WithTags([]string{"blog", "programming"}) - _, err := armariaapi.AddBook("https://jho.pe", bookOptions) + _, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -181,10 +180,10 @@ func TestListTagsWithQuery(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - bookOptions := armariaapi.DefaultAddBookOptions() + bookOptions := armaria.DefaultAddBookOptions() bookOptions.WithDB(db) bookOptions.WithTags([]string{"blog", "programming"}) - _, err := armariaapi.AddBook("https://jho.pe", bookOptions) + _, err := armaria.AddBook("https://jho.pe", bookOptions) if err != nil { t.Errorf("unexpected error: %s", err) } diff --git a/cmd/host/internal/test/remove_book_test.go b/cmd/host/internal/test/remove_book_test.go index 9ce0589..9bab6ab 100644 --- a/cmd/host/internal/test/remove_book_test.go +++ b/cmd/host/internal/test/remove_book_test.go @@ -9,16 +9,16 @@ import ( "github.com/google/uuid" "github.com/jonathanhope/armaria/cmd/host/internal/messaging" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/api" + "github.com/jonathanhope/armaria/pkg" ) func TestRemoveBook(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - options := armariaapi.DefaultAddBookOptions() + options := armaria.DefaultAddBookOptions() options.WithDB(db) - book, err := armariaapi.AddBook("https://jho.pe", options) + book, err := armaria.AddBook("https://jho.pe", options) if err != nil { t.Errorf("unexpected error: %s", err) } diff --git a/cmd/host/internal/test/remove_folder_test.go b/cmd/host/internal/test/remove_folder_test.go index 2208743..c3fad2c 100644 --- a/cmd/host/internal/test/remove_folder_test.go +++ b/cmd/host/internal/test/remove_folder_test.go @@ -9,16 +9,16 @@ import ( "github.com/google/uuid" "github.com/jonathanhope/armaria/cmd/host/internal/messaging" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/api" + "github.com/jonathanhope/armaria/pkg" ) func TestRemoveFolder(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - options := armariaapi.DefaultAddFolderOptions() + options := armaria.DefaultAddFolderOptions() options.WithDB(db) - folder, err := armariaapi.AddFolder("Blogs", options) + folder, err := armaria.AddFolder("Blogs", options) if err != nil { t.Errorf("unexpected error: %s", err) } diff --git a/cmd/host/internal/test/remove_tags_test.go b/cmd/host/internal/test/remove_tags_test.go index 905c20d..535b851 100644 --- a/cmd/host/internal/test/remove_tags_test.go +++ b/cmd/host/internal/test/remove_tags_test.go @@ -9,17 +9,17 @@ import ( "github.com/google/uuid" "github.com/jonathanhope/armaria/cmd/host/internal/messaging" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/api" + "github.com/jonathanhope/armaria/pkg" ) func TestRemoveTags(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - options := armariaapi.DefaultAddBookOptions() + options := armaria.DefaultAddBookOptions() options.WithDB(db) options.WithTags([]string{"blog", "programming"}) - book, err := armariaapi.AddBook("https://jho.pe", options) + book, err := armaria.AddBook("https://jho.pe", options) if err != nil { t.Errorf("unexpected error: %s", err) } diff --git a/cmd/host/internal/test/update_book_test.go b/cmd/host/internal/test/update_book_test.go index 583bbf0..8a09f39 100644 --- a/cmd/host/internal/test/update_book_test.go +++ b/cmd/host/internal/test/update_book_test.go @@ -9,8 +9,7 @@ import ( "github.com/google/uuid" "github.com/jonathanhope/armaria/cmd/host/internal/messaging" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/api" - "github.com/jonathanhope/armaria/pkg/model" + "github.com/jonathanhope/armaria/pkg" "github.com/samber/lo" ) @@ -18,9 +17,9 @@ func TestUpdateBookURL(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - options := armariaapi.DefaultAddBookOptions() + options := armaria.DefaultAddBookOptions() options.WithDB(db) - book, err := armariaapi.AddBook("https://jho.pe", options) + book, err := armaria.AddBook("https://jho.pe", options) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -56,9 +55,9 @@ func TestUpdateBookName(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - options := armariaapi.DefaultAddBookOptions() + options := armaria.DefaultAddBookOptions() options.WithDB(db) - book, err := armariaapi.AddBook("https://jho.pe", options) + book, err := armaria.AddBook("https://jho.pe", options) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -94,9 +93,9 @@ func TestUpdateBookDescription(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - options := armariaapi.DefaultAddBookOptions() + options := armaria.DefaultAddBookOptions() options.WithDB(db) - book, err := armariaapi.AddBook("https://jho.pe", options) + book, err := armaria.AddBook("https://jho.pe", options) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -133,10 +132,10 @@ func TestUpdateBookRemoveDescription(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - options := armariaapi.DefaultAddBookOptions() + options := armaria.DefaultAddBookOptions() options.WithDB(db) options.WithDescription("The blog of Jonathan Hope.") - book, err := armariaapi.AddBook("https://jho.pe", options) + book, err := armaria.AddBook("https://jho.pe", options) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -172,16 +171,16 @@ func TestUpdateBookParentID(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - folderOptions := armariaapi.DefaultAddFolderOptions() + folderOptions := armaria.DefaultAddFolderOptions() folderOptions.WithDB(db) - folder, err := armariaapi.AddFolder("Blogs", folderOptions) + folder, err := armaria.AddFolder("Blogs", folderOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - options := armariaapi.DefaultAddBookOptions() + options := armaria.DefaultAddBookOptions() options.WithDB(db) - book, err := armariaapi.AddBook("https://jho.pe", options) + book, err := armaria.AddBook("https://jho.pe", options) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -219,17 +218,17 @@ func TestUpdateBookRemoveParentID(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - folderOptions := armariaapi.DefaultAddFolderOptions() + folderOptions := armaria.DefaultAddFolderOptions() folderOptions.WithDB(db) - folder, err := armariaapi.AddFolder("Blogs", folderOptions) + folder, err := armaria.AddFolder("Blogs", folderOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - options := armariaapi.DefaultAddBookOptions() + options := armaria.DefaultAddBookOptions() options.WithDB(db) options.WithParentID(folder.ID) - book, err := armariaapi.AddBook("https://jho.pe", options) + book, err := armaria.AddBook("https://jho.pe", options) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -265,19 +264,19 @@ func TestUpdateBookOrderStart(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - addOptions := armariaapi.DefaultAddBookOptions() + addOptions := armaria.DefaultAddBookOptions() addOptions.WithDB(db) - book1, err := armariaapi.AddBook("https://one.com", addOptions) + book1, err := armaria.AddBook("https://one.com", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - book2, err := armariaapi.AddBook("https://two.com", addOptions) + book2, err := armaria.AddBook("https://two.com", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - book3, err := armariaapi.AddBook("https://three.com", addOptions) + book3, err := armaria.AddBook("https://three.com", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -291,10 +290,10 @@ func TestUpdateBookOrderStart(t *testing.T) { t.Errorf("unexpected error: %s", err) } - listOptions := armariaapi.DefaultListBooksOptions() + listOptions := armaria.DefaultListBooksOptions() listOptions.WithBooks(true) listOptions.WithDB(db) - books, err := armariaapi.ListBooks(listOptions) + books, err := armaria.ListBooks(listOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -319,19 +318,19 @@ func TestUpdateBookOrderEnd(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - addOptions := armariaapi.DefaultAddBookOptions() + addOptions := armaria.DefaultAddBookOptions() addOptions.WithDB(db) - book1, err := armariaapi.AddBook("https://one.com", addOptions) + book1, err := armaria.AddBook("https://one.com", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - book2, err := armariaapi.AddBook("https://two.com", addOptions) + book2, err := armaria.AddBook("https://two.com", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - book3, err := armariaapi.AddBook("https://three.com", addOptions) + book3, err := armaria.AddBook("https://three.com", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -345,10 +344,10 @@ func TestUpdateBookOrderEnd(t *testing.T) { t.Errorf("unexpected error: %s", err) } - listOptions := armariaapi.DefaultListBooksOptions() + listOptions := armaria.DefaultListBooksOptions() listOptions.WithBooks(true) listOptions.WithDB(db) - books, err := armariaapi.ListBooks(listOptions) + books, err := armaria.ListBooks(listOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -373,19 +372,19 @@ func TestUpdateBookOrderBetween(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - addOptions := armariaapi.DefaultAddBookOptions() + addOptions := armaria.DefaultAddBookOptions() addOptions.WithDB(db) - book1, err := armariaapi.AddBook("https://one.com", addOptions) + book1, err := armaria.AddBook("https://one.com", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - book2, err := armariaapi.AddBook("https://two.com", addOptions) + book2, err := armaria.AddBook("https://two.com", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - book3, err := armariaapi.AddBook("https://three.com", addOptions) + book3, err := armaria.AddBook("https://three.com", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -400,10 +399,10 @@ func TestUpdateBookOrderBetween(t *testing.T) { t.Errorf("unexpected error: %s", err) } - listOptions := armariaapi.DefaultListBooksOptions() + listOptions := armaria.DefaultListBooksOptions() listOptions.WithBooks(true) listOptions.WithDB(db) - books, err := armariaapi.ListBooks(listOptions) + books, err := armaria.ListBooks(listOptions) if err != nil { t.Errorf("unexpected error: %s", err) } diff --git a/cmd/host/internal/test/update_folder_test.go b/cmd/host/internal/test/update_folder_test.go index 5d55949..c418bb9 100644 --- a/cmd/host/internal/test/update_folder_test.go +++ b/cmd/host/internal/test/update_folder_test.go @@ -9,8 +9,7 @@ import ( "github.com/google/uuid" "github.com/jonathanhope/armaria/cmd/host/internal/messaging" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/api" - "github.com/jonathanhope/armaria/pkg/model" + "github.com/jonathanhope/armaria/pkg" "github.com/samber/lo" ) @@ -18,9 +17,9 @@ func TestUpdateFolder(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - options := armariaapi.DefaultAddFolderOptions() + options := armaria.DefaultAddFolderOptions() options.WithDB(db) - folder, err := armariaapi.AddFolder("Blogs", options) + folder, err := armaria.AddFolder("Blogs", options) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -56,15 +55,15 @@ func TestUpdateFolderParentID(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - options := armariaapi.DefaultAddFolderOptions() + options := armaria.DefaultAddFolderOptions() options.WithDB(db) - programming, err := armariaapi.AddFolder("Programming", options) + programming, err := armaria.AddFolder("Programming", options) if err != nil { t.Errorf("unexpected error: %s", err) } - blogs, err := armariaapi.AddFolder("Blogs", options) + blogs, err := armaria.AddFolder("Blogs", options) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -102,17 +101,17 @@ func TestUpdateFolderRemoveParentID(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - options := armariaapi.DefaultAddFolderOptions() + options := armaria.DefaultAddFolderOptions() options.WithDB(db) - programming, err := armariaapi.AddFolder("Programming", options) + programming, err := armaria.AddFolder("Programming", options) if err != nil { t.Errorf("unexpected error: %s", err) } options.WithParentID(programming.ID) - blogs, err := armariaapi.AddFolder("Blogs", options) + blogs, err := armaria.AddFolder("Blogs", options) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -148,19 +147,19 @@ func TestUpdateFolderOrderStart(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - addOptions := armariaapi.DefaultAddFolderOptions() + addOptions := armaria.DefaultAddFolderOptions() addOptions.WithDB(db) - folder1, err := armariaapi.AddFolder("one", addOptions) + folder1, err := armaria.AddFolder("one", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - folder2, err := armariaapi.AddFolder("two", addOptions) + folder2, err := armaria.AddFolder("two", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - folder3, err := armariaapi.AddFolder("three", addOptions) + folder3, err := armaria.AddFolder("three", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -174,10 +173,10 @@ func TestUpdateFolderOrderStart(t *testing.T) { t.Errorf("unexpected error: %s", err) } - listOptions := armariaapi.DefaultListBooksOptions() + listOptions := armaria.DefaultListBooksOptions() listOptions.WithFolders(true) listOptions.WithDB(db) - books, err := armariaapi.ListBooks(listOptions) + books, err := armaria.ListBooks(listOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -202,19 +201,19 @@ func TestUpdateFolderOrderEnd(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - addOptions := armariaapi.DefaultAddFolderOptions() + addOptions := armaria.DefaultAddFolderOptions() addOptions.WithDB(db) - folder1, err := armariaapi.AddFolder("one", addOptions) + folder1, err := armaria.AddFolder("one", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - folder2, err := armariaapi.AddFolder("two", addOptions) + folder2, err := armaria.AddFolder("two", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - folder3, err := armariaapi.AddFolder("three", addOptions) + folder3, err := armaria.AddFolder("three", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -228,10 +227,10 @@ func TestUpdateFolderOrderEnd(t *testing.T) { t.Errorf("unexpected error: %s", err) } - listOptions := armariaapi.DefaultListBooksOptions() + listOptions := armaria.DefaultListBooksOptions() listOptions.WithFolders(true) listOptions.WithDB(db) - books, err := armariaapi.ListBooks(listOptions) + books, err := armaria.ListBooks(listOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -256,19 +255,19 @@ func TestUpdateFolderOrderBetween(t *testing.T) { db := fmt.Sprintf("%s.db", uuid.New().String()) defer func() { os.Remove(db) }() - addOptions := armariaapi.DefaultAddFolderOptions() + addOptions := armaria.DefaultAddFolderOptions() addOptions.WithDB(db) - folder1, err := armariaapi.AddFolder("one", addOptions) + folder1, err := armaria.AddFolder("one", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - folder2, err := armariaapi.AddFolder("two", addOptions) + folder2, err := armaria.AddFolder("two", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } - folder3, err := armariaapi.AddFolder("three", addOptions) + folder3, err := armaria.AddFolder("three", addOptions) if err != nil { t.Errorf("unexpected error: %s", err) } @@ -283,10 +282,10 @@ func TestUpdateFolderOrderBetween(t *testing.T) { t.Errorf("unexpected error: %s", err) } - listOptions := armariaapi.DefaultListBooksOptions() + listOptions := armaria.DefaultListBooksOptions() listOptions.WithBooks(true) listOptions.WithDB(db) - books, err := armariaapi.ListBooks(listOptions) + books, err := armaria.ListBooks(listOptions) if err != nil { t.Errorf("unexpected error: %s", err) } diff --git a/cmd/seeder/main.go b/cmd/seeder/main.go index b09e013..e4825d7 100644 --- a/cmd/seeder/main.go +++ b/cmd/seeder/main.go @@ -3,7 +3,7 @@ package main import ( "github.com/alecthomas/kong" "github.com/brianvoe/gofakeit/v6" - "github.com/jonathanhope/armaria/pkg/api" + "github.com/jonathanhope/armaria/pkg" "github.com/samber/lo" ) @@ -22,8 +22,8 @@ func (r *SmallCmd) Run(_ *Context) error { folders := make([]string, 0) for i := 0; i < 10; i++ { - fo := armariaapi.DefaultAddFolderOptions() - fr, err := armariaapi.AddFolder(gofakeit.ProductName(), fo) + fo := armaria.DefaultAddFolderOptions() + fr, err := armaria.AddFolder(gofakeit.ProductName(), fo) if err != nil { return err } @@ -34,12 +34,12 @@ func (r *SmallCmd) Run(_ *Context) error { // Add 50 top level bookmarks. for i := 0; i < 50; i++ { - bo := armariaapi. + bo := armaria. DefaultAddBookOptions(). WithDescription(gofakeit.ProductDescription()). WithName(gofakeit.ProductName()). WithTags(tagsFactory()) - _, err := armariaapi.AddBook(gofakeit.URL(), bo) + _, err := armaria.AddBook(gofakeit.URL(), bo) if err != nil { return err } @@ -49,13 +49,13 @@ func (r *SmallCmd) Run(_ *Context) error { for _, f := range folders { for i := 0; i < 50; i++ { - bo := armariaapi. + bo := armaria. DefaultAddBookOptions(). WithDescription(gofakeit.ProductDescription()). WithName(gofakeit.ProductName()). WithTags(tagsFactory()). WithParentID(f) - _, err := armariaapi.AddBook(gofakeit.URL(), bo) + _, err := armaria.AddBook(gofakeit.URL(), bo) if err != nil { return err } @@ -73,8 +73,8 @@ func (r *MediumCmd) Run(_ *Context) error { folders := make([]string, 0) for i := 0; i < 50; i++ { - fo := armariaapi.DefaultAddFolderOptions() - fr, err := armariaapi.AddFolder(gofakeit.ProductName(), fo) + fo := armaria.DefaultAddFolderOptions() + fr, err := armaria.AddFolder(gofakeit.ProductName(), fo) if err != nil { return err } @@ -85,12 +85,12 @@ func (r *MediumCmd) Run(_ *Context) error { // Add 1000 top level bookmarks. for i := 0; i < 1000; i++ { - bo := armariaapi. + bo := armaria. DefaultAddBookOptions(). WithDescription(gofakeit.ProductDescription()). WithName(gofakeit.ProductName()). WithTags(tagsFactory()) - _, err := armariaapi.AddBook(gofakeit.URL(), bo) + _, err := armaria.AddBook(gofakeit.URL(), bo) if err != nil { return err } @@ -100,13 +100,13 @@ func (r *MediumCmd) Run(_ *Context) error { for _, f := range folders { for i := 0; i < 1000; i++ { - bo := armariaapi. + bo := armaria. DefaultAddBookOptions(). WithDescription(gofakeit.ProductDescription()). WithName(gofakeit.ProductName()). WithTags(tagsFactory()). WithParentID(f) - _, err := armariaapi.AddBook(gofakeit.URL(), bo) + _, err := armaria.AddBook(gofakeit.URL(), bo) if err != nil { return err } diff --git a/internal/config/doc.go b/internal/config/doc.go new file mode 100644 index 0000000..f2fed1b --- /dev/null +++ b/internal/config/doc.go @@ -0,0 +1,3 @@ +// config contains the loggic around config files for Armaria. +// The config file is a simple toml file. +package config diff --git a/internal/config/errors.go b/internal/config/errors.go new file mode 100644 index 0000000..5e4967f --- /dev/null +++ b/internal/config/errors.go @@ -0,0 +1,6 @@ +package config + +import "errors" + +// ErrConfigMissing is returned when the config file is missing. +var ErrConfigMissing = errors.New("config missing") diff --git a/pkg/api/get_config.go b/internal/config/get_config.go similarity index 65% rename from pkg/api/get_config.go rename to internal/config/get_config.go index 9bd2332..47cad16 100644 --- a/pkg/api/get_config.go +++ b/internal/config/get_config.go @@ -1,4 +1,4 @@ -package armariaapi +package config import ( "errors" @@ -6,8 +6,6 @@ import ( "os" "strings" - "github.com/jonathanhope/armaria/internal/paths" - "github.com/jonathanhope/armaria/pkg/model" "github.com/knadh/koanf/parsers/toml" "github.com/knadh/koanf/providers/file" "github.com/knadh/koanf/v2" @@ -15,13 +13,8 @@ import ( // GetConfig gets the current config. // If the sentinel error ErrConfigMissing then it doesn't exist. -func GetConfig() (armaria.Config, error) { - config := armaria.Config{} - - configPath, err := paths.Config() - if err != nil { - return config, fmt.Errorf("error getting config path while getting config: %w", err) - } +func GetConfig(configPath string) (Config, error) { + config := Config{} if _, err := os.Stat(configPath); errors.Is(err, os.ErrNotExist) { return config, nil @@ -30,13 +23,13 @@ func GetConfig() (armaria.Config, error) { var k = koanf.New(".") if err := k.Load(file.Provider(configPath), toml.Parser()); err != nil { if strings.Contains(err.Error(), "no such file or directory") { - return config, armaria.ErrConfigMissing + return config, ErrConfigMissing } else { return config, fmt.Errorf("error loading config while getting config: %w", err) } } - err = k.Unmarshal("", &config) + err := k.Unmarshal("", &config) if err != nil { return config, fmt.Errorf("error unmarshalling config while getting config: %w", err) } diff --git a/pkg/model/config.go b/internal/config/model.go similarity index 85% rename from pkg/model/config.go rename to internal/config/model.go index 03e0e6c..5c2e539 100644 --- a/pkg/model/config.go +++ b/internal/config/model.go @@ -1,4 +1,4 @@ -package armaria +package config // Config is the configuration for Armaria. type Config struct { diff --git a/internal/config/update_callback.go b/internal/config/update_callback.go new file mode 100644 index 0000000..ab653cc --- /dev/null +++ b/internal/config/update_callback.go @@ -0,0 +1,4 @@ +package config + +// UpdateConfigCallback is a callback to update the current config +type UpdateConfigCallback func(config *Config) diff --git a/pkg/api/update_config.go b/internal/config/update_config.go similarity index 59% rename from pkg/api/update_config.go rename to internal/config/update_config.go index 07e1bee..3fcbd30 100644 --- a/pkg/api/update_config.go +++ b/internal/config/update_config.go @@ -1,35 +1,25 @@ -package armariaapi +package config import ( "errors" "fmt" "os" - "github.com/jonathanhope/armaria/internal/paths" - "github.com/jonathanhope/armaria/pkg/model" "github.com/knadh/koanf/parsers/toml" "github.com/knadh/koanf/providers/structs" "github.com/knadh/koanf/v2" ) -// UpdateConfigCallback is a callback to update the current config -type UpdateConfigCallback func(config *armaria.Config) - // UpdateConfig updates the current config. // It will be created if it hasn't already been created. -func UpdateConfig(update UpdateConfigCallback) error { - config, err := GetConfig() - if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { +func UpdateConfig(folderPath string, configPath string, update UpdateConfigCallback) error { + config, err := GetConfig(configPath) + if err != nil && !errors.Is(err, ErrConfigMissing) { return fmt.Errorf("error getting config while updating config: %w", err) } - if errors.Is(err, armaria.ErrConfigMissing) { - folder, err := paths.Folder() - if err != nil { - return fmt.Errorf("error getting config folder path while updating config: %w", err) - } - - err = os.MkdirAll(folder, os.ModePerm) + if errors.Is(err, ErrConfigMissing) { + err = os.MkdirAll(folderPath, os.ModePerm) if err != nil { return fmt.Errorf("error making config folder while updating config: %w", err) } @@ -48,11 +38,6 @@ func UpdateConfig(update UpdateConfigCallback) error { return fmt.Errorf("error marshalling config while updating config: %w", err) } - configPath, err := paths.Config() - if err != nil { - return fmt.Errorf("error getting config path while updating config: %w", err) - } - handle, err := os.Create(configPath) if err != nil { return fmt.Errorf("error creating config file while updating config: %w", err) diff --git a/internal/db/book_dto.go b/internal/db/book_dto.go index 87740ac..e84c239 100644 --- a/internal/db/book_dto.go +++ b/internal/db/book_dto.go @@ -1,14 +1,11 @@ package db import ( - "strings" - "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/model" ) -// bookDTO is a DTO to stuff DB results into. -type bookDTO struct { +// BookDTO is a DTO to stuff DB results into. +type BookDTO struct { ID string `db:"id"` URL null.NullString `db:"url"` Name string `db:"name"` @@ -19,27 +16,3 @@ type bookDTO struct { ParentName null.NullString `db:"parent_name"` Tags string `db:"tags"` } - -// toBook converts a bookDTO to a Book. -func (book bookDTO) toBook() armaria.Book { - return armaria.Book{ - ID: book.ID, - URL: null.PtrFromNullString(book.URL), - Name: book.Name, - Description: null.PtrFromNullString(book.Description), - ParentID: null.PtrFromNullString(book.ParentID), - IsFolder: book.IsFolder, - Order: book.Order, - ParentName: null.PtrFromNullString(book.ParentName), - Tags: parseTags(book.Tags), - } -} - -// parseTags parses the tags coming back from the database. -func parseTags(tags string) []string { - if tags == "" { - return make([]string, 0) - } - - return strings.Split(tags, ",") -} diff --git a/internal/db/db.go b/internal/db/db.go index 8c24cb1..2cb4b5e 100644 --- a/internal/db/db.go +++ b/internal/db/db.go @@ -5,9 +5,7 @@ import ( "github.com/google/uuid" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/model" "github.com/nullism/bqb" - "github.com/samber/lo" ) // create @@ -77,13 +75,13 @@ type GetBooksArgs struct { Query null.NullString Tags []string After null.NullString - Order armaria.Order - Direction armaria.Direction + Order Order + Direction Direction First null.NullInt64 } // GetBooks lists bookmarks/folders in the bookmarks DB. -func GetBooks(tx Transaction, args GetBooksArgs) ([]armaria.Book, error) { +func GetBooks(tx Transaction, args GetBooksArgs) ([]BookDTO, error) { tags := bqb.New(`SELECT GROUP_CONCAT("tag")`) tags.Space(`FROM "bookmarks_tags"`) tags.Space(`WHERE "bookmark_id" = "child"."id"`) @@ -139,42 +137,42 @@ func GetBooks(tx Transaction, args GetBooksArgs) ([]armaria.Book, error) { } if args.After.Dirty && args.After.Valid { - if args.Order == armaria.OrderName && args.Direction == armaria.DirectionAsc { + if args.Order == OrderName && args.Direction == DirectionAsc { where.And(`("child"."name" > (SELECT "name" FROM "bookmarks" WHERE "id" = ?)`, args.After.String) - } else if args.Order == armaria.OrderName && args.Direction == armaria.DirectionDesc { + } else if args.Order == OrderName && args.Direction == DirectionDesc { where.And(`("child"."name" < (SELECT "name" FROM "bookmarks" WHERE "id" = ?)`, args.After.String) - } else if args.Order == armaria.OrderModified && args.Direction == armaria.DirectionAsc { + } else if args.Order == OrderModified && args.Direction == DirectionAsc { where.And(`("child"."modified" > (SELECT "modified" FROM "bookmarks" WHERE "id" = ?)`, args.After.String) - } else if args.Order == armaria.OrderModified && args.Direction == armaria.DirectionDesc { + } else if args.Order == OrderModified && args.Direction == DirectionDesc { where.And(`("child"."modified" < (SELECT "modified" FROM "bookmarks" WHERE "id" = ?)`, args.After.String) - } else if args.Order == armaria.OrderManual && args.Direction == armaria.DirectionAsc { + } else if args.Order == OrderManual && args.Direction == DirectionAsc { where.And(`("child"."order" > (SELECT "order" FROM "bookmarks" WHERE "id" = ?)`, args.After.String) - } else if args.Order == armaria.OrderManual && args.Direction == armaria.DirectionDesc { + } else if args.Order == OrderManual && args.Direction == DirectionDesc { where.And(`("child"."order" < (SELECT "order" FROM "bookmarks" WHERE "id" = ?)`, args.After.String) } - if args.Order == armaria.OrderName { + if args.Order == OrderName { where.Or(`("child"."name" = (SELECT "name" from "bookmarks" WHERE "id" = ?) AND "child"."id" > ?))`, args.After.String, args.After.String) - } else if args.Order == armaria.OrderModified { + } else if args.Order == OrderModified { where.Or(`("child"."modified" = (SELECT "modified" from "bookmarks" WHERE "id" = ?) AND "child"."id" > ?))`, args.After.String, args.After.String) - } else if args.Order == armaria.OrderManual { + } else if args.Order == OrderManual { where.Or(`("child"."order" = (SELECT "order" from "bookmarks" WHERE "id" = ?) AND "child"."id" > ?))`, args.After.String, args.After.String) } } books.Space("?", where) - if args.Direction == armaria.DirectionAsc && args.Order == armaria.OrderName { + if args.Direction == DirectionAsc && args.Order == OrderName { books.Space(`ORDER BY "child"."name" ASC`) - } else if args.Direction == armaria.DirectionDesc && args.Order == armaria.OrderName { + } else if args.Direction == DirectionDesc && args.Order == OrderName { books.Space(`ORDER BY "child"."name" DESC`) - } else if args.Direction == armaria.DirectionAsc && args.Order == armaria.OrderModified { + } else if args.Direction == DirectionAsc && args.Order == OrderModified { books.Space(`ORDER BY "child"."modified" ASC`) - } else if args.Direction == armaria.DirectionDesc && args.Order == armaria.OrderModified { + } else if args.Direction == DirectionDesc && args.Order == OrderModified { books.Space(`ORDER BY "child"."modified" DESC`) - } else if args.Direction == armaria.DirectionAsc && args.Order == armaria.OrderManual { + } else if args.Direction == DirectionAsc && args.Order == OrderManual { books.Space(`ORDER BY "child"."order" ASC`) - } else if args.Direction == armaria.DirectionDesc && args.Order == armaria.OrderManual { + } else if args.Direction == DirectionDesc && args.Order == OrderManual { books.Space(`ORDER BY "child"."order" DESC`) } @@ -182,10 +180,8 @@ func GetBooks(tx Transaction, args GetBooksArgs) ([]armaria.Book, error) { books.Space(`LIMIT ?`, args.First.Int64) } - results, err := query[bookDTO](tx, books) - return lo.Map(results, func(x bookDTO, index int) armaria.Book { - return x.toBook() - }), err + return query[BookDTO](tx, books) + } // GetTagsArgs are the args for getTagsDB. @@ -194,7 +190,7 @@ type GetTagsArgs struct { TagsFilter []string Query null.NullString After null.NullString - Direction armaria.Direction + Direction Direction First null.NullInt64 } @@ -218,7 +214,7 @@ func GetTags(tx Transaction, args GetTagsArgs) ([]string, error) { } if args.After.Dirty && args.After.Valid { - if args.Direction == armaria.DirectionAsc { + if args.Direction == DirectionAsc { where.And(`"tag" > ?`, args.After.String) } else { where.And(`"tag" < ?`, args.After.String) @@ -227,7 +223,7 @@ func GetTags(tx Transaction, args GetTagsArgs) ([]string, error) { tags.Space(`?`, where) - if args.Direction == armaria.DirectionAsc { + if args.Direction == DirectionAsc { tags.Space(`ORDER BY "tag" ASC, "id" ASC`) } else { tags.Space(`ORDER BY "tag" DESC, "id" ASC`) @@ -252,7 +248,7 @@ func BookFolderExists(tx Transaction, ID string, isFolder bool) (bool, error) { } // GetParentAndChildren gets a parent and all of its children. -func GetParentAndChildren(tx Transaction, ID string) ([]armaria.Book, error) { +func GetParentAndChildren(tx Transaction, ID string) ([]BookDTO, error) { tags := bqb.New(`SELECT GROUP_CONCAT("tag")`) tags.Space(`FROM "bookmarks_tags"`) tags.Space(`WHERE "bookmark_id" = "child"."id"`) @@ -295,10 +291,7 @@ func GetParentAndChildren(tx Transaction, ID string) ([]armaria.Book, error) { books.Comma(`"tags"`) books.Space(`FROM BOOK`) - results, err := query[bookDTO](tx, books) - return lo.Map(results, func(x bookDTO, index int) armaria.Book { - return x.toBook() - }), err + return query[BookDTO](tx, books) } // MaxOrder returns the max order for a given parentID. diff --git a/pkg/model/direction.go b/internal/db/direction.go similarity index 90% rename from pkg/model/direction.go rename to internal/db/direction.go index 33c9ddd..29b6591 100644 --- a/pkg/model/direction.go +++ b/internal/db/direction.go @@ -1,4 +1,4 @@ -package armaria +package db // Direction is the direction results are ordered by. type Direction string diff --git a/pkg/model/order.go b/internal/db/order.go similarity index 91% rename from pkg/model/order.go rename to internal/db/order.go index 0be8744..21ea865 100644 --- a/pkg/model/order.go +++ b/internal/db/order.go @@ -1,4 +1,4 @@ -package armaria +package db // Order is the field results are ordered on. type Order string diff --git a/internal/manifest/chrome_manifest.go b/internal/manifest/chrome_manifest.go new file mode 100644 index 0000000..39e6ce2 --- /dev/null +++ b/internal/manifest/chrome_manifest.go @@ -0,0 +1,10 @@ +package manifest + +// chromeManifest is the data structure that gets marshalled into the app manifest for Chrome. +type chromeManifest struct { + Name string `json:"name"` + Description string `json:"description"` + Path string `json:"path"` + HostType string `json:"type"` + AllowedOrigins []string `json:"allowed_origins"` +} diff --git a/internal/manifest/doc.go b/internal/manifest/doc.go new file mode 100644 index 0000000..1402a43 --- /dev/null +++ b/internal/manifest/doc.go @@ -0,0 +1,3 @@ +// manifest contains the logic needed to install manifests for the native messaging host. +// Browsers require these for extensions to communicate with native messaging hosts. +package manifest diff --git a/internal/manifest/firefox_manifest.go b/internal/manifest/firefox_manifest.go new file mode 100644 index 0000000..31339a6 --- /dev/null +++ b/internal/manifest/firefox_manifest.go @@ -0,0 +1,10 @@ +package manifest + +// firefoxManifest is the data structure that gets marshalled into the app manifest for Firefox. +type firefoxManifest struct { + Name string `json:"name"` + Description string `json:"description"` + Path string `json:"path"` + HostType string `json:"type"` + AllowedExtensions []string `json:"allowed_extensions"` +} diff --git a/internal/manifest/install.go b/internal/manifest/install.go new file mode 100644 index 0000000..5d4d6ab --- /dev/null +++ b/internal/manifest/install.go @@ -0,0 +1,66 @@ +package manifest + +import ( + "encoding/json" + "fmt" + "os" +) + +const name = "armaria" +const description = "Armaria is a fast local-first bookmarks manager." +const hostType = "stdio" +const firefoxExtension = "armaria@net" +const chromeExtension1 = "chrome-extension://cahkgigfdplmhgjbioakkgennhncioli/" +const chromeExtension2 = "chrome-extension://fbnilfpngakppdkddndcnckolmlpghdf/" + +// TODO: Windows needs registry entiries. + +// InstallManifest installs the app manifest. +func InstallManifest(path string, hostPath string, manifestType ManifestType) error { + var err error + var buffer []byte + + if manifestType == ManifestChrome || manifestType == ManifestChromium { + manifest := chromeManifest{ + Name: name, + Description: description, + Path: hostPath, + HostType: hostType, + AllowedOrigins: []string{ + chromeExtension1, + chromeExtension2, + }, + } + + buffer, err = json.Marshal(manifest) + if err != nil { + return fmt.Errorf("error marshalling manifest while installing manifest: %w", err) + } + } else if manifestType == ManifestFirefox { + manifest := firefoxManifest{ + Name: name, + Description: description, + Path: hostPath, + HostType: hostType, + AllowedExtensions: []string{firefoxExtension}, + } + + buffer, err = json.Marshal(manifest) + if err != nil { + return fmt.Errorf("error marshalling manifest while installing manifest: %w", err) + } + } + + handle, err := os.Create(path) + if err != nil { + return fmt.Errorf("error creating manifest file while installing manifest: %w", err) + } + defer handle.Close() + + _, err = handle.Write(buffer) + if err != nil { + return fmt.Errorf("error writing manfiest file contents while installing manifest: %w", err) + } + + return nil +} diff --git a/internal/manifest/manifest_type.go b/internal/manifest/manifest_type.go new file mode 100644 index 0000000..d6a3e75 --- /dev/null +++ b/internal/manifest/manifest_type.go @@ -0,0 +1,10 @@ +package manifest + +// ManifestType is the type of manifest being installed. +type ManifestType int + +const ( + ManifestFirefox ManifestType = iota + 1 + ManifestChrome + ManifestChromium +) diff --git a/pkg/api/add_book.go b/pkg/add_book.go similarity index 57% rename from pkg/api/add_book.go rename to pkg/add_book.go index a19bd07..33066af 100644 --- a/pkg/api/add_book.go +++ b/pkg/add_book.go @@ -1,4 +1,4 @@ -package armariaapi +package armaria import ( "fmt" @@ -7,8 +7,6 @@ import ( "github.com/jonathanhope/armaria/internal/db" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/internal/order" - "github.com/jonathanhope/armaria/internal/validate" - "github.com/jonathanhope/armaria/pkg/model" "github.com/samber/lo" ) @@ -57,75 +55,75 @@ func (o *addBookOptions) WithTags(tags []string) *addBookOptions { } // AddBook adds a bookmark to the bookmarks database. -func AddBook(url string, options *addBookOptions) (armaria.Book, error) { +func AddBook(url string, options *addBookOptions) (Book, error) { config, err := GetConfig() - if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { - return armaria.Book{}, fmt.Errorf("error getting config while adding bookmark: %w", err) + if err != nil && !errors.Is(err, ErrConfigMissing) { + return Book{}, fmt.Errorf("error getting config while adding bookmark: %w", err) } - return db.QueryWithTransaction(options.DB, config.DB, func(tx db.Transaction) (armaria.Book, error) { + return db.QueryWithTransaction(options.DB, config.DB, func(tx db.Transaction) (Book, error) { // Default name to URL if not provided. if !options.Name.Valid { options.Name = null.NullStringFrom(url) } - if err := validate.URL(null.NullStringFrom(url)); err != nil { - return armaria.Book{}, fmt.Errorf("URL validation failed while adding bookmark: %w", err) + if err := validateURL(null.NullStringFrom(url)); err != nil { + return Book{}, fmt.Errorf("URL validation failed while adding bookmark: %w", err) } - if err := validate.Name(options.Name); err != nil { - return armaria.Book{}, fmt.Errorf("name validation failed while adding bookmark: %w", err) + if err := validateName(options.Name); err != nil { + return Book{}, fmt.Errorf("name validation failed while adding bookmark: %w", err) } - if err := validate.Description(options.Description); err != nil { - return armaria.Book{}, fmt.Errorf("description validation failed while adding bookmark: %w", err) + if err := validateDescription(options.Description); err != nil { + return Book{}, fmt.Errorf("description validation failed while adding bookmark: %w", err) } - if err := validate.ParentID(tx, options.ParentID); err != nil { - return armaria.Book{}, fmt.Errorf("parent ID validation failed while adding bookmark: %w", err) + if err := validateParentID(tx, options.ParentID); err != nil { + return Book{}, fmt.Errorf("parent ID validation failed while adding bookmark: %w", err) } - if err := validate.Tags(options.Tags, make([]string, 0)); err != nil { - return armaria.Book{}, fmt.Errorf("tags validation failed while adding bookmark: %w", err) + if err := validateTags(options.Tags, make([]string, 0)); err != nil { + return Book{}, fmt.Errorf("tags validation failed while adding bookmark: %w", err) } previous, err := db.MaxOrder(tx, options.ParentID) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting max order while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting max order while adding bookmark: %w", err) } var current string if previous == "" { current, err = order.Initial() if err != nil { - return armaria.Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) } } else { current, err = order.End(previous) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) } } id, err := db.AddBook(tx, url, options.Name.String, options.Description, options.ParentID, current) if err != nil { - return armaria.Book{}, fmt.Errorf("error while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error while adding bookmark: %w", err) } existingTags, err := db.GetTags(tx, db.GetTagsArgs{ TagsFilter: options.Tags, }) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting tags while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting tags while adding bookmark: %w", err) } tagsToAdd, _ := lo.Difference(options.Tags, existingTags) if err = db.AddTags(tx, tagsToAdd); err != nil { - return armaria.Book{}, fmt.Errorf("error adding tags while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error adding tags while adding bookmark: %w", err) } if err = db.LinkTags(tx, id, options.Tags); err != nil { - return armaria.Book{}, fmt.Errorf("error linking tags while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error linking tags while adding bookmark: %w", err) } books, err := db.GetBooks(tx, db.GetBooksArgs{ @@ -133,9 +131,9 @@ func AddBook(url string, options *addBookOptions) (armaria.Book, error) { IncludeBooks: true, }) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting bookmarks while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting bookmarks while adding bookmark: %w", err) } - return books[0], nil + return toBook(books[0]), nil }) } diff --git a/pkg/api/add_folder.go b/pkg/add_folder.go similarity index 55% rename from pkg/api/add_folder.go rename to pkg/add_folder.go index f4fc4ab..f19a101 100644 --- a/pkg/api/add_folder.go +++ b/pkg/add_folder.go @@ -1,4 +1,4 @@ -package armariaapi +package armaria import ( "errors" @@ -7,8 +7,6 @@ import ( "github.com/jonathanhope/armaria/internal/db" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/internal/order" - "github.com/jonathanhope/armaria/internal/validate" - "github.com/jonathanhope/armaria/pkg/model" ) // addFolderOptions are the optional arguments for AddFolder. @@ -35,42 +33,42 @@ func (o *addFolderOptions) WithParentID(parentID string) *addFolderOptions { } // AddFolder adds a folder to the bookmarks database. -func AddFolder(name string, options *addFolderOptions) (armaria.Book, error) { +func AddFolder(name string, options *addFolderOptions) (Book, error) { config, err := GetConfig() - if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { - return armaria.Book{}, fmt.Errorf("error getting config while adding folder: %w", err) + if err != nil && !errors.Is(err, ErrConfigMissing) { + return Book{}, fmt.Errorf("error getting config while adding folder: %w", err) } - return db.QueryWithTransaction(options.DB, config.DB, func(tx db.Transaction) (armaria.Book, error) { - if err := validate.Name(null.NullStringFrom(name)); err != nil { - return armaria.Book{}, fmt.Errorf("name validation failed while adding folder: %w", err) + return db.QueryWithTransaction(options.DB, config.DB, func(tx db.Transaction) (Book, error) { + if err := validateName(null.NullStringFrom(name)); err != nil { + return Book{}, fmt.Errorf("name validation failed while adding folder: %w", err) } - if err := validate.ParentID(tx, options.ParentID); err != nil { - return armaria.Book{}, fmt.Errorf("parent ID validation failed while adding folder: %w", err) + if err := validateParentID(tx, options.ParentID); err != nil { + return Book{}, fmt.Errorf("parent ID validation failed while adding folder: %w", err) } previous, err := db.MaxOrder(tx, options.ParentID) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting max order while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting max order while adding bookmark: %w", err) } var current string if previous == "" { current, err = order.Initial() if err != nil { - return armaria.Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) } } else { current, err = order.End(previous) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) } } id, err := db.AddFolder(tx, name, options.ParentID, current) if err != nil { - return armaria.Book{}, fmt.Errorf("error while adding folder: %w", err) + return Book{}, fmt.Errorf("error while adding folder: %w", err) } books, err := db.GetBooks(tx, db.GetBooksArgs{ @@ -78,9 +76,9 @@ func AddFolder(name string, options *addFolderOptions) (armaria.Book, error) { IncludeFolders: true, }) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting folders while adding folder: %w", err) + return Book{}, fmt.Errorf("error getting folders while adding folder: %w", err) } - return books[0], nil + return toBook(books[0]), nil }) } diff --git a/pkg/api/add_tags.go b/pkg/add_tags.go similarity index 59% rename from pkg/api/add_tags.go rename to pkg/add_tags.go index 3f62320..02c242a 100644 --- a/pkg/api/add_tags.go +++ b/pkg/add_tags.go @@ -1,4 +1,4 @@ -package armariaapi +package armaria import ( "errors" @@ -6,8 +6,6 @@ import ( "github.com/jonathanhope/armaria/internal/db" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/internal/validate" - "github.com/jonathanhope/armaria/pkg/model" "github.com/samber/lo" ) @@ -28,43 +26,45 @@ func (o *addTagsOptions) WithDB(db string) *addTagsOptions { } // AddTags adds tags to a bookmark in the bookmarks database. -func AddTags(id string, tags []string, options *addTagsOptions) (armaria.Book, error) { +func AddTags(id string, tags []string, options *addTagsOptions) (Book, error) { config, err := GetConfig() - if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { - return armaria.Book{}, fmt.Errorf("error getting config while adding tag: %w", err) + if err != nil && !errors.Is(err, ErrConfigMissing) { + return Book{}, fmt.Errorf("error getting config while adding tag: %w", err) } - return db.QueryWithTransaction(options.DB, config.DB, func(tx db.Transaction) (armaria.Book, error) { + return db.QueryWithTransaction(options.DB, config.DB, func(tx db.Transaction) (Book, error) { books, err := db.GetBooks(tx, db.GetBooksArgs{ IDFilter: id, IncludeBooks: true, }) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting tags while adding tags: %w", err) + return Book{}, fmt.Errorf("error getting tags while adding tags: %w", err) } if len(books) != 1 || books[0].IsFolder { - return armaria.Book{}, armaria.ErrBookNotFound + return Book{}, ErrBookNotFound } - if err := validate.Tags(tags, books[0].Tags); err != nil { - return armaria.Book{}, fmt.Errorf("tags validation failed while adding tags: %w", err) + book := toBook(books[0]) + + if err := validateTags(tags, book.Tags); err != nil { + return Book{}, fmt.Errorf("tags validation failed while adding tags: %w", err) } existingTags, err := db.GetTags(tx, db.GetTagsArgs{ TagsFilter: tags, }) if err != nil { - return armaria.Book{}, err + return Book{}, err } tagsToAdd, _ := lo.Difference(tags, existingTags) if err = db.AddTags(tx, tagsToAdd); err != nil { - return armaria.Book{}, fmt.Errorf("error while adding tags: %w", err) + return Book{}, fmt.Errorf("error while adding tags: %w", err) } if err = db.LinkTags(tx, id, tags); err != nil { - return armaria.Book{}, fmt.Errorf("error linking tags while adding tags: %w", err) + return Book{}, fmt.Errorf("error linking tags while adding tags: %w", err) } books, err = db.GetBooks(tx, db.GetBooksArgs{ @@ -72,9 +72,9 @@ func AddTags(id string, tags []string, options *addTagsOptions) (armaria.Book, e IncludeBooks: true, }) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting bookmarks while adding tags: %w", err) + return Book{}, fmt.Errorf("error getting bookmarks while adding tags: %w", err) } - return books[0], nil + return toBook(books[0]), nil }) } diff --git a/pkg/api/doc.go b/pkg/api/doc.go deleted file mode 100644 index 03264eb..0000000 --- a/pkg/api/doc.go +++ /dev/null @@ -1,5 +0,0 @@ -// armariaapi is the public API for armaria. -// Armaria is a fast, open, and local first bookmarks manager. -// The API makes heavy use of the options pattern: https://www.sohamkamani.com/golang/options-pattern/ -// Many of the types used by this API can be found in the armaria package. -package armariaapi diff --git a/pkg/api/install_manfiest.go b/pkg/api/install_manfiest.go deleted file mode 100644 index 285505e..0000000 --- a/pkg/api/install_manfiest.go +++ /dev/null @@ -1,95 +0,0 @@ -package armariaapi - -import ( - "encoding/json" - "fmt" - "os" - - "github.com/jonathanhope/armaria/internal/paths" - "github.com/jonathanhope/armaria/pkg/model" -) - -// TODO: Windows needs registry entiries. - -// InstallManifestFirefox installs the app manifest for Firefox. -func InstallManifestFirefox() error { - path, err := paths.FirefoxManifest() - if err != nil { - return fmt.Errorf("error getting firefox manfiest path while installing manifest: %w", err) - } - - return installManifest(path, false) -} - -// InstallManifestChrome installs the app manifest for Firefox. -func InstallManifestChrome() error { - path, err := paths.ChromeManifest() - if err != nil { - return fmt.Errorf("error getting chrome manfiest path while installing manifest: %w", err) - } - - return installManifest(path, true) -} - -// InstallManifestChromium installs the app manifest for Firefox. -func InstallManifestChromium() error { - path, err := paths.ChromiumManifest() - if err != nil { - return fmt.Errorf("error getting chromium manfiest path while installing manifest: %w", err) - } - - return installManifest(path, true) -} - -// installManifest installs the app manifest. -func installManifest(path string, isChrome bool) error { - hostPath, err := paths.Host() - if err != nil { - return fmt.Errorf("error getting host path while installing manifest: %w", err) - } - - var buffer []byte - if isChrome { - manifest := armaria.ChromeManifest{ - Name: "armaria", - Description: "Armaria is a fast local-first bookmarks manager.", - Path: hostPath, - HostType: "stdio", - AllowedOrigins: []string{ - "chrome-extension://cahkgigfdplmhgjbioakkgennhncioli/", - "chrome-extension://fbnilfpngakppdkddndcnckolmlpghdf/", - }, - } - - buffer, err = json.Marshal(manifest) - if err != nil { - return fmt.Errorf("error marshalling manifest while installing manifest: %w", err) - } - } else { - manifest := armaria.FirefoxManifest{ - Name: "armaria", - Description: "Armaria is a fast local-first bookmarks manager.", - Path: hostPath, - HostType: "stdio", - AllowedExtensions: []string{"armaria@armaria.net"}, - } - - buffer, err = json.Marshal(manifest) - if err != nil { - return fmt.Errorf("error marshalling manifest while installing manifest: %w", err) - } - } - - handle, err := os.Create(path) - if err != nil { - return fmt.Errorf("error creating manifest file while installing manifest: %w", err) - } - defer handle.Close() - - _, err = handle.Write(buffer) - if err != nil { - return fmt.Errorf("error writing manfiest file contents while installing manifest: %w", err) - } - - return nil -} diff --git a/pkg/model/book.go b/pkg/book.go similarity index 100% rename from pkg/model/book.go rename to pkg/book.go diff --git a/pkg/config.go b/pkg/config.go new file mode 100644 index 0000000..80f8cc7 --- /dev/null +++ b/pkg/config.go @@ -0,0 +1,5 @@ +package armaria + +import "github.com/jonathanhope/armaria/internal/config" + +type Config = config.Config diff --git a/pkg/direction.go b/pkg/direction.go new file mode 100644 index 0000000..69d9562 --- /dev/null +++ b/pkg/direction.go @@ -0,0 +1,8 @@ +package armaria + +import "github.com/jonathanhope/armaria/internal/db" + +type Direction = db.Direction + +const DirectionAsc = db.DirectionAsc +const DirectionDesc = db.DirectionDesc diff --git a/pkg/doc.go b/pkg/doc.go new file mode 100644 index 0000000..dcdcdd7 --- /dev/null +++ b/pkg/doc.go @@ -0,0 +1,2 @@ +// Armaria is a fast, open, and local first bookmarks manager. +package armaria diff --git a/pkg/model/errors.go b/pkg/errors.go similarity index 95% rename from pkg/model/errors.go rename to pkg/errors.go index f546387..c731a0e 100644 --- a/pkg/model/errors.go +++ b/pkg/errors.go @@ -2,6 +2,8 @@ package armaria import ( "errors" + + "github.com/jonathanhope/armaria/internal/config" ) var ( @@ -45,8 +47,8 @@ var ( ErrInvalidDirection = errors.New("invalid direction") // ErrQueryTooShort is returned when a provided query is too short. ErrQueryTooShort = errors.New("query too short") - // ErrConfigMissing is returned when the config file is missing. - ErrConfigMissing = errors.New("config missing") // ErrInvalidOrdering is returned previous book >= next book for manual ordering. ErrInvalidOrdering = errors.New("invalid ordering") ) + +var ErrConfigMissing = config.ErrConfigMissing diff --git a/pkg/api/get_book.go b/pkg/get_book.go similarity index 63% rename from pkg/api/get_book.go rename to pkg/get_book.go index 876ad1a..9f223f7 100644 --- a/pkg/api/get_book.go +++ b/pkg/get_book.go @@ -1,4 +1,4 @@ -package armariaapi +package armaria import ( "errors" @@ -6,7 +6,6 @@ import ( "github.com/jonathanhope/armaria/internal/db" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/model" ) // getBookOptions are the optional arguments for GetBook. @@ -26,26 +25,26 @@ func (o *getBookOptions) WithDB(db string) *getBookOptions { } // GetBook gets a bookmark in the bookmarks database. -func GetBook(id string, options *getBookOptions) (book armaria.Book, err error) { +func GetBook(id string, options *getBookOptions) (book Book, err error) { config, err := GetConfig() - if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { - return armaria.Book{}, fmt.Errorf("error getting config while getting bookmark: %w", err) + if err != nil && !errors.Is(err, ErrConfigMissing) { + return Book{}, fmt.Errorf("error getting config while getting bookmark: %w", err) } - return db.QueryWithDB(options.DB, config.DB, func(tx db.Transaction) (armaria.Book, error) { + return db.QueryWithDB(options.DB, config.DB, func(tx db.Transaction) (Book, error) { books, err := db.GetBooks(tx, db.GetBooksArgs{ IDFilter: id, IncludeBooks: true, IncludeFolders: true, }) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting bookmarks while getting bookmark: %w", err) + return Book{}, fmt.Errorf("error getting bookmarks while getting bookmark: %w", err) } if len(books) == 0 { - return armaria.Book{}, armaria.ErrNotFound + return Book{}, ErrNotFound } - return books[0], nil + return toBook(books[0]), nil }) } diff --git a/pkg/get_config.go b/pkg/get_config.go new file mode 100644 index 0000000..f180623 --- /dev/null +++ b/pkg/get_config.go @@ -0,0 +1,19 @@ +package armaria + +import ( + "fmt" + + "github.com/jonathanhope/armaria/internal/config" + "github.com/jonathanhope/armaria/internal/paths" +) + +// GetConfig gets the current config. +// If the sentinel error ErrConfigMissing then it doesn't exist. +func GetConfig() (Config, error) { + configPath, err := paths.Config() + if err != nil { + return Config{}, fmt.Errorf("error getting config path while getting config: %w", err) + } + + return config.GetConfig(configPath) +} diff --git a/pkg/api/get_parent_names.go b/pkg/get_parent_names.go similarity index 88% rename from pkg/api/get_parent_names.go rename to pkg/get_parent_names.go index 23b787d..a44a8a4 100644 --- a/pkg/api/get_parent_names.go +++ b/pkg/get_parent_names.go @@ -1,4 +1,4 @@ -package armariaapi +package armaria import ( "fmt" @@ -6,7 +6,6 @@ import ( "errors" "github.com/jonathanhope/armaria/internal/db" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/model" "github.com/samber/lo" ) @@ -29,7 +28,7 @@ func (o *getParentNameOptions) WithDB(db string) *getParentNameOptions { // GetParentNames gets the parent names of a bookmark or folder. func GetParentNames(ID string, options *getParentNameOptions) ([]string, error) { config, err := GetConfig() - if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { + if err != nil && !errors.Is(err, ErrConfigMissing) { return nil, fmt.Errorf("error getting config while getting parent names: %w", err) } @@ -40,7 +39,7 @@ func GetParentNames(ID string, options *getParentNameOptions) ([]string, error) } if len(names) == 0 { - return nil, armaria.ErrNotFound + return nil, ErrNotFound } names = lo.Reverse(names) diff --git a/pkg/install_manfiest.go b/pkg/install_manfiest.go new file mode 100644 index 0000000..603669f --- /dev/null +++ b/pkg/install_manfiest.go @@ -0,0 +1,50 @@ +package armaria + +import ( + "fmt" + + "github.com/jonathanhope/armaria/internal/manifest" + "github.com/jonathanhope/armaria/internal/paths" +) + +// InstallManifestFirefox installs the app manifest for Firefox. +func InstallManifestFirefox() error { + path, err := paths.FirefoxManifest() + if err != nil { + return fmt.Errorf("error getting firefox manfiest path while installing manifest: %w", err) + } + hostPath, err := paths.Host() + if err != nil { + return fmt.Errorf("error getting host path while installing manifest: %w", err) + } + + return manifest.InstallManifest(path, hostPath, manifest.ManifestFirefox) +} + +// InstallManifestChrome installs the app manifest for Firefox. +func InstallManifestChrome() error { + path, err := paths.ChromeManifest() + if err != nil { + return fmt.Errorf("error getting chrome manfiest path while installing manifest: %w", err) + } + hostPath, err := paths.Host() + if err != nil { + return fmt.Errorf("error getting host path while installing manifest: %w", err) + } + + return manifest.InstallManifest(path, hostPath, manifest.ManifestChrome) +} + +// InstallManifestChromium installs the app manifest for Firefox. +func InstallManifestChromium() error { + path, err := paths.ChromiumManifest() + if err != nil { + return fmt.Errorf("error getting chromium manfiest path while installing manifest: %w", err) + } + hostPath, err := paths.Host() + if err != nil { + return fmt.Errorf("error getting host path while installing manifest: %w", err) + } + + return manifest.InstallManifest(path, hostPath, manifest.ManifestChromium) +} diff --git a/pkg/api/list_books.go b/pkg/list_books.go similarity index 78% rename from pkg/api/list_books.go rename to pkg/list_books.go index 6ca7b0f..44353c3 100644 --- a/pkg/api/list_books.go +++ b/pkg/list_books.go @@ -1,4 +1,4 @@ -package armariaapi +package armaria import ( "errors" @@ -6,8 +6,6 @@ import ( "github.com/jonathanhope/armaria/internal/db" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/internal/validate" - "github.com/jonathanhope/armaria/pkg/model" ) // listBookOptions are the optional arguments for ListBooks. @@ -19,8 +17,8 @@ type listBookOptions struct { Query null.NullString Tags []string After null.NullString - Order armaria.Order - Direction armaria.Direction + Order Order + Direction Direction First null.NullInt64 } @@ -29,8 +27,8 @@ func DefaultListBooksOptions() *listBookOptions { return &listBookOptions{ IncludeBookmarks: true, IncludeFolders: true, - Order: armaria.OrderManual, - Direction: armaria.DirectionAsc, + Order: OrderManual, + Direction: DirectionAsc, } } @@ -77,13 +75,13 @@ func (o *listBookOptions) WithAfter(after string) *listBookOptions { } // WithOrder sets the column to order on. -func (o *listBookOptions) WithOrder(order armaria.Order) *listBookOptions { +func (o *listBookOptions) WithOrder(order Order) *listBookOptions { o.Order = order return o } // WithDirection sets the direction to order by. -func (o *listBookOptions) WithDirection(direction armaria.Direction) *listBookOptions { +func (o *listBookOptions) WithDirection(direction Direction) *listBookOptions { o.Direction = direction return o } @@ -101,30 +99,30 @@ func (o *listBookOptions) WithoutParentID() *listBookOptions { } // ListBooks lists bookmarks and folders in the bookmarks database. -func ListBooks(options *listBookOptions) ([]armaria.Book, error) { +func ListBooks(options *listBookOptions) ([]Book, error) { config, err := GetConfig() - if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { + if err != nil && !errors.Is(err, ErrConfigMissing) { return nil, fmt.Errorf("error getting config while listing bookmarks: %w", err) } - return db.QueryWithDB(options.DB, config.DB, func(tx db.Transaction) ([]armaria.Book, error) { + return db.QueryWithDB(options.DB, config.DB, func(tx db.Transaction) ([]Book, error) { if !options.IncludeBookmarks && !options.IncludeFolders { return nil, nil } - if err := validate.First(options.First); err != nil { + if err := validateFirst(options.First); err != nil { return nil, fmt.Errorf("first validation failed while listing bookmarks: %w", err) } - if err := validate.Direction(options.Direction); err != nil { + if err := validateDirection(options.Direction); err != nil { return nil, fmt.Errorf("direction validation failed while listing bookmarks: %w", err) } - if err := validate.Order(options.Order); err != nil { + if err := validateOrder(options.Order); err != nil { return nil, fmt.Errorf("order validation failed while listing bookmarks: %w", err) } - if err := validate.Query(options.Query); err != nil { + if err := validateQuery(options.Query); err != nil { return nil, fmt.Errorf("query validation failed while listing bookmarks: %w", err) } @@ -140,9 +138,9 @@ func ListBooks(options *listBookOptions) ([]armaria.Book, error) { First: options.First, }) if err != nil { - return books, fmt.Errorf("error while listing bookmarks: %w", err) + return nil, fmt.Errorf("error while listing bookmarks: %w", err) } - return books, nil + return toBooks(books), nil }) } diff --git a/pkg/api/list_tags.go b/pkg/list_tags.go similarity index 80% rename from pkg/api/list_tags.go rename to pkg/list_tags.go index b8af480..169071c 100644 --- a/pkg/api/list_tags.go +++ b/pkg/list_tags.go @@ -1,4 +1,4 @@ -package armariaapi +package armaria import ( "errors" @@ -6,8 +6,6 @@ import ( "github.com/jonathanhope/armaria/internal/db" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/internal/validate" - "github.com/jonathanhope/armaria/pkg/model" ) // listTagsOptions are the optional arguments for ListTags. @@ -15,14 +13,14 @@ type listTagsOptions struct { DB null.NullString Query null.NullString After null.NullString - Direction armaria.Direction + Direction Direction First null.NullInt64 } // DefaultListTagsOptions are the default options for ListTags. func DefaultListTagsOptions() *listTagsOptions { return &listTagsOptions{ - Direction: armaria.DirectionAsc, + Direction: DirectionAsc, } } @@ -45,7 +43,7 @@ func (o *listTagsOptions) WithAfter(after string) *listTagsOptions { } // WithDirection sets the direction to order by. -func (o *listTagsOptions) WithDirection(direction armaria.Direction) *listTagsOptions { +func (o *listTagsOptions) WithDirection(direction Direction) *listTagsOptions { o.Direction = direction return o } @@ -59,22 +57,22 @@ func (o *listTagsOptions) WithFirst(first int64) *listTagsOptions { // ListTags lists tags in the bookmarks database. func ListTags(options *listTagsOptions) ([]string, error) { config, err := GetConfig() - if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { + if err != nil && !errors.Is(err, ErrConfigMissing) { return nil, fmt.Errorf("error getting config while listing tags: %w", err) } return db.QueryWithDB(options.DB, config.DB, func(tx db.Transaction) ([]string, error) { tags := make([]string, 0) - if err := validate.First(options.First); err != nil { + if err := validateFirst(options.First); err != nil { return tags, fmt.Errorf("first validation failed while listing tags: %w", err) } - if err := validate.Direction(options.Direction); err != nil { + if err := validateDirection(options.Direction); err != nil { return tags, fmt.Errorf("direction validation failed while listing tags: %w", err) } - if err := validate.Query(options.Query); err != nil { + if err := validateQuery(options.Query); err != nil { return tags, fmt.Errorf("query validation failed while listing tags: %w", err) } diff --git a/pkg/model/doc.go b/pkg/model/doc.go deleted file mode 100644 index f10e637..0000000 --- a/pkg/model/doc.go +++ /dev/null @@ -1,4 +0,0 @@ -// armaria is the public types for Armaria. -// Armaria is a fast, open, and local first bookmarks manager. -// The types need to be separate from the API to avoid circular dependencies. -package armaria diff --git a/pkg/model/manifest.go b/pkg/model/manifest.go deleted file mode 100644 index acd90d4..0000000 --- a/pkg/model/manifest.go +++ /dev/null @@ -1,19 +0,0 @@ -package armaria - -// ChromeManifest is the data structure that gets marshalled into the app manifest for Chrome. -type ChromeManifest struct { - Name string `json:"name"` - Description string `json:"description"` - Path string `json:"path"` - HostType string `json:"type"` - AllowedOrigins []string `json:"allowed_origins"` -} - -// FirefoxManifest is the data structure that gets marshalled into the app manifest for Firefox. -type FirefoxManifest struct { - Name string `json:"name"` - Description string `json:"description"` - Path string `json:"path"` - HostType string `json:"type"` - AllowedExtensions []string `json:"allowed_extensions"` -} diff --git a/pkg/order.go b/pkg/order.go new file mode 100644 index 0000000..223d3c0 --- /dev/null +++ b/pkg/order.go @@ -0,0 +1,9 @@ +package armaria + +import "github.com/jonathanhope/armaria/internal/db" + +type Order = db.Order + +const OrderModified = db.OrderModified +const OrderName = db.OrderName +const OrderManual = db.OrderManual diff --git a/pkg/api/remove_book.go b/pkg/remove_book.go similarity index 86% rename from pkg/api/remove_book.go rename to pkg/remove_book.go index 44928a5..754becf 100644 --- a/pkg/api/remove_book.go +++ b/pkg/remove_book.go @@ -1,4 +1,4 @@ -package armariaapi +package armaria import ( "errors" @@ -6,8 +6,6 @@ import ( "github.com/jonathanhope/armaria/internal/db" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/internal/validate" - "github.com/jonathanhope/armaria/pkg/model" ) // removeBookOptions are the optional arguments for RemoveBook. @@ -29,12 +27,12 @@ func (o *removeBookOptions) WithDB(db string) *removeBookOptions { // RemoveBook removes a bookmark from the bookmarks database. func RemoveBook(id string, options *removeBookOptions) (err error) { config, err := GetConfig() - if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { + if err != nil && !errors.Is(err, ErrConfigMissing) { return fmt.Errorf("error getting config while removing bookmark: %w", err) } return db.ExecWithTransaction(options.DB, config.DB, func(tx db.Transaction) error { - if err := validate.BookID(tx, id); err != nil { + if err := validateBookID(tx, id); err != nil { return fmt.Errorf("bookmark ID validation failed while removing bookmark: %w", err) } @@ -45,7 +43,7 @@ func RemoveBook(id string, options *removeBookOptions) (err error) { if err != nil { return fmt.Errorf("error getting bookmarks while removing bookmark: %w", err) } - book := books[0] + book := toBook(books[0]) if err = db.UnlinkTags(tx, book.ID, book.Tags); err != nil { return fmt.Errorf("error unlinking tags while removing bookmark: %w", err) diff --git a/pkg/api/remove_folder.go b/pkg/remove_folder.go similarity index 85% rename from pkg/api/remove_folder.go rename to pkg/remove_folder.go index c80b837..733c0b7 100644 --- a/pkg/api/remove_folder.go +++ b/pkg/remove_folder.go @@ -1,4 +1,4 @@ -package armariaapi +package armaria import ( "errors" @@ -6,8 +6,6 @@ import ( "github.com/jonathanhope/armaria/internal/db" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/internal/validate" - "github.com/jonathanhope/armaria/pkg/model" "github.com/samber/lo" ) @@ -30,12 +28,12 @@ func (o *removeFolderOptions) WithDB(db string) *removeFolderOptions { // RemoveFolder removes a folder from the bookmarks database. func RemoveFolder(id string, options *removeFolderOptions) error { config, err := GetConfig() - if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { + if err != nil && !errors.Is(err, ErrConfigMissing) { return fmt.Errorf("error getting config while removing folder: %w", err) } return db.ExecWithTransaction(options.DB, config.DB, func(tx db.Transaction) error { - if err := validate.ParentID(tx, null.NullStringFrom(id)); err != nil { + if err := validateParentID(tx, null.NullStringFrom(id)); err != nil { return fmt.Errorf("parent ID validation failed while removing folder: %w", err) } @@ -44,7 +42,7 @@ func RemoveFolder(id string, options *removeFolderOptions) error { return fmt.Errorf("error getting folder and children while removing folder: %w", err) } - for _, bookOrFolder := range lo.Reverse(bookOrFolders) { + for _, bookOrFolder := range lo.Reverse(toBooks(bookOrFolders)) { if !bookOrFolder.IsFolder { if err = db.UnlinkTags(tx, bookOrFolder.ID, bookOrFolder.Tags); err != nil { return fmt.Errorf("error unlinking tags while removing folder: %w", err) diff --git a/pkg/api/remove_tags.go b/pkg/remove_tags.go similarity index 59% rename from pkg/api/remove_tags.go rename to pkg/remove_tags.go index e396be2..1fbb775 100644 --- a/pkg/api/remove_tags.go +++ b/pkg/remove_tags.go @@ -1,4 +1,4 @@ -package armariaapi +package armaria import ( "errors" @@ -6,7 +6,6 @@ import ( "github.com/jonathanhope/armaria/internal/db" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/model" "github.com/samber/lo" ) @@ -27,37 +26,39 @@ func (o *removeTagsOptions) WithDB(db string) *removeTagsOptions { } // RemoveTags removes tags from a bookmark in the bookmarks database. -func RemoveTags(id string, tags []string, options *removeTagsOptions) (armaria.Book, error) { +func RemoveTags(id string, tags []string, options *removeTagsOptions) (Book, error) { config, err := GetConfig() - if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { - return armaria.Book{}, fmt.Errorf("error getting config while removing tags: %w", err) + if err != nil && !errors.Is(err, ErrConfigMissing) { + return Book{}, fmt.Errorf("error getting config while removing tags: %w", err) } - return db.QueryWithTransaction(options.DB, config.DB, func(tx db.Transaction) (armaria.Book, error) { + return db.QueryWithTransaction(options.DB, config.DB, func(tx db.Transaction) (Book, error) { books, err := db.GetBooks(tx, db.GetBooksArgs{ IDFilter: id, IncludeBooks: true, }) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting bookmarks while removing tags: %w", err) + return Book{}, fmt.Errorf("error getting bookmarks while removing tags: %w", err) } if len(books) != 1 || books[0].IsFolder { - return armaria.Book{}, armaria.ErrBookNotFound + return Book{}, ErrBookNotFound } + book := toBook(books[0]) + for _, tag := range tags { - if !lo.Contains(books[0].Tags, tag) { - return armaria.Book{}, armaria.ErrTagNotFound + if !lo.Contains(book.Tags, tag) { + return Book{}, ErrTagNotFound } } - if err = db.UnlinkTags(tx, books[0].ID, tags); err != nil { - return armaria.Book{}, fmt.Errorf("error unlinking tags while removing tags: %w", err) + if err = db.UnlinkTags(tx, book.ID, tags); err != nil { + return Book{}, fmt.Errorf("error unlinking tags while removing tags: %w", err) } if err = db.CleanOrphanedTags(tx, tags); err != nil { - return armaria.Book{}, fmt.Errorf("error cleaning orphaned tags while removing tags: %w", err) + return Book{}, fmt.Errorf("error cleaning orphaned tags while removing tags: %w", err) } books, err = db.GetBooks(tx, db.GetBooksArgs{ @@ -65,9 +66,9 @@ func RemoveTags(id string, tags []string, options *removeTagsOptions) (armaria.B IncludeBooks: true, }) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting bookmarks while removing tags: %w", err) + return Book{}, fmt.Errorf("error getting bookmarks while removing tags: %w", err) } - return books[0], nil + return toBook(books[0]), nil }) } diff --git a/pkg/to_book.go b/pkg/to_book.go new file mode 100644 index 0000000..2ff5cf9 --- /dev/null +++ b/pkg/to_book.go @@ -0,0 +1,40 @@ +package armaria + +import ( + "strings" + + "github.com/jonathanhope/armaria/internal/db" + "github.com/jonathanhope/armaria/internal/null" + "github.com/samber/lo" +) + +// toBooks converts a slice of BookDTO to a slice of Books. +func toBooks(books []db.BookDTO) []Book { + return lo.Map(books, func(book db.BookDTO, _ int) Book { + return toBook(book) + }) +} + +// toBook converts a BookDTO to a Book. +func toBook(book db.BookDTO) Book { + return Book{ + ID: book.ID, + URL: null.PtrFromNullString(book.URL), + Name: book.Name, + Description: null.PtrFromNullString(book.Description), + ParentID: null.PtrFromNullString(book.ParentID), + IsFolder: book.IsFolder, + Order: book.Order, + ParentName: null.PtrFromNullString(book.ParentName), + Tags: parseTags(book.Tags), + } +} + +// parseTags parses the tags coming back from the database. +func parseTags(tags string) []string { + if tags == "" { + return make([]string, 0) + } + + return strings.Split(tags, ",") +} diff --git a/internal/db/book_dto_test.go b/pkg/to_book_test.go similarity index 96% rename from internal/db/book_dto_test.go rename to pkg/to_book_test.go index 2e47251..4404cd4 100644 --- a/internal/db/book_dto_test.go +++ b/pkg/to_book_test.go @@ -1,4 +1,4 @@ -package db +package armaria import ( "fmt" diff --git a/pkg/api/update_book.go b/pkg/update_book.go similarity index 64% rename from pkg/api/update_book.go rename to pkg/update_book.go index 13b517f..135ffb2 100644 --- a/pkg/api/update_book.go +++ b/pkg/update_book.go @@ -1,4 +1,4 @@ -package armariaapi +package armaria import ( "errors" @@ -7,8 +7,6 @@ import ( "github.com/jonathanhope/armaria/internal/db" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/internal/order" - "github.com/jonathanhope/armaria/internal/validate" - "github.com/jonathanhope/armaria/pkg/model" ) // updateBookOptions are the optional arguments for UpdateBook. @@ -82,65 +80,65 @@ func (o *updateBookOptions) WithOrderAfter(id string) *updateBookOptions { } // UpdateBook updates a bookmark in the bookmarks database. -func UpdateBook(id string, options *updateBookOptions) (armaria.Book, error) { +func UpdateBook(id string, options *updateBookOptions) (Book, error) { config, err := GetConfig() - if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { - return armaria.Book{}, fmt.Errorf("error getting config while updating bookmark: %w", err) + if err != nil && !errors.Is(err, ErrConfigMissing) { + return Book{}, fmt.Errorf("error getting config while updating bookmark: %w", err) } - return db.QueryWithTransaction(options.DB, config.DB, func(tx db.Transaction) (armaria.Book, error) { - if err := validate.BookID(tx, id); err != nil { - return armaria.Book{}, fmt.Errorf("bookmark ID validation failed while updating bookmark: %w", err) + return db.QueryWithTransaction(options.DB, config.DB, func(tx db.Transaction) (Book, error) { + if err := validateBookID(tx, id); err != nil { + return Book{}, fmt.Errorf("bookmark ID validation failed while updating bookmark: %w", err) } if !options.Name.Dirty && !options.URL.Dirty && !options.Description.Dirty && !options.ParentID.Dirty && !options.PreviousBook.Dirty && !options.NextBook.Dirty { - return armaria.Book{}, armaria.ErrNoUpdate + return Book{}, ErrNoUpdate } if options.Name.Dirty { - if err := validate.Name(options.Name); err != nil { - return armaria.Book{}, fmt.Errorf("name validation failed while updating bookmark: %w", err) + if err := validateName(options.Name); err != nil { + return Book{}, fmt.Errorf("name validation failed while updating bookmark: %w", err) } } if options.URL.Dirty { - if err := validate.URL(options.URL); err != nil { - return armaria.Book{}, fmt.Errorf("URL validation failed while updating bookmark: %w", err) + if err := validateURL(options.URL); err != nil { + return Book{}, fmt.Errorf("URL validation failed while updating bookmark: %w", err) } } if options.Description.Dirty { - if err := validate.Description(options.Description); err != nil { - return armaria.Book{}, fmt.Errorf("description validation failed while updating bookmark: %w", err) + if err := validateDescription(options.Description); err != nil { + return Book{}, fmt.Errorf("description validation failed while updating bookmark: %w", err) } } if options.ParentID.Dirty { - if err := validate.ParentID(tx, options.ParentID); err != nil { - return armaria.Book{}, fmt.Errorf("parent ID validation failed while updating bookmark: %w", err) + if err := validateParentID(tx, options.ParentID); err != nil { + return Book{}, fmt.Errorf("parent ID validation failed while updating bookmark: %w", err) } } - current, err := validate.Ordering(tx, options.PreviousBook, options.NextBook) + current, err := validateOrdering(tx, options.PreviousBook, options.NextBook) if err != nil { - return armaria.Book{}, fmt.Errorf("ordering validation failed while updating bookmark: %w", err) + return Book{}, fmt.Errorf("ordering validation failed while updating bookmark: %w", err) } if current == "" && options.ParentID.Dirty { previous, err := db.MaxOrder(tx, options.ParentID) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting max order while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting max order while adding bookmark: %w", err) } if previous == "" { current, err = order.Initial() if err != nil { - return armaria.Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) } } else { current, err = order.End(previous) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) } } } @@ -152,7 +150,7 @@ func UpdateBook(id string, options *updateBookOptions) (armaria.Book, error) { ParentID: options.ParentID, Order: current, }); err != nil { - return armaria.Book{}, fmt.Errorf("error while updating bookmark: %w", err) + return Book{}, fmt.Errorf("error while updating bookmark: %w", err) } books, err := db.GetBooks(tx, db.GetBooksArgs{ @@ -160,9 +158,9 @@ func UpdateBook(id string, options *updateBookOptions) (armaria.Book, error) { IncludeBooks: true, }) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting bookmarks while updating bookmark: %w", err) + return Book{}, fmt.Errorf("error getting bookmarks while updating bookmark: %w", err) } - return books[0], nil + return toBook(books[0]), nil }) } diff --git a/pkg/update_config.go b/pkg/update_config.go new file mode 100644 index 0000000..e2de405 --- /dev/null +++ b/pkg/update_config.go @@ -0,0 +1,26 @@ +package armaria + +import ( + "fmt" + + "github.com/jonathanhope/armaria/internal/config" + "github.com/jonathanhope/armaria/internal/paths" +) + +type UpdateConfigCallback = config.UpdateConfigCallback + +// UpdateConfig updates the current config. +// It will be created if it hasn't already been created. +func UpdateConfig(update UpdateConfigCallback) error { + folderPath, err := paths.Folder() + if err != nil { + return fmt.Errorf("error getting config folder path while updating config: %w", err) + } + + configPath, err := paths.Config() + if err != nil { + return fmt.Errorf("error getting config path while updating config: %w", err) + } + + return config.UpdateConfig(folderPath, configPath, update) +} diff --git a/pkg/api/update_folder.go b/pkg/update_folder.go similarity index 62% rename from pkg/api/update_folder.go rename to pkg/update_folder.go index 0ce129f..4958253 100644 --- a/pkg/api/update_folder.go +++ b/pkg/update_folder.go @@ -1,4 +1,4 @@ -package armariaapi +package armaria import ( "errors" @@ -7,8 +7,6 @@ import ( "github.com/jonathanhope/armaria/internal/db" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/internal/order" - "github.com/jonathanhope/armaria/internal/validate" - "github.com/jonathanhope/armaria/pkg/model" ) // updateFolderOptions are the optional arguments for UpdateFolder. @@ -62,53 +60,53 @@ func (o *updateFolderOptions) WithOrderAfter(id string) *updateFolderOptions { } // UpdateFolder updates a folder in the bookmarks database. -func UpdateFolder(id string, options *updateFolderOptions) (armaria.Book, error) { +func UpdateFolder(id string, options *updateFolderOptions) (Book, error) { config, err := GetConfig() - if err != nil && !errors.Is(err, armaria.ErrConfigMissing) { - return armaria.Book{}, fmt.Errorf("error getting config while updating folder: %w", err) + if err != nil && !errors.Is(err, ErrConfigMissing) { + return Book{}, fmt.Errorf("error getting config while updating folder: %w", err) } - return db.QueryWithTransaction(options.DB, config.DB, func(tx db.Transaction) (armaria.Book, error) { - if err := validate.ParentID(tx, null.NullStringFrom(id)); err != nil { - return armaria.Book{}, fmt.Errorf("bookmark ID validation failed while updating folder: %w", err) + return db.QueryWithTransaction(options.DB, config.DB, func(tx db.Transaction) (Book, error) { + if err := validateParentID(tx, null.NullStringFrom(id)); err != nil { + return Book{}, fmt.Errorf("bookmark ID validation failed while updating folder: %w", err) } if !options.Name.Dirty && !options.ParentID.Dirty && !options.PreviousBook.Dirty && !options.NextBook.Dirty { - return armaria.Book{}, armaria.ErrNoUpdate + return Book{}, ErrNoUpdate } if options.Name.Dirty { - if err := validate.Name(options.Name); err != nil { - return armaria.Book{}, fmt.Errorf("name validation failed while updating folder: %w", err) + if err := validateName(options.Name); err != nil { + return Book{}, fmt.Errorf("name validation failed while updating folder: %w", err) } } if options.ParentID.Dirty { - if err := validate.ParentID(tx, options.ParentID); err != nil { - return armaria.Book{}, fmt.Errorf("parent ID validation failed while updating folder: %w", err) + if err := validateParentID(tx, options.ParentID); err != nil { + return Book{}, fmt.Errorf("parent ID validation failed while updating folder: %w", err) } } - current, err := validate.Ordering(tx, options.PreviousBook, options.NextBook) + current, err := validateOrdering(tx, options.PreviousBook, options.NextBook) if err != nil { - return armaria.Book{}, fmt.Errorf("ordering validation failed while updating bookmark: %w", err) + return Book{}, fmt.Errorf("ordering validation failed while updating bookmark: %w", err) } if current == "" && options.ParentID.Dirty { previous, err := db.MaxOrder(tx, options.ParentID) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting max order while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting max order while adding bookmark: %w", err) } if previous == "" { current, err = order.Initial() if err != nil { - return armaria.Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) } } else { current, err = order.End(previous) if err != nil { - return armaria.Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) + return Book{}, fmt.Errorf("error getting current order while adding bookmark: %w", err) } } } @@ -118,7 +116,7 @@ func UpdateFolder(id string, options *updateFolderOptions) (armaria.Book, error) ParentID: options.ParentID, Order: current, }); err != nil { - return armaria.Book{}, fmt.Errorf("error while updating folder: %w", err) + return Book{}, fmt.Errorf("error while updating folder: %w", err) } books, err := db.GetBooks(tx, db.GetBooksArgs{ @@ -126,9 +124,9 @@ func UpdateFolder(id string, options *updateFolderOptions) (armaria.Book, error) IncludeFolders: true, }) if err != nil { - return armaria.Book{}, fmt.Errorf("error geting bookmarks while updating folder: %w", err) + return Book{}, fmt.Errorf("error geting bookmarks while updating folder: %w", err) } - return books[0], nil + return toBook(books[0]), nil }) } diff --git a/internal/validate/validators.go b/pkg/validators.go similarity index 56% rename from internal/validate/validators.go rename to pkg/validators.go index 73820c2..90d91b6 100644 --- a/internal/validate/validators.go +++ b/pkg/validators.go @@ -1,5 +1,4 @@ -// validate contains validation logic that should be run before DB calls are made. -package validate +package armaria import ( "fmt" @@ -8,74 +7,73 @@ import ( "github.com/jonathanhope/armaria/internal/db" "github.com/jonathanhope/armaria/internal/null" "github.com/jonathanhope/armaria/internal/order" - "github.com/jonathanhope/armaria/pkg/model" "github.com/samber/lo" ) -const MaxNameLength = 2048 -const MaxURLLength = 2048 -const MaxDescriptionLength = 4096 -const MaxTagLength = 128 -const MaxTags = 24 -const MinQueryLength = 3 +const maxNameLength = 2048 +const maxURLLength = 2048 +const maxDescriptionLength = 4096 +const maxTagLength = 128 +const maxTags = 24 +const minQueryLength = 3 -// URL validates a URL value. +// validateURL validates a validateURL value. // It's only used for bookmarks and must have a length >= 1 and <= 2048. -func URL(URL null.NullString) error { +func validateURL(URL null.NullString) error { if !URL.Valid || URL.String == "" { - return armaria.ErrURLTooShort + return ErrURLTooShort } - if len(URL.String) > MaxURLLength { - return armaria.ErrURLTooLong + if len(URL.String) > maxURLLength { + return ErrURLTooLong } return nil } -// Name validates a name value. +// validateName validates a name value. // It's required and must have a length >= 1 and <= 1024. -func Name(name null.NullString) error { +func validateName(name null.NullString) error { if !name.Valid || name.String == "" { - return armaria.ErrNameTooShort + return ErrNameTooShort } - if len(name.String) > MaxNameLength { - return armaria.ErrNameTooLong + if len(name.String) > maxNameLength { + return ErrNameTooLong } return nil } -// Description validates a description value. +// validateDescription validates a description value. // IT's optional and must have a length <= 5096. -func Description(description null.NullString) error { +func validateDescription(description null.NullString) error { if !description.Valid { return nil } if description.String == "" { - return armaria.ErrDescriptionTooShort + return ErrDescriptionTooShort } - if len(description.String) > MaxDescriptionLength { - return armaria.ErrDescriptionTooLong + if len(description.String) > maxDescriptionLength { + return ErrDescriptionTooLong } return nil } -// Tags validates a tags value. +// validateTags validates a tags value. // They must be unique. // Each must have a length >= 1 and <= 128. // Each must be comprised of the chars A-Z a-z 0-9 - _ -func Tags(tags []string, existingTags []string) error { +func validateTags(tags []string, existingTags []string) error { if len(tags)+len(existingTags) > 24 { - return armaria.ErrTooManyTags + return ErrTooManyTags } if len(tags) != len(lo.Uniq(tags)) { - return armaria.ErrDuplicateTag + return ErrDuplicateTag } r, err := regexp.Compile(`^[a-zA-Z0-9\-_]*$`) @@ -85,93 +83,93 @@ func Tags(tags []string, existingTags []string) error { for _, tag := range tags { if tag == "" { - return armaria.ErrTagTooShort + return ErrTagTooShort } - if len(tag) > MaxTagLength { - return armaria.ErrTagTooLong + if len(tag) > maxTagLength { + return ErrTagTooLong } matched := r.MatchString(tag) if !matched { - return armaria.ErrTagInvalidChar + return ErrTagInvalidChar } if lo.Contains(existingTags, tag) { - return armaria.ErrDuplicateTag + return ErrDuplicateTag } } return nil } -// First validates a limit value. +// validateFirst validates a limit value. // It's optional, but if it is provided it must be > 0. -func First(limit null.NullInt64) error { +func validateFirst(limit null.NullInt64) error { if !limit.Valid { return nil } if limit.Int64 <= 0 { - return armaria.ErrFirstTooSmall + return ErrFirstTooSmall } return nil } -// Order validates an order value. +// validateOrder validates an order value. // It must be modified or name. -func Order(order armaria.Order) error { - if order != armaria.OrderModified && order != armaria.OrderName && order != armaria.OrderManual { - return armaria.ErrInvalidOrder +func validateOrder(order Order) error { + if order != OrderModified && order != OrderName && order != OrderManual { + return ErrInvalidOrder } return nil } -// Direction validates a direction value. +// validateDirection validates a direction value. // It must be asc or desc. -func Direction(direction armaria.Direction) error { - if direction != armaria.DirectionAsc && direction != armaria.DirectionDesc { - return armaria.ErrInvalidDirection +func validateDirection(direction Direction) error { + if direction != DirectionAsc && direction != DirectionDesc { + return ErrInvalidDirection } return nil } -// Query validates a query value. +// validateQuery validates a query value. // It's optional must be at least 3 chars long. -func Query(query null.NullString) error { +func validateQuery(query null.NullString) error { if !query.Valid { return nil } - if len(query.String) < MinQueryLength { - return armaria.ErrQueryTooShort + if len(query.String) < minQueryLength { + return ErrQueryTooShort } return nil } -// BookID validates a bookmark ID value. +// validateBookID validates a bookmark ID value. // The target bookmark must exist. -func BookID(tx db.Transaction, ID string) error { +func validateBookID(tx db.Transaction, ID string) error { exists, err := db.BookFolderExists(tx, ID, false) if err != nil { return fmt.Errorf("error checking if bookmark exists while validating bookmark ID") } if !exists { - return armaria.ErrBookNotFound + return ErrBookNotFound } return nil } -// ParentID validates a parent ID value. +// validateParentID validates a parent ID value. // It's optional but if it is provided the target parent folder must exist. -func ParentID(tx db.Transaction, parentID null.NullString) error { +func validateParentID(tx db.Transaction, parentID null.NullString) error { if !parentID.Valid { return nil } @@ -182,15 +180,15 @@ func ParentID(tx db.Transaction, parentID null.NullString) error { } if !exists { - return armaria.ErrFolderNotFound + return ErrFolderNotFound } return nil } -// Ordering validates the values used for manual ordering. +// validateOrdering validates the values used for manual ordering. // The previousBook value must be < the nextBook value. -func Ordering(tx db.Transaction, previousID null.NullString, nextID null.NullString) (string, error) { +func validateOrdering(tx db.Transaction, previousID null.NullString, nextID null.NullString) (string, error) { var previousOrder string var nextOrder string @@ -204,7 +202,7 @@ func Ordering(tx db.Transaction, previousID null.NullString, nextID null.NullStr return "", fmt.Errorf("error checking if book exists while validating ordering") } if len(books) == 0 { - return "", armaria.ErrNotFound + return "", ErrNotFound } previousOrder = books[0].Order } @@ -219,13 +217,13 @@ func Ordering(tx db.Transaction, previousID null.NullString, nextID null.NullStr return "", fmt.Errorf("error checking if book exists while validating ordering") } if len(books) == 0 { - return "", armaria.ErrNotFound + return "", ErrNotFound } nextOrder = books[0].Order } if previousOrder != "" && nextOrder != "" && previousOrder >= nextOrder { - return "", armaria.ErrInvalidOrdering + return "", ErrInvalidOrdering } if previousOrder != "" && nextOrder != "" { diff --git a/internal/validate/validators_test.go b/pkg/validators_test.go similarity index 68% rename from internal/validate/validators_test.go rename to pkg/validators_test.go index fcabf55..e4fd41c 100644 --- a/internal/validate/validators_test.go +++ b/pkg/validators_test.go @@ -1,4 +1,4 @@ -package validate +package armaria import ( "database/sql" @@ -8,7 +8,6 @@ import ( "github.com/google/uuid" "github.com/jonathanhope/armaria/internal/null" - "github.com/jonathanhope/armaria/pkg/model" ) func TestURL(t *testing.T) { @@ -18,16 +17,16 @@ func TestURL(t *testing.T) { } tests := []test{ - {input: nullStringOfLength(0, true), want: armaria.ErrURLTooShort}, - {input: nullStringOfLength(0, false), want: armaria.ErrURLTooShort}, + {input: nullStringOfLength(0, true), want: ErrURLTooShort}, + {input: nullStringOfLength(0, false), want: ErrURLTooShort}, {input: nullStringOfLength(1, false), want: nil}, - {input: nullStringOfLength(2049, false), want: armaria.ErrURLTooLong}, + {input: nullStringOfLength(2049, false), want: ErrURLTooLong}, {input: nullStringOfLength(2048, false), want: nil}, } for _, tc := range tests { t.Run(tc.input.String, func(t *testing.T) { - got := URL(tc.input) + got := validateURL(tc.input) validateValidator(t, tc.want, got) }) } @@ -40,16 +39,16 @@ func TestName(t *testing.T) { } tests := []test{ - {input: nullStringOfLength(0, true), want: armaria.ErrNameTooShort}, - {input: nullStringOfLength(0, false), want: armaria.ErrNameTooShort}, + {input: nullStringOfLength(0, true), want: ErrNameTooShort}, + {input: nullStringOfLength(0, false), want: ErrNameTooShort}, {input: nullStringOfLength(1, false), want: nil}, - {input: nullStringOfLength(2049, false), want: armaria.ErrNameTooLong}, + {input: nullStringOfLength(2049, false), want: ErrNameTooLong}, {input: nullStringOfLength(2048, false), want: nil}, } for _, tc := range tests { t.Run(tc.input.String, func(t *testing.T) { - got := Name(tc.input) + got := validateName(tc.input) validateValidator(t, tc.want, got) }) } @@ -63,15 +62,15 @@ func TestDescription(t *testing.T) { tests := []test{ {input: nullStringOfLength(0, true), want: nil}, - {input: nullStringOfLength(0, false), want: armaria.ErrDescriptionTooShort}, + {input: nullStringOfLength(0, false), want: ErrDescriptionTooShort}, {input: nullStringOfLength(1, false), want: nil}, - {input: nullStringOfLength(4097, false), want: armaria.ErrDescriptionTooLong}, + {input: nullStringOfLength(4097, false), want: ErrDescriptionTooLong}, {input: nullStringOfLength(4096, false), want: nil}, } for _, tc := range tests { t.Run(tc.input.String, func(t *testing.T) { - got := Description(tc.input) + got := validateDescription(tc.input) validateValidator(t, tc.want, got) }) } @@ -95,20 +94,20 @@ func TestTags(t *testing.T) { } tests := []test{ - {input: []string{"x", "x"}, existing: []string{}, want: armaria.ErrDuplicateTag}, - {input: []string{"x"}, existing: []string{"x"}, want: armaria.ErrDuplicateTag}, - {input: []string{""}, existing: []string{}, want: armaria.ErrTagTooShort}, + {input: []string{"x", "x"}, existing: []string{}, want: ErrDuplicateTag}, + {input: []string{"x"}, existing: []string{"x"}, want: ErrDuplicateTag}, + {input: []string{""}, existing: []string{}, want: ErrTagTooShort}, {input: []string{"x"}, existing: []string{}, want: nil}, - {input: []string{stringOfLength("x", 129)}, existing: []string{}, want: armaria.ErrTagTooLong}, + {input: []string{stringOfLength("x", 129)}, existing: []string{}, want: ErrTagTooLong}, {input: []string{stringOfLength("x", 128)}, existing: []string{}, want: nil}, - {input: []string{"?"}, existing: []string{}, want: armaria.ErrTagInvalidChar}, - {input: twentyFiveTags, existing: []string{}, want: armaria.ErrTooManyTags}, + {input: []string{"?"}, existing: []string{}, want: ErrTagInvalidChar}, + {input: twentyFiveTags, existing: []string{}, want: ErrTooManyTags}, {input: twentyFourTags, existing: []string{}, want: nil}, } for _, tc := range tests { t.Run(fmt.Sprintf("%+v", tc.input), func(t *testing.T) { - got := Tags(tc.input, tc.existing) + got := validateTags(tc.input, tc.existing) validateValidator(t, tc.want, got) }) } @@ -123,12 +122,12 @@ func TestFirst(t *testing.T) { tests := []test{ {input: nullInt64(1, false), want: nil}, {input: nullInt64(0, true), want: nil}, - {input: nullInt64(0, false), want: armaria.ErrFirstTooSmall}, + {input: nullInt64(0, false), want: ErrFirstTooSmall}, } for _, tc := range tests { t.Run(fmt.Sprintf("%d", tc.input.Int64), func(t *testing.T) { - got := First(tc.input) + got := validateFirst(tc.input) validateValidator(t, tc.want, got) }) } @@ -136,21 +135,21 @@ func TestFirst(t *testing.T) { func TestOrder(t *testing.T) { type test struct { - input armaria.Order + input Order want error } tests := []test{ - {input: armaria.OrderName, want: nil}, - {input: armaria.OrderModified, want: nil}, - {input: armaria.OrderManual, want: nil}, - {input: "", want: armaria.ErrInvalidOrder}, - {input: "Description", want: armaria.ErrInvalidOrder}, + {input: OrderName, want: nil}, + {input: OrderModified, want: nil}, + {input: OrderManual, want: nil}, + {input: "", want: ErrInvalidOrder}, + {input: "Description", want: ErrInvalidOrder}, } for _, tc := range tests { t.Run(string(tc.input), func(t *testing.T) { - got := Order(tc.input) + got := validateOrder(tc.input) validateValidator(t, tc.want, got) }) } @@ -158,20 +157,20 @@ func TestOrder(t *testing.T) { func TestDirection(t *testing.T) { type test struct { - input armaria.Direction + input Direction want error } tests := []test{ - {input: armaria.DirectionAsc, want: nil}, - {input: armaria.DirectionDesc, want: nil}, - {input: "", want: armaria.ErrInvalidDirection}, - {input: "up", want: armaria.ErrInvalidDirection}, + {input: DirectionAsc, want: nil}, + {input: DirectionDesc, want: nil}, + {input: "", want: ErrInvalidDirection}, + {input: "up", want: ErrInvalidDirection}, } for _, tc := range tests { t.Run(string(tc.input), func(t *testing.T) { - got := Direction(tc.input) + got := validateDirection(tc.input) validateValidator(t, tc.want, got) }) } @@ -186,12 +185,12 @@ func TestQuery(t *testing.T) { tests := []test{ {input: nullStringOfLength(0, true), want: nil}, {input: nullStringOfLength(3, false), want: nil}, - {input: nullStringOfLength(2, false), want: armaria.ErrQueryTooShort}, + {input: nullStringOfLength(2, false), want: ErrQueryTooShort}, } for _, tc := range tests { t.Run(tc.input.String, func(t *testing.T) { - got := Query(tc.input) + got := validateQuery(tc.input) validateValidator(t, tc.want, got) }) }