NewDeleteCmd creates and returns a delete command for index objects
(f *cmdutil.Factory, runF func(*DeleteOptions) error)
| 36 | |
| 37 | // NewDeleteCmd creates and returns a delete command for index objects |
| 38 | func NewDeleteCmd(f *cmdutil.Factory, runF func(*DeleteOptions) error) *cobra.Command { |
| 39 | var confirm bool |
| 40 | |
| 41 | opts := &DeleteOptions{ |
| 42 | IO: f.IOStreams, |
| 43 | Config: f.Config, |
| 44 | SearchClient: f.SearchClient, |
| 45 | PrintFlags: cmdutil.NewPrintFlags(), |
| 46 | } |
| 47 | |
| 48 | cmd := &cobra.Command{ |
| 49 | Use: "delete <index> [--object-ids <object-ids> | --filters <filters>...] [--confirm] [--wait]", |
| 50 | Args: validators.ExactArgs(1), |
| 51 | ValidArgsFunction: cmdutil.IndexNames(opts.SearchClient), |
| 52 | Annotations: map[string]string{ |
| 53 | "acls": "deleteObject", |
| 54 | }, |
| 55 | Short: "Remove records from an index", |
| 56 | Long: heredoc.Doc(` |
| 57 | This command deletes records from the specified index. |
| 58 | |
| 59 | You can specify the records to delete with their objectIDs or use the filter-related flags. |
| 60 | `), |
| 61 | Example: heredoc.Doc(` |
| 62 | # Delete a record with the objectID "1" from the "MOVIES" index |
| 63 | $ algolia objects delete MOVIES --object-ids 1 |
| 64 | |
| 65 | # Delete records with the objectIDs "1" and "2" from the "MOVIES" index |
| 66 | $ algolia objects delete MOVIES --object-ids 1,2 |
| 67 | |
| 68 | # Delete all records matching the filters "type:Scripted" from the "MOVIES" index |
| 69 | $ algolia objects delete MOVIES --filters "type:Scripted" --confirm |
| 70 | `), |
| 71 | RunE: func(cmd *cobra.Command, args []string) error { |
| 72 | opts.Index = args[0] |
| 73 | if err := cmdutil.ValidateNoControlChars("index", opts.Index); err != nil { |
| 74 | return err |
| 75 | } |
| 76 | deleteParams, err := cmdutil.FlagValuesMap(cmd.Flags(), cmdutil.DeleteByParams...) |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | opts.NdeleteParams = len(deleteParams) |
| 81 | |
| 82 | // Convert map into object |
| 83 | tmp, err := json.Marshal(deleteParams) |
| 84 | if err != nil { |
| 85 | return err |
| 86 | } |
| 87 | err = json.Unmarshal(tmp, &opts.DeleteParams) |
| 88 | if err != nil { |
| 89 | return err |
| 90 | } |
| 91 | |
| 92 | if len(opts.ObjectIDs) == 0 && opts.NdeleteParams == 0 { |
| 93 | return cmdutil.FlagErrorf("you must specify either --object-ids or a filter") |
| 94 | } |
| 95 | for _, objectID := range opts.ObjectIDs { |