Skip to content

Commit 76fd5a8

Browse files
committed
fix: re-implement the auto-generate-object-id flag
The v4 API client doesn't have an option for this; it's always true. This is the same behavior as the API. This commit implements this check in the CLI.
1 parent 70cad9c commit 76fd5a8

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

pkg/cmd/objects/import/import.go

+16-4
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ type ImportOptions struct {
2222
SearchClient func() (*search.APIClient, error)
2323
Index string
2424

25-
Scanner *bufio.Scanner
26-
BatchSize int
27-
Wait bool
25+
Scanner *bufio.Scanner
26+
BatchSize int
27+
AutoObjectIDs bool
28+
Wait bool
2829
}
2930

30-
// NewImportCmd creates and returns an import command for indice object
31+
// NewImportCmd creates and returns an import command for records
3132
func NewImportCmd(f *cmdutil.Factory) *cobra.Command {
3233
opts := &ImportOptions{
3334
IO: f.IOStreams,
@@ -76,6 +77,8 @@ func NewImportCmd(f *cmdutil.Factory) *cobra.Command {
7677
_ = cmd.MarkFlagRequired("file")
7778

7879
cmd.Flags().IntVarP(&opts.BatchSize, "batch-size", "b", 1000, "Specify the upload batch size")
80+
cmd.Flags().
81+
BoolVarP(&opts.AutoObjectIDs, "auto-generate-object-id-if-not-exist", "a", false, "Auto-generate objectIDs if they don't exist")
7982
cmd.Flags().BoolVarP(&opts.Wait, "wait", "w", false, "wait for the operation to complete")
8083
return cmd
8184
}
@@ -100,6 +103,15 @@ func runImportCmd(opts *ImportOptions) error {
100103
err := fmt.Errorf("failed to parse JSON object on line %d: %s", count, err)
101104
return err
102105
}
106+
// The API client doesn't support this flag since v4
107+
// The API always automatically generates objectIDs
108+
// if you pass in records without them
109+
// Implement it here to avoid breaking changes
110+
if !opts.AutoObjectIDs {
111+
if _, ok := record["objectID"]; !ok {
112+
return fmt.Errorf("missing objectID on line %d", count)
113+
}
114+
}
103115
records = append(records, record)
104116
count++
105117
}

pkg/cmd/objects/import/import_test.go

+12
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ func Test_runImportCmd(t *testing.T) {
3737
cli: fmt.Sprintf("foo -F '%s'", tmpFile),
3838
wantOut: "✓ Successfully imported 1 objects to foo in",
3939
},
40+
{
41+
name: "missing objectID",
42+
cli: "foo -F -",
43+
stdin: `{"attribute": "foo"}`,
44+
wantErr: "missing objectID on line 0",
45+
},
46+
{
47+
name: "with auto-generated objectID",
48+
cli: "foo --auto-generate-object-id-if-not-exist -F -",
49+
stdin: `{"attribute": "foo"}`,
50+
wantOut: "✓ Successfully imported 1 objects to foo in",
51+
},
4052
{
4153
name: "from stdin with invalid JSON",
4254
cli: "foo -F -",

0 commit comments

Comments
 (0)