Skip to content

Commit ace1046

Browse files
authored
Merge pull request #12 from bugout-dev/journal-search-order
Added query parameters argument to journal search
2 parents 3405f61 + 3a2b279 commit ace1046

3 files changed

Lines changed: 10 additions & 3 deletions

File tree

cmd/bugout/spire/entries.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,7 @@ func CreateEntriesListCommand() *cobra.Command {
203203
func CreateEntriesSearchCommand() *cobra.Command {
204204
var token, journalID string
205205
var limit, offset int
206+
var queryParams map[string]string
206207
cmd := &cobra.Command{
207208
Use: "search [query]",
208209
Short: "Search across the entries in a Bugout journal",
@@ -215,7 +216,7 @@ func CreateEntriesSearchCommand() *cobra.Command {
215216

216217
searchQuery := strings.Join(args, " ")
217218

218-
entries, err := client.Spire.SearchEntries(token, journalID, searchQuery, limit, offset)
219+
entries, err := client.Spire.SearchEntries(token, journalID, searchQuery, limit, offset, queryParams)
219220
if err != nil {
220221
return err
221222
}
@@ -229,6 +230,7 @@ func CreateEntriesSearchCommand() *cobra.Command {
229230
cmd.Flags().StringVarP(&journalID, "journal", "j", "", "ID of journal")
230231
cmd.Flags().IntVarP(&limit, "limit", "N", 10, "Number of entries per page")
231232
cmd.Flags().IntVarP(&offset, "offset", "n", 0, "Index of starting entry on current page")
233+
cmd.Flags().StringToStringVarP(&queryParams, "params", "p", nil, "Optional query parameters to add to the query")
232234

233235
return cmd
234236
}

pkg/spire/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type SpireCaller interface {
2828
DeleteEntry(token, journalID, entryID string) (Entry, error)
2929
GetEntry(token, journalID, entryID string) (Entry, error)
3030
ListEntries(token, journalID string, limit, offset int) (EntryResultsPage, error)
31-
SearchEntries(token, journalID, searchQuery string, limit, offset int) (EntryResultsPage, error)
31+
SearchEntries(token, journalID, searchQuery string, limit, offset int, queryParameters map[string]string) (EntryResultsPage, error)
3232
TagEntry(token, journalID, entryID string, tags []string) (Entry, error)
3333
UntagEntry(token, journalID, entryID string, tags []string) (Entry, error)
3434
UpdateEntry(token, journalID, entryID, title, content string) (Entry, error)

pkg/spire/entries.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func (client SpireClient) ListEntries(token, journalID string, limit, offset int
138138
return entries, decodeErr
139139
}
140140

141-
func (client SpireClient) SearchEntries(token, journalID, searchQuery string, limit, offset int) (EntryResultsPage, error) {
141+
func (client SpireClient) SearchEntries(token, journalID, searchQuery string, limit, offset int, queryParameters map[string]string) (EntryResultsPage, error) {
142142
entriesRoute := fmt.Sprintf("%s/%s/search", client.Routes.Journals, journalID)
143143
request, requestErr := http.NewRequest("GET", entriesRoute, nil)
144144
if requestErr != nil {
@@ -148,6 +148,11 @@ func (client SpireClient) SearchEntries(token, journalID, searchQuery string, li
148148
request.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
149149
// Pattern taken from Stack Overflow: https://stackoverflow.com/a/30657518/13659585
150150
query := request.URL.Query()
151+
// First add query parameters. q, limit, and offset will override the same parameters passed in
152+
// the queryParameters object.
153+
for k, v := range queryParameters {
154+
query.Add(k, v)
155+
}
151156
query.Add("q", searchQuery)
152157
query.Add("limit", strconv.Itoa(limit))
153158
query.Add("offset", strconv.Itoa(offset))

0 commit comments

Comments
 (0)