This repository has been archived by the owner on Sep 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
request.go
264 lines (250 loc) · 6.84 KB
/
request.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
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
262
263
264
// Package request obtains and writes the data of the group to various formats.
package request
import (
"database/sql"
"errors"
"fmt"
"io"
"os"
"strings"
"text/template"
"time"
"github.com/Defacto2/df2/pkg/database"
"github.com/Defacto2/df2/pkg/groups/internal/acronym"
"github.com/Defacto2/df2/pkg/groups/internal/filter"
"github.com/Defacto2/df2/pkg/str"
)
var ErrPointer = errors.New("pointer value cannot be nil")
// Flags for group functions.
type Flags struct {
Filter string // Filter groups by category.
Counts bool // Counts the group's total files.
Initialisms bool // Initialisms and acronyms for groups.
Progress bool // Progress counter when requesting database data.
}
// Result on a group.
type Result struct {
ID string // ID used in URLs to link to the group.
Name string // Name of the group.
Count int // Count file totals.
Initialism string // Initialism or acronym.
HR bool // Inject a HR element to separate a collection of groups.
}
// DataList saves an auto-complete list for HTML input elements to the dest file path.
func (r Flags) DataList(db *sql.DB, w, dest io.Writer) error {
if db == nil {
return database.ErrDB
}
if w == nil {
w = io.Discard
}
if dest == nil {
dest = io.Discard
}
// <option value="Bitchin ANSI Design" label="BAD (Bitchin ANSI Design)">
tpl := `{{range .}}{{if .Initialism}}<option value="{{.Name}}" label="{{.Initialism}} ({{.Name}})">{{end}}`
tpl += `<option value="{{.Name}}" label="{{.Name}}">{{end}}`
if err := r.Parse(db, w, dest, tpl); err != nil {
return fmt.Errorf("template: %w", err)
}
return nil
}
// HTML prints a snippet listing links to each group, with an optional file count.
// If dest is empty the results will be send to stdout.
func (r Flags) HTML(db *sql.DB, w, dest io.Writer) error {
if db == nil {
return database.ErrDB
}
if w == nil {
w = io.Discard
}
if dest == nil {
dest = io.Discard
}
// <h2><a href="/g/13-omens">13 OMENS</a> 13O</h2><hr>
tpl := `{{range .}}{{if .HR}}<hr>{{end}}<h2><a href="/g/{{.ID}}">{{.Name}}</a>`
tpl += `{{if .Initialism}} ({{.Initialism}}){{end}}{{if .Count}} <small>({{.Count}})</small>{{end}}</h2>{{end}}`
if err := r.Parse(db, w, dest, tpl); err != nil {
return fmt.Errorf("template: %w", err)
}
return nil
}
// Files returns the number of files associated with the named group.
func (r Flags) Files(db *sql.DB, name string) (int, error) {
if db == nil {
return 0, database.ErrDB
}
if !r.Counts {
return 0, nil
}
total, err := filter.Count(db, name)
if err != nil {
return 0, fmt.Errorf("files %q: %w", name, err)
}
return total, nil
}
// Initialism returns the initialism of the named filter.
func (r Flags) Initialism(db *sql.DB, name string) (string, error) {
if db == nil {
return "", database.ErrDB
}
if !r.Initialisms {
return "", nil
}
s, err := acronym.Get(db, name)
if err != nil {
return "", fmt.Errorf("initialism %q: %w", name, err)
}
return s, nil
}
func (r Flags) iterate(db *sql.DB, w io.Writer, groups ...string) (*[]Result, error) {
if db == nil {
return nil, database.ErrDB
}
if w == nil {
w = io.Discard
}
piped := str.Piped()
total := len(groups)
data := make([]Result, total)
var (
hr bool
lastLetter string
)
for i, grp := range groups {
if !piped && r.Progress {
str.Progress(w, r.Filter, i+1, total)
}
lastLetter, hr = filter.UseHr(lastLetter, grp)
c, err := r.Files(db, grp)
if err != nil {
return nil, fmt.Errorf("iterate files %q: %w", grp, err)
}
init, err := r.Initialism(db, grp)
if err != nil {
return nil, fmt.Errorf("iterate initialism %q: %w", grp, err)
}
data[i] = Result{
ID: filter.Slug(grp),
Name: grp,
Count: c,
Initialism: init,
HR: hr,
}
}
return &data, nil
}
// Parse the group template and save it to the named file.
// If the dest is empty, the results will be sent to stdout.
// The HTML returned to stdout is different to the markup saved
// to a file.
func (r Flags) Parse(db *sql.DB, w, dest io.Writer, tmpl string) error {
if db == nil {
return database.ErrDB
}
if w == nil {
w = io.Discard
}
if dest == nil {
dest = io.Discard
}
list, count, err := filter.List(db, w, r.Filter)
if err != nil {
return fmt.Errorf("parse list: %w", err)
}
if !str.Piped() {
if f := r.Filter; f == "" {
fmt.Fprintln(w, count, "matching (all) records found")
} else {
fmt.Fprintf(w, "%d matching %s records found\n", count, f)
}
}
data, err := r.iterate(db, w, list...)
if err != nil {
return fmt.Errorf("parse iterate: %w", err)
}
t, err := template.New("h2").Parse(tmpl)
if err != nil {
return fmt.Errorf("parse template: %w", err)
}
if dest == os.Stdout {
return t.Execute(dest, &data)
}
return r.parse(t, data, dest, count)
}
func (r Flags) parse(t *template.Template, data *[]Result, dest io.Writer, count int) error {
if t == nil {
return fmt.Errorf("%w: t templte", ErrPointer)
}
if data == nil {
return fmt.Errorf("%w: data result", ErrPointer)
}
if dest == nil {
dest = io.Discard
}
switch filter.Get(r.Filter) {
case filter.BBS, filter.FTP, filter.Group, filter.Magazine:
fmt.Fprint(dest, r.prependHTML(count))
if err := t.Execute(dest, &data); err != nil {
return fmt.Errorf("parse execute: %w", err)
}
fmt.Fprintln(dest, "</div>")
case filter.None:
return fmt.Errorf("parse %q: %w", r.Filter, filter.ErrFilter)
}
return nil
}
func (r Flags) prependHTML(total int) string {
now := time.Now()
s := "<div class=\"pagination-statistics\"><span class=\"label label-default\">"
s += fmt.Sprintf("%d %s sites</span>",
total, r.Filter)
s += fmt.Sprintf(" <span class=\"label label-default\">updated, %s</span>", now.Format("2006 Jan 2"))
s += "</div><div class=\"columns-list\" id=\"organisation-drill-down\">"
return s
}
// Print list organisations or groups filtered by a name and summaries the results.
func Print(db *sql.DB, w io.Writer, r Flags) (int, error) {
if db == nil {
return 0, database.ErrDB
}
if w == nil {
w = io.Discard
}
grp, total, err := filter.List(db, w, r.Filter)
if err != nil {
return 0, fmt.Errorf("print groups: %w", err)
}
fmt.Fprintf(w, "\n\n%d matching %q records found\n", total, r.Filter)
a := make([]string, total)
for i, g := range grp {
if r.Progress {
str.Progress(w, r.Filter, i+1, total)
}
s := g
if r.Initialisms {
if in, err := acronym.Get(db, g); err != nil {
return 0, fmt.Errorf("print initialism: %w", err)
} else if in != "" {
s = fmt.Sprintf("%v [%s]", s, in)
}
}
// file totals
if r.Counts {
c, err := filter.Count(db, g)
if err != nil {
return 0, fmt.Errorf("print counts: %w", err)
}
if c > 0 {
s = fmt.Sprintf("%v (%d)", s, c)
}
}
a[i] = s
}
// remove empty val
if a[0] == "" {
a = a[1:]
}
fmt.Fprintf(w, "\n%s\nTotal groups %d\n", strings.Join(a, ", "), total)
return total, nil
}