Skip to content

Commit

Permalink
Merge pull request #33174 from vespa-engine/mpolden/ignore-missing
Browse files Browse the repository at this point in the history
Add option for ignoring non-existent documents
  • Loading branch information
mpolden authored Jan 31, 2025
2 parents 5ba3713 + bcd9bd2 commit 705490c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
22 changes: 13 additions & 9 deletions client/go/internal/cli/cmd/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func sendOperation(op document.Operation, args []string, timeoutSecs int, waiter
return printResult(cli, operationResult(false, doc, service, result), false)
}

func readDocuments(ids []string, timeoutSecs int, waiter *Waiter, printCurl bool, cli *CLI, fieldSet string, headers []string) error {
func readDocuments(ids []string, timeoutSecs int, waiter *Waiter, printCurl bool, cli *CLI, fieldSet string, headers []string, ignoreNotFound bool) error {
parsedIds := make([]document.Id, 0, len(ids))
for _, id := range ids {
parsedId, err := document.ParseId(id)
Expand All @@ -122,7 +122,10 @@ func readDocuments(ids []string, timeoutSecs int, waiter *Waiter, printCurl bool
for _, docId := range parsedIds {
result := client.Get(docId, fieldSet)
if err := printResult(cli, operationResult(true, document.Document{Id: docId}, service, result), true); err != nil {
return err
ignoreErr := ignoreNotFound && result.HTTPStatus == 404
if !ignoreErr {
return err
}
}
}

Expand Down Expand Up @@ -278,11 +281,12 @@ $ vespa document remove id:mynamespace:music::a-head-full-of-dreams`,

func newDocumentGetCmd(cli *CLI) *cobra.Command {
var (
printCurl bool
timeoutSecs int
waitSecs int
fieldSet string
headers []string
printCurl bool
ignoreNotFound bool
timeoutSecs int
waitSecs int
fieldSet string
headers []string
)
cmd := &cobra.Command{
Use: "get id",
Expand All @@ -293,10 +297,11 @@ func newDocumentGetCmd(cli *CLI) *cobra.Command {
Example: `$ vespa document get id:mynamespace:music::a-head-full-of-dreams...`,
RunE: func(cmd *cobra.Command, args []string) error {
waiter := cli.waiter(time.Duration(waitSecs)*time.Second, cmd)
return readDocuments(args, timeoutSecs, waiter, printCurl, cli, fieldSet, headers)
return readDocuments(args, timeoutSecs, waiter, printCurl, cli, fieldSet, headers, ignoreNotFound)
},
}
cmd.Flags().StringVar(&fieldSet, "field-set", "", "Fields to include when reading document")
cmd.Flags().BoolVar(&ignoreNotFound, "ignore-missing", false, "Do not treat non-existent document as an error")
addDocumentFlags(cli, cmd, &printCurl, &timeoutSecs, &waitSecs, &headers)
return cmd
}
Expand Down Expand Up @@ -331,7 +336,6 @@ func printResult(cli *CLI, result OperationResult, payloadOnlyOnSuccess bool) er
}
fmt.Fprintln(out, result.Payload)
}

if !result.Success {
err := errHint(fmt.Errorf("document operation failed"))
err.quiet = true
Expand Down
15 changes: 13 additions & 2 deletions client/go/internal/cli/cmd/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ package cmd
import (
"bytes"
"encoding/json"
"fmt"
"errors"
"io"
"os"
"strconv"
Expand Down Expand Up @@ -131,6 +131,17 @@ func TestDocumentGet(t *testing.T) {
[]string{"id:mynamespace:music::a-head-full-of-dreams"}, t)
}

func TestDocumentGetIgnoreMissing(t *testing.T) {
client := &mock.HTTPClient{}
client.NextResponseString(404, "{\"message\":\"not found\"}")
cli, stdout, stderr := newTestCLI(t)
cli.httpClient = client
assert.Nil(t, cli.Run("document", "get", "-t", "http://127.0.0.1:8080", "--ignore-missing",
"id:mynamespace:music::no-such-doc-1", "id:mynamespace:music::a-head-full-of-dreams"))
assert.Equal(t, "Success: Read id:mynamespace:music::a-head-full-of-dreams\n", stdout.String())
assert.Equal(t, "Error: Invalid document operation: Status 404\n{\n \"message\": \"not found\"\n}\n", stderr.String())
}

func TestDocumentGetWithHeader(t *testing.T) {
client := &mock.HTTPClient{}
assertDocumentGet(client, []string{"document", "get", "--header", "X-Foo: Bar", "id:mynamespace:music::a-head-full-of-dreams"},
Expand Down Expand Up @@ -224,7 +235,7 @@ func assertDocumentGet(client *mock.HTTPClient, args []string, documentIds []str

func assertDocumentTransportError(t *testing.T, errorMessage string) {
client := &mock.HTTPClient{}
client.NextResponseError(fmt.Errorf("%s", errorMessage))
client.NextResponseError(errors.New(errorMessage))
cli, _, stderr := newTestCLI(t)
cli.httpClient = client
assert.NotNil(t, cli.Run("-t", "http://127.0.0.1:8080", "document", "put",
Expand Down

0 comments on commit 705490c

Please sign in to comment.