Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added a query command #47

Merged
merged 1 commit into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 | |