summaryrefslogtreecommitdiff
path: root/d1pkgweb-query.go
blob: 1ed8f6a0365519028ba7e8d88821b97510af2583 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package main

import (
	"bufio"
	"fmt"
	"html/template"
	"net/http"
	"net/http/cgi"
	"os"
	"os/exec"
	"strings"
	"time"
)

type Result struct {
	Name    string
	Version string
	URL     string
	Suite   string
}

type ResultPage struct {
	Query      string
	Release    string
	NumResults int
	Time       string
	Results    []Result
}

var resTempl = `
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" href="/buggers.css" /> 
<style type="text/css">
code {font-weight: normal; 
      margin: 0 0px; 
      padding: 0px 0px; 
      border: 1px solid #ffffff;
      font-size: 95%; } </style>


<title>Devuan Package Information</title>
<body>

    <div class="nav">
    <div class="leftfl">
      <a href="https://pkginfo.devuan.org" title="Devuan Package Information">
          <span>Home</span></a>
        <span class="separator">|</span>
      <a href="https://devuan.org" title="Devuan website">
          <span>Devuan</span></a>
        <span class="separator">|</span>
      <a href="https://git.devuan.org" title="Devuan gitlab">
          <span>Git</span></a>
        <span class="separator">|</span>
      <a href="https://devuan.org" title="Devuan BTS">
          <span>BTS</span></a>
        <span class="separator">|</span>
      <a href="https://dev1galaxy.org" title="dev1galaxy forum">
          <span>Forum</span></a>
        <span class="separator">|</span>
      <a href="https://popcon.devuan.org" title="Popularity Context">
          <span>Popcon</span></a>
   </div>

    <div class="rightfl-do">
  <a title="Please support Devuan development" class="button-do" href="https://devuan.org/os/donate">donate now!</a>
    </div>

    <div class="rightfl-dl">
  <a title="Download Devuan" class="button-dl" href="https://files.devuan.org/">download</a>
    </div>

  </div>


<div class="wrap">

<form method="GET" action="/cgi-bin/d1pkgweb-query">
Search Devuan Packages for: <input type="text" name="search"/> in release 
<select name="release">
<option value="jessie" selected>Jessie 1.0.0</option>
<option value="ascii" >Ascii 2.0.0</option>
<option value="beowulf" >Beowulf (testing)</option>
<option value="ceres" >Ceres (unstable)</option>
<option value="experimental" >Experimental</option>
<option value="any" >Any</option>
</select>
<input type="submit" value="Go!"/>
</form>


<h1>{{.NumResults}} results for <b>"{{.Query}}"</b> in {{.Release}} (in {{.Time}})  </h1>

<ul class="res_list">
{{range .Results}} 
<li class="res_item">
  <a href="{{.URL}}">[{{.Suite}}] {{.Name}}-{{.Version}}</a>
</li>
{{end}}
</div>

</div>
<p style="margin-top:-30px; margin-bottom:30px ; text-align: center;"> This site is a cookie-free zone</p>

   <div class="foot">                   
     <p class="row" id="copyright">
       Copyright (c) 2014-2018  <a href="https://www.dyne.org" data-rel="fiscal sponsor" 
               title="Learn about our fiscal sponsor">Dyne.org foundation</a>
        <span></span>    
        <a rel="license" href="https://creativecommons.org/licenses/by-sa/4.0/" 
           title="This work is licensed under a Creative Commons Attribution-ShareAlike 
         4.0 International License.">CC-BY-SA 4.0 international</a>
        <span></span>    
        <p id="copyright" style="font-size: .8em; line-height: 100%; margin-top: -1.5em; margin-bottom: 2em">
        Devuan is a registered trademark of the Dyne.org foundation. Debian is a registered trademark of
        Software in the Public Interest, Inc. Linux is a registered trademark of Linus Torvalds.</p>
        </div>

</body>
</html>

`

var releaseMap = map[string]string{
	"jessie":       "index_jessie.txt",
	"ascii":        "index_ascii.txt",
	"beowulf":      "index_beowulf.txt",
	"ceres":        "index_ceres.txt",
	"experimental": "index_experimental.txt",
	"any":          "index.txt"}

func printError(errCode int, errMsg string) {

	fmt.Printf("Status:%d %s\r\n", errCode, errMsg)
	fmt.Printf("Content-Type: text/plain\r\n")
	fmt.Printf("\r\n%s\r\n", errMsg)
}

func parseLines(s string) []Result {

	results := make([]Result, 0, 10)

	scanner := bufio.NewScanner(strings.NewReader(s))
	for scanner.Scan() {
		URL := scanner.Text()
		URLParts := strings.Split(URL, "/")[1:]
		pkgNameVer := strings.Split(URLParts[len(URLParts)-1], ".html")[0]
		Name := strings.Split(pkgNameVer, "_")[0]
		Version := strings.Split(pkgNameVer, "_")[1]
		Suite := URLParts[2]
		results = append(results, Result{URL: URL, Name: Name, Version: Version, Suite: Suite})
	}
	//fmt.Printf("len(results): %d\n", resSize)
	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
	var release string

	req.ParseForm()

	searchQuery := req.Form["search"]
	if len(searchQuery) < 1 {
		printError(503, fmt.Sprintf("Something went wrong in parsing query...\r\n%s\r\n", req.Form))
		os.Exit(0)
	}
	res.Query = searchQuery[0]
	QueryTerms := strings.Split(res.Query, " ")

	selectedRelease := req.Form["release"]
	if len(selectedRelease) < 1 {
		release = "any"
	} else {
		release = selectedRelease[0]
	}

	res.Release = release
	fmt.Printf("QueryTerms: %s\n", QueryTerms)

	startTime := time.Now()

	commands := make([]*exec.Cmd, 0)

	cmd := "grep"
	args := []string{QueryTerms[0], releaseMap[release]}

	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 {
		res.Results = parseLines(string(cmdOut[:len(cmdOut)]))
		res.NumResults = len(res.Results)
		res.Time = fmt.Sprintf("%s", time.Since(startTime))
	}
	return res, nil
}

func printResults(results ResultPage) {
	t, err := template.New("webpage").Parse(resTempl)

	if err != nil {
		printError(502, "Something went wrong...")
		return
	}

	fmt.Printf("Status: 200 OK\r\n")
	fmt.Printf("Content-Type: text/html\r\n")
	t.Execute(os.Stdout, results)

}

func main() {
	var req *http.Request
	var err error
	req, err = cgi.Request()
	if err != nil {
		printError(500, "cannot get requested resource"+err.Error())
		return
	}

	res, err := getResults(*req)
	if err != nil {
		printError(404, fmt.Sprintf("%s", err))
	} else {
		printResults(res)
	}

}