/
update_external_fields_cmd.go
60 lines (49 loc) · 1.51 KB
/
update_external_fields_cmd.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
package cli
import (
"context"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/ugent-library/biblio-backoffice/backends/ecoom"
"github.com/ugent-library/biblio-backoffice/backends/jcr"
"github.com/ugent-library/biblio-backoffice/models"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
func init() {
rootCmd.AddCommand(updateExternalFields)
}
var updateExternalFields = &cobra.Command{
Use: "update-external-fields",
Short: "Update publication external fields",
RunE: func(cmd *cobra.Command, args []string) error {
mongoClient, err := mongo.Connect(
context.Background(),
options.Client().ApplyURI(config.MongoDBURL))
if err != nil {
return errors.Wrap(err, "unable to initialize connection to mongodb")
}
fixers := []func(context.Context, *models.Publication) error{
ecoom.NewPublicationFixer(mongoClient),
jcr.NewPublicationFixer(mongoClient),
}
repo := newServices().Repo
var lastErr error
ctx := context.TODO()
repo.EachPublicationWithStatus("public", func(p *models.Publication) bool {
for _, fixer := range fixers {
if err := fixer(ctx, p); err != nil {
lastErr = err
return false
}
}
if err := repo.UpdatePublication(p.SnapshotID, p, nil); err != nil {
logger.Errorf("unable to update external fields in publication %s: %s", p.ID, err)
lastErr = err
return false
}
logger.Infof("successfully updated external fields in publication %s", p.ID)
return true
})
return lastErr
},
}