Skip to content

Commit

Permalink
feat: added top-level filter to list commands (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanHope authored Nov 11, 2023
1 parent a9e20df commit 1ae5bf1
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 21 deletions.
25 changes: 25 additions & 0 deletions bdd/features/cli_list_all.feature
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ Feature: List All with CLI
| id | parent_id | is_folder | name | url | description | tags |
| [id] | [parent_1_id] | false | https://jho.pe | https://jho.pe | NULL | |

@cli @list_all
Scenario: Can list top level bookmarks/folders
Given the DB already has the following entries:
| id | parent_id | is_folder | name | url | description | tags |
| {parent_1_id} | NULL | true | blogs | NULL | NULL | |
| {id} | [parent_1_id] | false | https://jho.pe | https://jho.pe | NULL | |
When I run it with the following args:
"""
list all --no-folder
"""
Then the folllowing books are returned:
| id | parent_id | is_folder | name | url | description | tags |
| [parent_1_id] | NULL | true | blogs | NULL | NULL | |

@cli @list_all
Scenario: Can search bookmarks/folders
Given the DB already has the following entries:
Expand Down Expand Up @@ -138,3 +152,14 @@ Feature: List All with CLI
"""
Query too short
"""

@cli @list_all
Scenario: Cannot filter by folder and top level at same time
When I run it with the following args:
"""
list all --folder [parent_id] --no-folder
"""
Then the following error is returned:
"""
Arguments folder and no-folder are mutually exclusive
"""
26 changes: 26 additions & 0 deletions bdd/features/cli_list_books.feature
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ Feature: List Books with CLI
| id | parent_id | is_folder | name | url | description | tags |
| [id_1] | [parent_id] | false | https://jho.pe | https://jho.pe | NULL | |

@cli @list_books
Scenario: Can list top-level bookmarks
Given the DB already has the following entries:
| id | parent_id | is_folder | name | url | description | tags |
| {parent_id} | NULL | true | blogs | NULL | NULL | |
| {id_1} | [parent_id] | false | https://jho.pe | https://jho.pe | NULL | |
| {id_2} | NULL | false | https://armaria.net | https://armaria.net | NULL | |
When I run it with the following args:
"""
list books --no-folder
"""
Then the folllowing books are returned:
| id | parent_id | is_folder | name | url | description | tags |
| [id_2] | NULL | false | https://armaria.net | https://armaria.net | NULL | |

@cli @list_books
Scenario: Can search bookmarks
Given the DB already has the following entries:
Expand Down Expand Up @@ -146,3 +161,14 @@ Feature: List Books with CLI
"""
Query too short
"""

@cli @list_books
Scenario: Cannot filter by folder and top level at same time
When I run it with the following args:
"""
list books --folder [parent_id] --no-folder
"""
Then the following error is returned:
"""
Arguments folder and no-folder are mutually exclusive
"""
26 changes: 26 additions & 0 deletions bdd/features/cli_list_folders.feature
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ Feature: List Folder with CLI
| id | parent_id | is_folder | name | url | description | tags |
| [parent_2_id] | [parent_1_id] | true | tech | NULL | NULL | |

@cli @list_folders
Scenario: Can list top level folders
Given the DB already has the following entries:
| id | parent_id | is_folder | name | url | description | tags |
| {parent_1_id} | NULL | true | blogs | NULL | NULL | |
| {parent_2_id} | [parent_1_id] | true | tech | NULL | NULL | |
| {id} | NULL | false | https://jho.pe | https://jho.pe | NULL | |
When I run it with the following args:
"""
list folders --no-folder
"""
Then the folllowing books are returned:
| id | parent_id | is_folder | name | url | description | tags |
| [parent_1_id] | NULL | true | blogs | NULL | NULL | |

@cli @list_folders
Scenario: Can search folders
Given the DB already has the following entries:
Expand Down Expand Up @@ -131,3 +146,14 @@ Feature: List Folder with CLI
"""
Query too short
"""

@cli @list_folders
Scenario: Cannot filter by folder and top level at same time
When I run it with the following args:
"""
list folders --folder [parent_id] --no-folder
"""
Then the following error is returned:
"""
Arguments folder and no-folder are mutually exclusive
"""
72 changes: 51 additions & 21 deletions cli/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,26 @@ func (r *AddTagsCmd) Run(ctx *Context) error {

// ListAllCmd is a CLI command to list bookmarks and folders.
type ListAllCmd struct {
Folder *string `help:"Folder to list bookmarks/folders in."`
After *string `help:"ID of bookmark/folder to return results after."`
Query *string `help:"Query to search bookmarks/folders by."`
Tag []string `help:"Tag to filter bookmarks/folders by."`
Order lib.Order `help:"Field results are ordered on: modified/name." enum:"modified,name" default:"modified"`
Dir lib.Direction `help:"Direction results are ordered by: asc/desc." enum:"asc,desc" default:"asc"`
First *int64 `help:"The max number of bookmarks/folders to return."`
Folder *string `help:"Folder to list bookmarks/folders in."`
NoFolder bool `help:"List top level bookmarks/folders."`
After *string `help:"ID of bookmark/folder to return results after."`
Query *string `help:"Query to search bookmarks/folders by."`
Tag []string `help:"Tag to filter bookmarks/folders by."`
Order lib.Order `help:"Field results are ordered on: modified/name." enum:"modified,name" default:"modified"`
Dir lib.Direction `help:"Direction results are ordered by: asc/desc." enum:"asc,desc" default:"asc"`
First *int64 `help:"The max number of bookmarks/folders to return."`
}

// Run list bookmarks and folders.
func (r *ListAllCmd) Run(ctx *Context) error {
start := time.Now()

if r.NoFolder && r.Folder != nil {
formatError(ctx.Writer, ctx.Formatter, ErrFolderNoFolderMutuallyExclusive)
ctx.ReturnCode(1)
return nil
}

options := lib.DefaultListBooksOptions()
options.WithFolders(true)
options.WithBooks(true)
Expand All @@ -181,6 +188,9 @@ func (r *ListAllCmd) Run(ctx *Context) error {
if r.Folder != nil {
options.WithParentID(*r.Folder)
}
if r.NoFolder {
options.WithoutParentID()
}
if r.After != nil {
options.WithAfter(*r.After)
}
Expand Down Expand Up @@ -217,19 +227,26 @@ func (r *ListAllCmd) Run(ctx *Context) error {

// ListBooksCmd is a CLI command to list bookmarks.
type ListBooksCmd struct {
Folder *string `help:"Folder to list bookmarks in."`
After *string `help:"ID of bookmark to return results after."`
Query *string `help:"Query to search bookmarks by."`
Tag []string `help:"Tag to filter bookmarks by."`
Order lib.Order `help:"Field results are ordered on: modified/name." enum:"modified,name" default:"modified"`
Dir lib.Direction `help:"Direction results are ordered by: asc/desc." enum:"asc,desc" default:"asc"`
First *int64 `help:"The max number of bookmarks to return."`
Folder *string `help:"Folder to list bookmarks in."`
NoFolder bool `help:"List top level bookmarks."`
After *string `help:"ID of bookmark to return results after."`
Query *string `help:"Query to search bookmarks by."`
Tag []string `help:"Tag to filter bookmarks by."`
Order lib.Order `help:"Field results are ordered on: modified/name." enum:"modified,name" default:"modified"`
Dir lib.Direction `help:"Direction results are ordered by: asc/desc." enum:"asc,desc" default:"asc"`
First *int64 `help:"The max number of bookmarks to return."`
}

// Run list bookmarks.
func (r *ListBooksCmd) Run(ctx *Context) error {
start := time.Now()

if r.NoFolder && r.Folder != nil {
formatError(ctx.Writer, ctx.Formatter, ErrFolderNoFolderMutuallyExclusive)
ctx.ReturnCode(1)
return nil
}

options := lib.DefaultListBooksOptions()
options.WithFolders(false)
options.WithBooks(true)
Expand All @@ -239,6 +256,9 @@ func (r *ListBooksCmd) Run(ctx *Context) error {
if r.Folder != nil {
options.WithParentID(*r.Folder)
}
if r.NoFolder {
options.WithoutParentID()
}
if r.After != nil {
options.WithAfter(*r.After)
}
Expand Down Expand Up @@ -275,19 +295,26 @@ func (r *ListBooksCmd) Run(ctx *Context) error {

// ListFoldersCmd is a CLI command to list folders.
type ListFoldersCmd struct {
Folder *string `help:"Folder to list folders in."`
After *string `help:"ID of folder to return results after."`
Query *string `help:"Query to search folders by."`
Tag []string `help:"Tag to filter folders by."`
Order lib.Order `help:"Field results are ordered on: modified/name." enum:"modified,name" default:"modified"`
Dir lib.Direction `help:"Direction results are ordered by: asc/desc." enum:"asc,desc" default:"asc"`
First *int64 `help:"The max number of folders to return."`
Folder *string `help:"Folder to list folders in."`
NoFolder bool `help:"List top level folders."`
After *string `help:"ID of folder to return results after."`
Query *string `help:"Query to search folders by."`
Tag []string `help:"Tag to filter folders by."`
Order lib.Order `help:"Field results are ordered on: modified/name." enum:"modified,name" default:"modified"`
Dir lib.Direction `help:"Direction results are ordered by: asc/desc." enum:"asc,desc" default:"asc"`
First *int64 `help:"The max number of folders to return."`
}

// Run list folders.
func (r *ListFoldersCmd) Run(ctx *Context) error {
start := time.Now()

if r.NoFolder && r.Folder != nil {
formatError(ctx.Writer, ctx.Formatter, ErrFolderNoFolderMutuallyExclusive)
ctx.ReturnCode(1)
return nil
}

options := lib.DefaultListBooksOptions()
options.WithFolders(true)
options.WithBooks(false)
Expand All @@ -297,6 +324,9 @@ func (r *ListFoldersCmd) Run(ctx *Context) error {
if r.Folder != nil {
options.WithParentID(*r.Folder)
}
if r.NoFolder {
options.WithoutParentID()
}
if r.After != nil {
options.WithAfter(*r.After)
}
Expand Down
2 changes: 2 additions & 0 deletions lib/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ func getBooksDB(tx transaction, args getBooksDBArgs) ([]Book, error) {

if args.parentID.Dirty && args.parentID.Valid {
where.And(`"child"."parent_id" = ?`, args.parentID.String)
} else if args.parentID.Dirty && !args.parentID.Valid {
where.And(`"child"."parent_id" IS NULL`)
}

if args.query.Dirty && args.query.Valid {
Expand Down
5 changes: 5 additions & 0 deletions lib/list_books.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ func (o *listBooksOptions) WithFirst(first int64) {
o.first = NullInt64From(first)
}

// WithoutParentID removes the parent ID of a bookmark.
func (o *listBooksOptions) WithoutParentID() {
o.parentID = NullStringFromPtr(nil)
}

// ListBooks lists bookmarks and folders in the bookmarks database.
func ListBooks(options listBooksOptions) ([]Book, error) {
return queryWithDB(options.db, connectDB, func(tx transaction) ([]Book, error) {
Expand Down

0 comments on commit 1ae5bf1

Please sign in to comment.