| 49 | } |
| 50 | |
| 51 | func search(cmd *cobra.Command, args []string) (err error) { |
| 52 | if len(args) == 0 { |
| 53 | return invalidArgumentsErrorWithDetails("`search` requires a `query` argument", argumentErrorDetails("query")) |
| 54 | } |
| 55 | if len(args) > 2 { |
| 56 | return invalidArgumentsErrorWithDetails("`search` accepts at most one optional `path-scope` argument", mergeJSONErrorDetails(argumentErrorDetails("path-scope"), pathErrorDetails(args[2]))) |
| 57 | } |
| 58 | |
| 59 | var scope string |
| 60 | if len(args) == 2 { |
| 61 | scope = args[1] |
| 62 | if !strings.HasPrefix(scope, "/") { |
| 63 | return invalidArgumentsErrorWithDetails("`search` `path-scope` must begin with \"/\"", mergeJSONErrorDetails(argumentErrorDetails("path-scope"), pathErrorDetails(scope))) |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | opts, err := parseSearchOptions(cmd) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | arg := newSearchV2Arg(args[0], scope, opts) |
| 72 | |
| 73 | dbx := filesNewFunc(config) |
| 74 | res, err := dbx.SearchV2Context(currentContext(), arg) |
| 75 | if err != nil { |
| 76 | return err |
| 77 | } |
| 78 | |
| 79 | var entries []files.IsMetadata |
| 80 | entries = appendSearchMatches(entries, res.Matches, opts.limit) |
| 81 | |
| 82 | for res.HasMore && !searchLimitReached(entries, opts.limit) { |
| 83 | contArg := files.NewSearchV2ContinueArg(res.Cursor) |
| 84 | res, err = dbx.SearchContinueV2Context(currentContext(), contArg) |
| 85 | if err != nil { |
| 86 | return err |
| 87 | } |
| 88 | entries = appendSearchMatches(entries, res.Matches, opts.limit) |
| 89 | } |
| 90 | |
| 91 | sortEntries(entries, opts.list) |
| 92 | |
| 93 | return renderSearchOutput(cmd, args[0], scope, entries, opts) |
| 94 | } |
| 95 | |
| 96 | func parseSearchOptions(cmd *cobra.Command) (searchCommandOptions, error) { |
| 97 | content, _ := cmd.Flags().GetBool("content") |