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

Function runImportCmd

pkg/cmd/objects/import/import.go:85–162  ·  view source on GitHub ↗
(opts *ImportOptions)

Source from the content-addressed store, hash-verified

83}
84
85func runImportCmd(opts *ImportOptions) error {
86 client, err := opts.SearchClient()
87 if err != nil {
88 return err
89 }
90
91 count := 0
92 var records []map[string]any
93 opts.IO.StartProgressIndicatorWithLabel("Importing records")
94 elapsed := time.Now()
95 for opts.Scanner.Scan() {
96 line := opts.Scanner.Text()
97 if line == "" {
98 continue
99 }
100 var record map[string]any
101 if err := json.Unmarshal([]byte(line), &record); err != nil {
102 opts.IO.StopProgressIndicator()
103 return fmt.Errorf("failed to parse JSON object on line %d: %s", count, err)
104 }
105
106 if len(record) == 0 {
107 opts.IO.StopProgressIndicator()
108 return fmt.Errorf("empty object on line %d", count)
109 }
110
111 // The API always generates object IDs for the batch endpoint
112 // Version 3 of the Go API client implemented this option,
113 // but not version 4. Implement it here.
114 if !opts.AutoObjectIDs {
115 if _, ok := record["objectID"]; !ok {
116 return fmt.Errorf("missing objectID on line %d", count)
117 }
118 }
119 records = append(records, record)
120 count++
121 }
122
123 responses, err := client.SaveObjects(opts.Index, records, search.WithBatchSize(opts.BatchSize))
124 if err != nil {
125 return err
126 }
127
128 if opts.Wait {
129 opts.IO.UpdateProgressIndicatorLabel("Waiting for the task to complete")
130 for _, res := range responses {
131 _, err := client.WaitForTask(opts.Index, res.TaskID)
132 if err != nil {
133 opts.IO.StopProgressIndicator()
134 return err
135 }
136 }
137 }
138
139 opts.IO.UpdateProgressIndicatorLabel(
140 fmt.Sprintf("Imported %d objects in %v", len(records), time.Since(elapsed)),
141 )
142

Callers 1

NewImportCmdFunction · 0.70

Calls 8

StopProgressIndicatorMethod · 0.80
ColorSchemeMethod · 0.80
IsStdoutTTYMethod · 0.80
SuccessIconMethod · 0.80
BoldMethod · 0.80
ErrorfMethod · 0.65

Tested by

no test coverage detected