MCPcopy Create free account
hub / github.com/algolia/cli / NewOperationsCmd

Function NewOperationsCmd

pkg/cmd/objects/operations/operations.go:41–95  ·  view source on GitHub ↗

NewOperationsCmd creates and returns an operations command for object operations

(f *cmdutil.Factory, runF func(*OperationsOptions) error)

Source from the content-addressed store, hash-verified

39
40// NewOperationsCmd creates and returns an operations command for object operations
41func 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
97func runOperationsCmd(opts *OperationsOptions) error {
98 client, err := opts.SearchClient()

Calls 5

NewPrintFlagsFunction · 0.92
NoArgsFunction · 0.92
ScanFileFunction · 0.92
runOperationsCmdFunction · 0.85
AddFlagsMethod · 0.45