Skip to content

Commit

Permalink
feat: added a query command
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanHope committed Jan 13, 2024
1 parent 1a41fdd commit b5d19ff
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
36 changes: 36 additions & 0 deletions cmd/cli/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type RootCmd struct {
Update UpdateCmd `cmd:"" help:"Update a folder or bookmark."`
List ListCmd `cmd:"" help:"List folders, bookmarks, or tags."`
Get GetCmd `cmd:"" help:"Get a folder or bookmark."`
Query QueryCmd `cmd:"" help:"Query folders and bookmarks."`

Config ConfigCmd `cmd:"" help:"Manage the configuration."`
Manifest ManifestCmd `cmd:"" help:"Manage the app manifest."`
Expand Down Expand Up @@ -857,6 +858,41 @@ func (r *VersionCmd) Run(ctx *Context) error {
return nil
}

// QueryCmd is a CLI command to query bookmarks.
type QueryCmd struct {
First int64 `help:"The max number of bookmarks/folders to return." default:"5"`

Query string `arg:"" name:"query" help:"Query to search by."`
}

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

options := armariaapi.DefaultListBooksOptions()
options.WithFolders(true)
options.WithBooks(true)
if ctx.DB != nil {
options.WithDB(*ctx.DB)
}
options.WithFirst(r.First)
options.WithQuery(r.Query)

books, err := armariaapi.ListBooks(options)
if err != nil {
formatError(ctx.Writer, ctx.Formatter, err)
ctx.ReturnCode(1)
return nil
}

elapsed := time.Since(start)

formatBookResults(ctx.Writer, ctx.Formatter, books)
formatSuccess(ctx.Writer, ctx.Formatter, fmt.Sprintf("Listed in %s", elapsed))

return nil
}

// TUICommand is a CLI command to start the TUI.
type TUICommand struct {
}
Expand Down
31 changes: 31 additions & 0 deletions test/features/cli_query.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Feature: Query with CLI

The Armaria CLI can be used to query bookmarks and folders.

@cli @query
Scenario: Can query 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} | NULL | false | https://jho.pe | https://jho.pe | NULL | |
When I run it with the following args:
"""
query blog
"""
Then the folllowing books are returned:
| id | parent_id | is_folder | name | url | description | tags |
| [parent_1_id] | NULL | true | blogs | NULL | NULL | |

@cli @query
Scenario: Can limit query results
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} | NULL | true | blogs | NULL | NULL | |
When I run it with the following args:
"""
query blog --first 1
"""
Then the folllowing books are returned:
| id | parent_id | is_folder | name | url | description | tags |
| [parent_1_id] | NULL | true | blogs | NULL | NULL | |

0 comments on commit b5d19ff

Please sign in to comment.