-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
89 lines (75 loc) · 1.88 KB
/
main.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// SPDX-FileCopyrightText: 2022 Since 2011 Authors of OpenSlides, see https://github.com/OpenSlides/OpenSlides/blob/master/AUTHORS
//
// SPDX-License-Identifier: MIT
// Package main implements the generation of the filter used
// for the searchable fields.
package main
import (
"bufio"
"flag"
"fmt"
log "github.com/sirupsen/logrus"
"os"
"github.com/OpenSlides/openslides-search-service/pkg/meta"
)
func nolog(format string, args ...any) {}
func run(input, filter, output string, verbose bool) error {
lg := nolog
if verbose {
lg = log.Printf
log.SetLevel(log.TraceLevel)
}
lg("Fetching models from %s\n", input)
collections, err := meta.Fetch[meta.Collections](input)
if err != nil {
return fmt.Errorf("loading input failed: %w", err)
}
var f *os.File
var out *bufio.Writer
if output != "" {
if f, err = os.Create(output); err != nil {
return fmt.Errorf("creating output failed: %w", err)
}
out = bufio.NewWriter(f)
} else {
out = bufio.NewWriter(os.Stdout)
}
check := func(e error) {
if err == nil {
err = e
}
}
lg("Writing filter\n")
searchable := meta.RetainStrings()
collections.Retain(func(k, fk string, f *meta.Member) bool {
switch fk {
case "id", "sequential_number":
f.Searchable = false
return true
case "meeting_id":
f.Searchable = true
return true
default:
f.Searchable = true
return searchable(k, fk, f)
}
})
check(collections.AsFilters().Write(out))
check(out.Flush())
if f != nil {
check(f.Close())
}
return err
}
func main() {
var (
input = flag.String("input", "meta/models.yml", "source of input")
filter = flag.String("filter", "", "source of filter")
output = flag.String("output", "", "output file (default STDOUT)")
verbose = flag.Bool("verbose", false, "verbose logging")
)
flag.Parse()
if err := run(*input, *filter, *output, *verbose); err != nil {
log.Fatalf("error: %v\n", err)
}
}