/
reindex.go
114 lines (95 loc) · 2.79 KB
/
reindex.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package commands
import (
"fmt"
"github.com/jlentink/aem/internal/aem"
"github.com/jlentink/aem/internal/aem/indexes"
"github.com/jlentink/aem/internal/output"
"github.com/manifoldco/promptui"
"github.com/spf13/cobra"
"os"
"strings"
)
type commandReindex struct {
verbose bool
instanceName string
index string
}
func (c *commandReindex) setup() *cobra.Command {
cmd := &cobra.Command{
Use: "index",
Short: "Reindex index on instance",
Aliases: []string{"reindex"},
PreRun: c.preRun,
Run: c.run,
}
cmd.Flags().StringVarP(&c.instanceName, "name", "n", aem.GetDefaultInstanceName(), "Instance to stop")
cmd.Flags().StringVarP(&c.index, "index", "i", "", "Index to reindex")
return cmd
}
func (c *commandReindex) preRun(cmd *cobra.Command, args []string) {
c.verbose, _ = cmd.Flags().GetBool("verbose")
output.SetVerbose(verbose)
ConfigCheckListProjects()
RegisterProject()
}
func (c *commandReindex) run(cmd *cobra.Command, args []string) {
_, i, errorString, err := getConfigAndInstance(c.instanceName)
if err != nil {
output.Printf(output.NORMAL, errorString, err.Error())
os.Exit(ExitError)
}
if c.index == "" {
searchIndexes, indexErr := indexes.GetIndexes(i)
if indexErr != nil {
output.Printf(output.NORMAL, "Error retrieving indexes. (%s)", err.Error())
os.Exit(ExitError)
}
index := c.searchIndex(searchIndexes)
if index == nil {
output.Printf(output.NORMAL, "Process killed stopping...")
os.Exit(ExitError)
}
c.index = index.Name
}
err = indexes.Reindex(i, c.index)
if err != nil {
output.Printf(output.NORMAL, "Error sending reindex. (%s)", err)
os.Exit(ExitError)
}
}
func (c *commandReindex) searchIndex(indexesList []*indexes.Index) *indexes.Index {
localIndexes := make([]indexes.Index, 0)
for _, cIndex := range indexesList {
localIndexes = append(localIndexes, *cIndex)
}
templates := &promptui.SelectTemplates{
Label: "{{ . }}?",
Active: "\U000027A1 {{ .Name | cyan }} ({{ .Info | red }})",
Inactive: " {{ .Name | cyan }} ({{ .Info | red }})",
Selected: "\U0001F522 ReIndexing... {{ .Name | red | cyan }}",
Details: `--------- Package ----------
{{ "Name:" | faint }} {{ .Name }}
{{ "Reindex count:" | faint }} {{ .ReindexCount }}
{{ "Info:" | faint }} {{ .Info }}
`,
}
searcher := func(input string, index int) bool {
cIndex := indexesList[index]
name := strings.Replace(strings.ToLower(cIndex.Name), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
}
prompt := promptui.Select{
Label: "Select Index",
Items: localIndexes,
Templates: templates,
Size: 20,
Searcher: searcher,
}
in, _, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return nil
}
return &localIndexes[in]
}