-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmode.go
116 lines (97 loc) · 2.16 KB
/
mode.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
115
116
package main
import (
"fmt"
"os"
"strings"
"github.com/jskcnsl/eesc/ast"
"github.com/jskcnsl/eesc/client"
"github.com/jskcnsl/eesc/config"
"github.com/spf13/cobra"
)
func prepareTargets() ([]string, error) {
if err := config.Validate(); err != nil {
return nil, err
}
query := []string{}
if config.QueryExp != "" {
query = append(query, config.QueryExp)
}
if config.ExpFile != "" {
fmt.Printf("read expression from: %s\n", config.ExpFile)
content, err := os.ReadFile(config.ExpFile)
if err != nil {
return nil, err
}
query = append(query, strings.Split(string(content), "\n")...)
}
result := make([]string, 0, len(query))
for _, q := range query {
tq := strings.TrimSpace(config.JoinExp + " " + q)
if tq != "" {
result = append(result, tq)
}
}
return result, nil
}
func search(cmd *cobra.Command, args []string) error {
targets, err := prepareTargets()
if err != nil {
return err
}
esc, err := client.NewEsClient(config.ServerAddress, config.IndexName)
if err != nil {
return err
}
for _, t := range targets {
o.Writeln(strings.Repeat("#", 32))
o.Writeln(t)
a := ast.NewAst(t)
if err := a.Resolve(); err != nil {
o.Writeln(err.Error())
continue
}
if config.Verbose {
o.Writeln(a.Details())
}
o.Writeln(strings.Repeat("#", 32))
o.Writeln(strings.Repeat("=", 32))
res, err := esc.Search(cmd.Context(), a.Query(), config.Size, config.Offset)
if err != nil {
o.Writeln(err.Error())
} else {
o.Writeln(res)
}
}
return nil
}
func count(cmd *cobra.Command, args []string) error {
targets, err := prepareTargets()
if err != nil {
return err
}
esc, err := client.NewEsClient(config.ServerAddress, config.IndexName)
if err != nil {
return err
}
for _, t := range targets {
o.Writeln(strings.Repeat("#", 32))
o.Writeln(t)
a := ast.NewAst(t)
if err := a.Resolve(); err != nil {
o.Writeln(err.Error())
continue
}
if config.Verbose {
o.Writeln(a.Details())
}
o.Writeln(strings.Repeat("#", 32))
o.Writeln(strings.Repeat("=", 32))
res, err := esc.Count(cmd.Context(), a.Query())
if err != nil {
o.Writeln(err.Error())
} else {
o.Writeln(res)
}
}
return nil
}