diff --git a/cmd/internal/pkgsite-cli/command.go b/cmd/internal/pkgsite-cli/command.go
index 2955028..4e4b2e2 100644
--- a/cmd/internal/pkgsite-cli/command.go
+++ b/cmd/internal/pkgsite-cli/command.go
@@ -13,6 +13,7 @@
"runtime/debug"
"slices"
"strings"
+ "time"
)
// A command describes a subcommand of the tool.
@@ -161,12 +162,14 @@
jsonOut bool
limit int
server string
+ timeout time.Duration
}
func (f *commonFlags) register(fs *flag.FlagSet) {
fs.BoolVar(&f.jsonOut, "json", false, "output JSON")
fs.IntVar(&f.limit, "limit", 0, "max results (default: 25)")
fs.StringVar(&f.server, "server", defaultServer, "API server URL")
+ fs.DurationVar(&f.timeout, "timeout", 30*time.Second, "request timeout")
}
func (f *commonFlags) effectiveLimit() int {
diff --git a/cmd/internal/pkgsite-cli/main.go b/cmd/internal/pkgsite-cli/main.go
index 3b2854c..73b3006 100644
--- a/cmd/internal/pkgsite-cli/main.go
+++ b/cmd/internal/pkgsite-cli/main.go
@@ -15,7 +15,9 @@
package main
import (
+ "context"
"encoding/json"
+ "errors"
"flag"
"fmt"
"io"
@@ -114,10 +116,18 @@
if !ok {
aerr = &client.Error{Code: 1, Message: err.Error()}
}
+ if errors.Is(err, context.DeadlineExceeded) {
+ aerr.Message = "request timed out"
+ aerr.Fixes = []string{"use the -timeout flag to increase the timeout"}
+ }
writeJSON(stdout, stderr, aerr)
return
}
- fmt.Fprintln(stderr, err)
+ if errors.Is(err, context.DeadlineExceeded) {
+ fmt.Fprintln(stderr, "Error: request timed out; use the -timeout flag to increase it")
+ } else {
+ fmt.Fprintln(stderr, err)
+ }
}
func writeJSON(stdout, stderr io.Writer, v any) int {
diff --git a/cmd/internal/pkgsite-cli/module.go b/cmd/internal/pkgsite-cli/module.go
index e82da40..160d5c5 100644
--- a/cmd/internal/pkgsite-cli/module.go
+++ b/cmd/internal/pkgsite-cli/module.go
@@ -8,7 +8,6 @@
"context"
"flag"
"io"
- "time"
"golang.org/x/pkgsite/cmd/internal/pkgsite-cli/client"
"golang.org/x/sync/errgroup"
@@ -21,7 +20,7 @@
}
path, version := splitPathVersion(args[0])
- ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ ctx, cancel := context.WithTimeout(context.Background(), m.timeout)
defer cancel()
c, err := client.New(m.server)
diff --git a/cmd/internal/pkgsite-cli/package.go b/cmd/internal/pkgsite-cli/package.go
index f49dd91..d5a8b98 100644
--- a/cmd/internal/pkgsite-cli/package.go
+++ b/cmd/internal/pkgsite-cli/package.go
@@ -11,7 +11,6 @@
"io"
"os/exec"
"strings"
- "time"
"golang.org/x/pkgsite/cmd/internal/pkgsite-cli/client"
)
@@ -32,7 +31,7 @@
p.goos = goos
p.goarch = goarch
- ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ ctx, cancel := context.WithTimeout(context.Background(), p.timeout)
defer cancel()
c, err := client.New(p.server)
diff --git a/cmd/internal/pkgsite-cli/search.go b/cmd/internal/pkgsite-cli/search.go
index 56a022b..97b4281 100644
--- a/cmd/internal/pkgsite-cli/search.go
+++ b/cmd/internal/pkgsite-cli/search.go
@@ -9,7 +9,6 @@
"flag"
"io"
"strings"
- "time"
"golang.org/x/pkgsite/cmd/internal/pkgsite-cli/client"
)
@@ -21,7 +20,7 @@
}
query := strings.Join(args, " ")
- ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+ ctx, cancel := context.WithTimeout(context.Background(), s.timeout)
defer cancel()
c, err := client.New(s.server)