cmd/pkgsite: accept space-separated paths too
Treat any command-line argument as a path or comma-separated list of
paths.
For golang/go#47780
Change-Id: I5d9044d3ff0e14af99c4632f3c723039179a94a5
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/345270
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
Reviewed-by: Julie Qiu <julie@golang.org>
diff --git a/cmd/pkgsite/main.go b/cmd/pkgsite/main.go
index 0110008..95895fa 100644
--- a/cmd/pkgsite/main.go
+++ b/cmd/pkgsite/main.go
@@ -23,7 +23,9 @@
import (
"context"
"flag"
+ "fmt"
"net/http"
+ "os"
"strings"
"time"
@@ -47,15 +49,20 @@
)
func main() {
+ flag.Usage = func() {
+ fmt.Fprintf(flag.CommandLine.Output(), "usage: %s [flags] [PATHS ...]\n", os.Args[0])
+ fmt.Fprintf(flag.CommandLine.Output(), " where PATHS is a single path or a comma-separated list\n")
+ flag.PrintDefaults()
+ }
flag.Parse()
ctx := context.Background()
- paths := flag.Arg(0)
- if paths == "" {
- paths = "."
+ paths := collectPaths(flag.Args())
+ if len(paths) == 0 {
+ paths = []string{"."}
}
- server, err := newServer(ctx, strings.Split(paths, ","), *gopathMode)
+ server, err := newServer(ctx, paths, *gopathMode)
if err != nil {
log.Fatalf(ctx, "newServer: %v", err)
}
@@ -66,6 +73,14 @@
log.Fatal(ctx, http.ListenAndServe(*httpAddr, mw(router)))
}
+func collectPaths(args []string) []string {
+ var paths []string
+ for _, arg := range args {
+ paths = append(paths, strings.Split(arg, ",")...)
+ }
+ return paths
+}
+
func newServer(ctx context.Context, paths []string, gopathMode bool) (*frontend.Server, error) {
getters := buildGetters(ctx, paths, gopathMode)
lds := fetchdatasource.Options{
diff --git a/cmd/pkgsite/main_test.go b/cmd/pkgsite/main_test.go
index f0db81d..b749076 100644
--- a/cmd/pkgsite/main_test.go
+++ b/cmd/pkgsite/main_test.go
@@ -10,6 +10,8 @@
"net/http"
"net/http/httptest"
"testing"
+
+ "github.com/google/go-cmp/cmp"
)
func Test(t *testing.T) {
@@ -27,3 +29,11 @@
t.Errorf("%q: got status code = %d, want %d", "/testmod", w.Code, http.StatusOK)
}
}
+
+func TestCollectPaths(t *testing.T) {
+ got := collectPaths([]string{"a", "b,c2,d3", "e4", "f,g"})
+ want := []string{"a", "b", "c2", "d3", "e4", "f", "g"}
+ if !cmp.Equal(got, want) {
+ t.Errorf("got %v, want %v", got, want)
+ }
+}