summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKatolaZ <katolaz@freaknet.org>2018-01-11 16:14:22 +0000
committerKatolaZ <katolaz@freaknet.org>2018-01-11 16:14:22 +0000
commit3d3245599e656f67b2cb1677507e3c26c73ef6c4 (patch)
treea5e909d6f674df2360ff4d4bbf92583f596d09de
parent1f952f0d59e7b6ae130f45f85ef9f4d077abbe25 (diff)
implemented AND logic for space-separated words
-rw-r--r--d1pkgweb-query.go41
1 files changed, 37 insertions, 4 deletions
diff --git a/d1pkgweb-query.go b/d1pkgweb-query.go
index a6514b5..fe2d099 100644
--- a/d1pkgweb-query.go
+++ b/d1pkgweb-query.go
@@ -36,7 +36,7 @@ Search Devuan Packages for: <input type="text" name="search"/>
</form>
-<div class="title">{{.NumResults}} results for <b>{{.Query}} (in {{.Time}})</b> </div>
+<div class="title">{{.NumResults}} results for <b>"{{.Query}}"</b> (in {{.Time}}) </div>
<ul class="res_list">
{{range .Results}}
@@ -76,6 +76,24 @@ func parseLines(s string) []Result {
return results
}
+func pipeComands(commands []*exec.Cmd) ([]byte, error) {
+
+ for i, command := range commands[:len(commands)-1] {
+ out, err := command.StdoutPipe()
+ if err != nil {
+ return nil, err
+ }
+ command.Start()
+ commands[i+1].Stdin = out
+ }
+ final, err := commands[len(commands)-1].Output()
+ if err != nil {
+ return nil, err
+ }
+ return final, nil
+
+}
+
func getResults(req http.Request) (ResultPage, error) {
var res ResultPage
@@ -89,13 +107,28 @@ func getResults(req http.Request) (ResultPage, error) {
}
res.Query = searchQuery[0]
+ QueryTerms := strings.Split(res.Query, " ")
+
+ fmt.Printf("QueryTerms: %s\n", QueryTerms)
startTime := time.Now()
+ commands := make([]*exec.Cmd, 0)
+
cmd := "grep"
- args := []string{res.Query, "index.txt"}
- if cmdOut, err := exec.Command(cmd, args...).Output(); err != nil {
- //fmt.Printf("error executing command: %s", err)
+ args := []string{QueryTerms[0], "index.txt"}
+
+ commands = append(commands, exec.Command(cmd, args...))
+
+ for _, word := range QueryTerms[1:] {
+ args = []string{word}
+ fmt.Printf("word: %s\r\n", word)
+ commands = append(commands, exec.Command(cmd, args...))
+ }
+
+ //if cmdOut, err := exec.Command(cmd, args...).Output(); err != nil {
+ if cmdOut, err := pipeComands(commands); err != nil {
+ fmt.Printf("error executing command: %s", err)
res.Time = fmt.Sprintf("%s", time.Since(startTime))
return res, nil
} else {