aboutsummaryrefslogtreecommitdiff
path: root/devtools/cmd/seeddb/main.go
diff options
context:
space:
mode:
authorJonathan Amsterdam <jba@google.com>2021-07-26 08:28:11 -0400
committerJonathan Amsterdam <jba@google.com>2021-07-26 14:50:49 +0000
commit894319f1c858c3eb690c829b73d071747ccf4a49 (patch)
tree6eb3d24dc1d9c5c44bc84ee4aefa44c6fcf647f9 /devtools/cmd/seeddb/main.go
parent0ba6f8e622ca6309070c1def637a006730e68d47 (diff)
downloadgo-x-pkgsite-894319f1c858c3eb690c829b73d071747ccf4a49.tar.xz
internal: add ReadFileLines
Factor out this commonly used function to the internal package. Also, update some comments in devtools/cmd/seeddb. Change-Id: I0276ce51d0dd1f4aafa2f6c562d1b83ce21a0d14 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/337330 Trust: Jonathan Amsterdam <jba@google.com> Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: kokoro <noreply+kokoro@google.com> Reviewed-by: Jamal Carvalho <jamal@golang.org> Reviewed-by: Julie Qiu <julie@golang.org>
Diffstat (limited to 'devtools/cmd/seeddb/main.go')
-rw-r--r--devtools/cmd/seeddb/main.go41
1 files changed, 7 insertions, 34 deletions
diff --git a/devtools/cmd/seeddb/main.go b/devtools/cmd/seeddb/main.go
index 9f63939a..419573ee 100644
--- a/devtools/cmd/seeddb/main.go
+++ b/devtools/cmd/seeddb/main.go
@@ -2,23 +2,21 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-// The seeddb command is used to populates a database with an initial set of
-// modules.
+// The seeddb command populates a database with an initial set of modules.
package main
import (
- "bufio"
"context"
"flag"
"fmt"
"net/http"
- "os"
"sort"
"strings"
"sync"
"time"
_ "github.com/jackc/pgx/v4/stdlib" // for pgx driver
+ "golang.org/x/pkgsite/internal"
"golang.org/x/pkgsite/internal/config"
"golang.org/x/pkgsite/internal/config/dynconfig"
"golang.org/x/pkgsite/internal/database"
@@ -34,7 +32,7 @@ import (
)
var (
- seedfile = flag.String("seed", "devtools/cmd/seeddb/seed.txt", "filename containing modules for seeding the database; relative to devtools/cmd/seeddb")
+ seedfile = flag.String("seed", "devtools/cmd/seeddb/seed.txt", "filename containing modules for seeding the database")
)
func main() {
@@ -52,7 +50,6 @@ func main() {
}
ctx = experiment.NewContext(ctx, exps...)
- // Wrap the postgres driver with our own wrapper, which adds OpenCensus instrumentation.
ddb, err := database.Open("pgx", cfg.DBConnInfo(), "seeddb")
if err != nil {
log.Fatalf(ctx, "database.Open for host %s failed with %v", cfg.DBHost, err)
@@ -113,7 +110,7 @@ func run(ctx context.Context, db *postgres.DB, proxyURL string) error {
for _, v := range vers {
v := v
g.Go(func() error {
- // Log the duration of this fetch request.
+ // Record the duration of this fetch request.
start := time.Now()
defer func() {
r.add(m.path, v, start)
@@ -162,8 +159,8 @@ func fetchFunc(ctx context.Context, f *worker.Fetcher, m, v string) (err error)
}
type results struct {
- paths map[string]time.Duration
mu sync.Mutex
+ paths map[string]time.Duration
}
func (r *results) add(modPath, version string, start time.Time) {
@@ -182,12 +179,11 @@ type module struct {
}
// readSeedFile reads a file of module versions that we want to fetch for
-// seeding the database. Format of the file:
-// each line is
+// seeding the database. Each line of the file should be of the form:
// module@version
func readSeedFile(ctx context.Context, seedfile string) (_ []*module, err error) {
defer derrors.Wrap(&err, "readSeedFile %q", seedfile)
- lines, err := readFileLines(seedfile)
+ lines, err := internal.ReadFileLines(seedfile)
if err != nil {
return nil, err
}
@@ -204,29 +200,6 @@ func readSeedFile(ctx context.Context, seedfile string) (_ []*module, err error)
return modules, nil
}
-// readFileLines reads filename and returns its lines, trimmed of whitespace.
-// Blank lines and lines whose first non-blank character is '#' are omitted.
-func readFileLines(filename string) ([]string, error) {
- var lines []string
- f, err := os.Open(filename)
- if err != nil {
- return nil, err
- }
- defer f.Close()
- scan := bufio.NewScanner(f)
- for scan.Scan() {
- line := strings.TrimSpace(scan.Text())
- if line == "" || line[0] == '#' {
- continue
- }
- lines = append(lines, line)
- }
- if err := scan.Err(); err != nil {
- return nil, err
- }
- return lines, nil
-}
-
func fetchExperiments(ctx context.Context, cfg *config.Config) ([]string, error) {
if cfg.DynamicConfigLocation == "" {
return nil, nil