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
220 lines (209 loc) · 6.1 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
package request
import (
"bufio"
"bytes"
"errors"
"fmt"
"os"
"path"
"strings"
"text/template"
"github.com/Defacto2/df2/pkg/groups/internal/acronym"
"github.com/Defacto2/df2/pkg/groups/internal/group"
"github.com/Defacto2/df2/pkg/logs"
"github.com/Defacto2/df2/pkg/str"
"github.com/spf13/viper"
)
// 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 prints an auto-complete list for HTML input elements.
func (r Flags) DataList(name string) error {
// <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(name, 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.
func (r Flags) HTML(name string) error {
// <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(name, 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(name string) (int, error) {
if !r.Counts {
return 0, nil
}
total, err := group.Count(name)
if err != nil {
return 0, fmt.Errorf("files %q: %w", name, err)
}
return total, nil
}
// Initialism returns the initialism of the named group.
func (r Flags) Initialism(name string) (string, error) {
if !r.Initialisms {
return "", nil
}
s, err := acronym.Get(name)
if err != nil {
return "", fmt.Errorf("initialism %q: %w", name, err)
}
return s, nil
}
func (r Flags) iterate(groups ...string) (*[]Result, error) {
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(r.Filter, i+1, total)
}
lastLetter, hr = group.UseHr(lastLetter, grp)
c, err := r.Files(grp)
if err != nil {
return nil, fmt.Errorf("iterate files %q: %w", grp, err)
}
init, err := r.Initialism(grp)
if err != nil {
return nil, fmt.Errorf("iterate initialism %q: %w", grp, err)
}
data[i] = Result{
ID: group.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 named file is empty, the results will be sent to stdout.
func (r Flags) Parse(name, tmpl string) error { //nolint:funlen
groups, total, err := group.List(r.Filter)
if err != nil {
return fmt.Errorf("parse list: %w", err)
}
if !str.Piped() {
if f := r.Filter; f == "" {
logs.Println(total, "matching (all) records found")
} else {
p := path.Join(viper.GetString("directory.html"), name)
logs.Printf("%d matching %s records found (%s)\n", total, f, p)
}
}
data, err := r.iterate(groups...)
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 name == "" {
var buf bytes.Buffer
wr := bufio.NewWriter(&buf)
if err = t.Execute(wr, &data); err != nil {
return fmt.Errorf("parse execute: %w", err)
}
if err := wr.Flush(); err != nil {
return fmt.Errorf("parse flush: %w", err)
}
logs.Println(buf.String())
return nil
}
switch group.Get(r.Filter) {
case group.BBS, group.FTP, group.Group, group.Magazine:
html := path.Join(viper.GetString("directory.html"), name)
f, err := os.Create(html)
if err != nil {
if _, _ = os.Stat(viper.GetString("directory.html")); errors.Is(err, os.ErrNotExist) {
return fmt.Errorf("parse create: parent directory is missing: %w", err)
}
return fmt.Errorf("parse create: %w", err)
}
defer f.Close()
// prepend html
s := "<div class=\"pagination-statistics\"><span class=\"label label-default\">"
s += fmt.Sprintf("%d %s sites</span></div><div class=\"columns-list\" id=\"organisation-drill-down\">",
total, r.Filter)
if _, err = f.WriteString(s); err != nil {
return fmt.Errorf("prepend writestring: %w", err)
}
// html template
if err = t.Execute(f, &data); err != nil {
return fmt.Errorf("parse execute: %w", err)
}
// append html
if _, err := f.WriteString("</div>\n"); err != nil {
return fmt.Errorf("append writestring: %w", err)
}
case group.None:
return fmt.Errorf("parse %q: %w", r.Filter, group.ErrFilter)
}
return nil
}
// Print list organisations or groups filtered by a name and summaries the results.
func Print(r Flags) (total int, err error) {
grp, total, err := group.List(r.Filter)
if err != nil {
return 0, fmt.Errorf("print groups: %w", err)
}
logs.Println(total, "matching", r.Filter, "records found")
a := make([]string, total)
for i, g := range grp {
if r.Progress {
str.Progress(r.Filter, i+1, total)
}
s := g
if r.Initialisms {
if in, err := acronym.Get(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 := group.Count(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:]
}
logs.Printf("\n%s\nTotal groups %d\n", strings.Join(a, ", "), total)
return total, nil
}