diff options
-rw-r--r-- | d1pkgweb-query.go | 41 |
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 { |