forked from openshift/ops-sop-search
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindexer.go
More file actions
95 lines (76 loc) · 2.16 KB
/
indexer.go
File metadata and controls
95 lines (76 loc) · 2.16 KB
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
90
91
92
93
94
95
package sopsearch
import (
"fmt"
"log"
"os"
"time"
"github.com/pkg/errors"
)
//Indexer interface which allows indexer to create or update an index
type Indexer interface {
CreateOrUpdateIndex(index, documentID, body string) error
}
//IndexSOP takes the map holding the SOPs and indexes them or updates the index that
//already exists.
func IndexSOP(indexer Indexer, sops map[string]string) error {
for key, content := range sops {
err := indexer.CreateOrUpdateIndex("sop", key, content)
if err != nil {
return err
}
}
return nil
}
//RunIndex performs the indexing. If the index bool is true, then this function will
//perform the indexing routine. If the bool is false, then it won't index. The Config
//object is used to get the elasticsearch url. Once it finishes the indexing routine,
//it will print out how much time it took to index the SOP documents.
func RunIndex(index bool, config Config) error {
if index {
log.Printf("Indexing %s now...\n", config.RepoName)
start := time.Now()
ec, err := NewElasticClient(
[]string{config.ElasticURL}, "", "")
if err != nil {
return err
}
path, err := os.Getwd()
if err != nil {
return err
}
md, ad, err := ScanForFiles(path, config)
if err != nil {
return err
}
sop, err := ToBulkSOP(md, ad)
if err != nil {
return err
}
jmap, err := ToBulkJSON(sop)
if err != nil {
return err
}
err = IndexSOP(&ec, jmap)
if err != nil {
return err
}
elapsed := time.Since(start)
log.Printf("Indexing complete! Time elapsed: %v\n", elapsed)
}
return nil
}
//NeedReIndex performs a git pull to determine if a re-indexing needs to occur. If
//the repo is already up to date, then it will do a RunIndex with false. If it's not,
//then it wil return RunIndex with true.
func NeedReIndex(config Config) error {
ret, err := GitPull(config.GitScript)
if err != nil {
msg := fmt.Sprintf("Could not pull repo %s\n error: %s\n", config.RepoName, err)
return errors.Wrap(err, msg)
}
if string(ret) != string("Already up to date.\n") {
return RunIndex(true, config)
}
log.Printf("Repo %s is up to date, no need to re-index.\n", config.RepoName)
return RunIndex(false, config)
}