NewOperationsCmd creates and returns an operations command for object operations
(f *cmdutil.Factory, runF func(*OperationsOptions) error)
| 39 | |
| 40 | // NewOperationsCmd creates and returns an operations command for object operations |
| 41 | func NewOperationsCmd(f *cmdutil.Factory, runF func(*OperationsOptions) error) *cobra.Command { |
| 42 | opts := &OperationsOptions{ |
| 43 | IO: f.IOStreams, |
| 44 | Config: f.Config, |
| 45 | SearchClient: f.SearchClient, |
| 46 | PrintFlags: cmdutil.NewPrintFlags(), |
| 47 | } |
| 48 | |
| 49 | cmd := &cobra.Command{ |
| 50 | Use: "operations -F <file> [--wait] [--continue-on-errors]", |
| 51 | Args: validators.NoArgs(), |
| 52 | Aliases: []string{"operation", "batch"}, |
| 53 | Annotations: map[string]string{ |
| 54 | "acls": "addObject,deleteObject,deleteIndex", |
| 55 | }, |
| 56 | Short: "Perform several indexing operations", |
| 57 | Long: heredoc.Doc(` |
| 58 | Perform several indexing operations |
| 59 | |
| 60 | The file must contains one single JSON object per line (newline delimited JSON objects - ndjson format: https://ndjson.org/). |
| 61 | Each JSON object must be a valid indexing operation, as documented in the REST API documentation: https://www.algolia.com/doc/rest-api/search/#batch-write-operations-multiple-indices |
| 62 | `), |
| 63 | Example: heredoc.Doc(` |
| 64 | # Batch operations from the "operations.ndjson" file |
| 65 | $ algolia objects operations -F operations.ndjson |
| 66 | `), |
| 67 | RunE: func(cmd *cobra.Command, args []string) error { |
| 68 | scanner, err := cmdutil.ScanFile(opts.File, opts.IO.In) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | opts.Scanner = scanner |
| 73 | |
| 74 | if runF != nil { |
| 75 | return runF(opts) |
| 76 | } |
| 77 | |
| 78 | return runOperationsCmd(opts) |
| 79 | }, |
| 80 | } |
| 81 | |
| 82 | cmd.Flags(). |
| 83 | StringVarP(&opts.File, "file", "F", "", "The file to read the indexing operations from (use \"-\" to read from standard input)") |
| 84 | _ = cmd.MarkFlagRequired("file") |
| 85 | |
| 86 | cmd.Flags(). |
| 87 | BoolVarP(&opts.Wait, "wait", "w", false, "Wait for the indexing operation(s) to complete before returning.") |
| 88 | cmd.Flags(). |
| 89 | BoolVarP(&opts.ContinueOnError, "continue-on-error", "C", false, "Continue processing operations even if some operations are invalid.") |
| 90 | cmd.Flags().BoolVar(&opts.DryRun, "dry-run", false, "Validate and preview the batch request without sending it") |
| 91 | |
| 92 | opts.PrintFlags.AddFlags(cmd) |
| 93 | |
| 94 | return cmd |
| 95 | } |
| 96 | |
| 97 | func runOperationsCmd(opts *OperationsOptions) error { |
| 98 | client, err := opts.SearchClient() |