-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.go
81 lines (70 loc) · 1.75 KB
/
search.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
package main
import (
"context"
"database/sql"
"fmt"
"os"
"strconv"
"go.bbkane.com/warg/command"
"go.bbkane.com/warg/help"
_ "modernc.org/sqlite"
)
type searchResult struct {
Link string
StarredAt string
StargazerCount int
Description string
}
func search(ctx command.Context) error {
dsn := ctx.Flags["--sqlite-dsn"].(string)
limit := ctx.Flags["--limit"].(int)
term := ctx.Flags["--term"].(string)
col, err := help.ConditionallyEnableColor(ctx.Flags, os.Stdout)
if err != nil {
return fmt.Errorf("error enabling color: %w", err)
}
db, err := sql.Open("sqlite", dsn)
if err != nil {
return fmt.Errorf("db open error: %s: %w", dsn, err)
}
query := `
SELECT
'https://github.com/' || NameWithOwner AS link,
StarredAt,
StargazerCount,
CASE
WHEN Description = '' THEN SUBSTR(Readme, 0, 50) || '...'
ELSE Description
END AS Description
FROM
Repo_fts
WHERE
Repo_fts MATCH ?
ORDER BY
RANK
LIMIT
?
`
rows, err := db.QueryContext(context.Background(), query, term, limit)
if err != nil {
return fmt.Errorf("error querying: %w", err)
}
defer rows.Close()
var s searchResult
for rows.Next() {
err := rows.Scan(&s.Link, &s.StarredAt, &s.StargazerCount, &s.Description)
if err != nil {
return fmt.Errorf("error scanning result: %w", err)
}
fmt.Println(col.Add(col.Bold, "Link") + ": " + s.Link)
fmt.Println(col.Add(col.Bold+col.FgGreenBright, "StarredAt") + ": " + s.StarredAt)
fmt.Println(col.Add(col.Bold+col.FgCyanBright, "StargazerCount") + ": " + strconv.Itoa(s.StargazerCount))
fmt.Println(col.Add(col.Bold+col.FgYellowBright, "Description") + ": " + s.Description)
fmt.Println()
}
err = rows.Err()
if err != nil {
return fmt.Errorf("error at end of scan: %w", err)
}
return nil
}