Skip to content

Commit

Permalink
Add search command, closes #2
Browse files Browse the repository at this point in the history
  • Loading branch information
bbkane committed Jul 9, 2022
1 parent 54d255f commit 20da2e2
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 0 deletions.
33 changes: 33 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,35 @@ func app() *warg.App {
),
)

searchCmd := command.New(

"Full text search SQLite database",
search,
command.Flag(
"--limit",
"Max number of results",
value.Int,
flag.Default("50"),
flag.Required(),
),
command.Flag(
"--sqlite-dsn",
"Sqlite DSN. Usually the file name.",
value.String,
flag.Default("starghaze.db"),
flag.Required(),
),
command.Flag(
"--term",
"Search for this term",
value.String,
flag.Alias("-t"),
flag.Required(),
),

// TODO: how many results? limit by date added?
)

app := warg.New(
"starghaze",
section.New(
Expand All @@ -187,6 +216,10 @@ func app() *warg.App {
"format",
formatCmd,
),
section.ExistingCommand(
"search",
searchCmd,
),
section.ExistingSection(
"gsheets",
gsheetsSection,
Expand Down
81 changes: 81 additions & 0 deletions search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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
}

0 comments on commit 20da2e2

Please sign in to comment.