NewUpdateCmd creates and returns an update command for index objects
(f *cmdutil.Factory, runF func(*UpdateOptions) error)
| 40 | |
| 41 | // NewUpdateCmd creates and returns an update command for index objects |
| 42 | func NewUpdateCmd(f *cmdutil.Factory, runF func(*UpdateOptions) error) *cobra.Command { |
| 43 | opts := &UpdateOptions{ |
| 44 | IO: f.IOStreams, |
| 45 | Config: f.Config, |
| 46 | SearchClient: f.SearchClient, |
| 47 | PrintFlags: cmdutil.NewPrintFlags(), |
| 48 | } |
| 49 | |
| 50 | cmd := &cobra.Command{ |
| 51 | Use: "update <index> -F <file> [--create-if-not-exists] [--wait] [--continue-on-error]", |
| 52 | Args: validators.ExactArgs(1), |
| 53 | ValidArgsFunction: cmdutil.IndexNames(opts.SearchClient), |
| 54 | Annotations: map[string]string{ |
| 55 | "acls": "addObject", |
| 56 | }, |
| 57 | Short: "Update an index with records from a file", |
| 58 | Long: heredoc.Doc(` |
| 59 | Update a specified index with records from a file. |
| 60 | |
| 61 | The file must contains one JSON object per line (newline delimited JSON objects - ndjson format: https://ndjson.org/). |
| 62 | `), |
| 63 | Example: heredoc.Doc(` |
| 64 | # Update the "MOVIES" index with records from the "objects.ndjson" file |
| 65 | $ algolia objects update MOVIES -F objects.ndjson |
| 66 | |
| 67 | # Update the "MOVIES" index with records from the "objects.ndjson" file and create the records if they don't exist |
| 68 | $ algolia objects update MOVIES -F objects.ndjson --create-if-not-exists |
| 69 | |
| 70 | # Update the "MOVIES" index with records from the "objects.ndjson" file and wait for the operation to complete |
| 71 | $ algolia objects update MOVIES -F objects.ndjson --wait |
| 72 | |
| 73 | # Update the "MOVIES" index with records from the "objects.ndjson" file and continue updating records even if some are invalid |
| 74 | $ algolia objects update MOVIES -F objects.ndjson --continue-on-error |
| 75 | `), |
| 76 | RunE: func(cmd *cobra.Command, args []string) error { |
| 77 | opts.Index = args[0] |
| 78 | |
| 79 | scanner, err := cmdutil.ScanFile(opts.File, opts.IO.In) |
| 80 | if err != nil { |
| 81 | return err |
| 82 | } |
| 83 | opts.Scanner = scanner |
| 84 | |
| 85 | if runF != nil { |
| 86 | return runF(opts) |
| 87 | } |
| 88 | |
| 89 | return runUpdateCmd(opts) |
| 90 | }, |
| 91 | } |
| 92 | |
| 93 | cmd.Flags(). |
| 94 | StringVarP(&opts.File, "file", "F", "", "Records to update from `file` (use \"-\" to read from standard input)") |
| 95 | _ = cmd.MarkFlagRequired("file") |
| 96 | |
| 97 | cmd.Flags(). |
| 98 | BoolVarP(&opts.CreateIfNotExists, "create-if-not-exists", "c", false, "If provided, updating a nonexistent object will create a new one with the objectID and the attributes defined in the file") |
| 99 | cmd.Flags(). |