-
Notifications
You must be signed in to change notification settings - Fork 1
/
detectorConfigs.go
65 lines (51 loc) · 1.66 KB
/
detectorConfigs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"context"
"fmt"
"path/filepath"
"strings"
"github.com/pixlise/core/v4/api/dbCollections"
"github.com/pixlise/core/v4/api/filepaths"
"github.com/pixlise/core/v4/core/fileaccess"
protos "github.com/pixlise/core/v4/generated-protos"
"go.mongodb.org/mongo-driver/mongo"
)
func migrateDetectorConfigs(configBucket string, destConfigBucket string, fs fileaccess.FileAccess, dest *mongo.Database) error {
coll := dest.Collection(dbCollections.DetectorConfigsName)
err := coll.Drop(context.TODO())
if err != nil {
return err
}
detectorConfigPaths, err := fs.ListObjects(configBucket, filepaths.RootDetectorConfig)
if err != nil {
fatalError(err)
}
// This one is directly compatible with the protobuf-defined struct!
destCfgs := []interface{}{}
for _, p := range detectorConfigPaths {
if strings.HasSuffix(p, "pixlise-config.json") {
// Config name is one back in file path
name := filepath.Base(filepath.Dir(p))
cfg := protos.DetectorConfig{}
err = fs.ReadJSON(configBucket, p, &cfg, false)
if err != nil {
return err
}
cfg.Id = name
destCfgs = append(destCfgs, &cfg)
}
}
result, err := coll.InsertMany(context.TODO(), destCfgs)
if err != nil {
return err
}
fmt.Printf("Detector configs inserted: %v\n", len(result.InsertedIDs))
// Copy to the destination bucket too
failOnError := make([]bool, len(detectorConfigPaths))
for c := range failOnError {
failOnError[c] = true
}
s3Copy(fs, configBucket, detectorConfigPaths, destConfigBucket, detectorConfigPaths, failOnError)
fmt.Printf("%v detector config files copied to dest bucket: %v\n", len(detectorConfigPaths), destConfigBucket)
return nil
}