From 32fcd7b23ea54233a947fffc53773a5a2520b8dc Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Tue, 17 May 2022 10:11:16 -0700 Subject: [PATCH 01/17] Back to development (#57) --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50f8c2c..f74dc58 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] +- No changes yet. + +[Unreleased]: https://github.com/uber-go/sally/compare/v1.2.0...HEAD + ## [1.2.0] - 2022-05-17 ### Added - Packages now support specifying branches for target repositories with the From c0f42eb71697afa1a100524d4a1787582ae3ab6c Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Sun, 18 Dec 2022 22:54:57 -0800 Subject: [PATCH 02/17] ci: Use Go 1.19 and update GH actions (#58) This updates sally to build and test against Go 1.19. Additionally, this upgrades the GitHub Action versions for checkout and setup-go. One of the features in setup-go v3 is that caching is built-in and opted-into with `cache: true`. Non-CI changes: gofmt, drop ioutil --- .github/workflows/go.yml | 16 +++++----------- config.go | 4 ++-- templates/templates.go | 2 ++ 3 files changed, 9 insertions(+), 13 deletions(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index a038ee5..a8c53f6 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -13,21 +13,15 @@ jobs: runs-on: ubuntu-latest steps: - - name: Setup Go - uses: actions/setup-go@v2 - with: - go-version: 1.18.x - name: Checkout code - uses: actions/checkout@v2 + uses: actions/checkout@v3 - - name: Load cached dependencies - uses: actions/cache@v1 + - name: Setup Go + uses: actions/setup-go@v3 with: - path: ~/go/pkg/mod - key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} - restore-keys: | - ${{ runner.os }}-go- + go-version: 1.19.x + cache: true - name: Lint run: make lint diff --git a/config.go b/config.go index e322da3..2588f39 100644 --- a/config.go +++ b/config.go @@ -2,7 +2,7 @@ package main import ( "fmt" - "io/ioutil" + "os" "sort" "strings" @@ -55,7 +55,7 @@ func ensureAlphabetical(data []byte) bool { func Parse(path string) (*Config, error) { var c Config - data, err := ioutil.ReadFile(path) + data, err := os.ReadFile(path) if err != nil { return nil, err } diff --git a/templates/templates.go b/templates/templates.go index 644eb3b..7f90908 100644 --- a/templates/templates.go +++ b/templates/templates.go @@ -3,9 +3,11 @@ package templates import _ "embed" // needed for go:embed // Index holds the contents of the index.html template. +// //go:embed index.html var Index string // Package holds the contents of the package.html template. +// //go:embed package.html var Package string From 442df014f2dd5ca897b80fcad889726a230ccaf2 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Sun, 18 Dec 2022 22:55:21 -0800 Subject: [PATCH 03/17] Set up dependabot (#59) sally is a binary, not a library so it's okay to keep it on the latest dependencies. This sets up dependabot updates for Go modules used by sally. --- .github/dependabot.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..3938344 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,6 @@ +version: 2 +updates: + - package-ecosystem: "gomod" + directory: "/" + schedule: + interval: "daily" From 47a726398b055cf77df38addf90a1a388a53b2c7 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Sun, 18 Dec 2022 23:01:01 -0800 Subject: [PATCH 04/17] Delete unused Docker infrastructure (#60) This was all added back when these tests were running on Travis CI and attempted to create an isolated build of sally. This is all no longer necessary because GitHub Actions and Go modules provide enough isolation. --- .dockerignore | 1 - Dockerfile | 12 ------------ Dockerfile.scratch | 6 ------ Makefile | 27 --------------------------- 4 files changed, 46 deletions(-) delete mode 100644 .dockerignore delete mode 100644 Dockerfile delete mode 100644 Dockerfile.scratch diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index 22d0d82..0000000 --- a/.dockerignore +++ /dev/null @@ -1 +0,0 @@ -vendor diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 1198370..0000000 --- a/Dockerfile +++ /dev/null @@ -1,12 +0,0 @@ -FROM golang:1.18 - -EXPOSE 8080 -RUN \ - curl -fsSLO https://get.docker.com/builds/Linux/x86_64/docker-latest.tgz && \ - tar --strip-components=1 -xvzf docker-latest.tgz -C /usr/local/bin -ENV GO111MODULE=on -RUN mkdir -p /go/src/go.uber.org/sally -WORKDIR /go/src/go.uber.org/sally -ADD . /go/src/go.uber.org/sally/ -RUN go mod vendor -CMD ["make", "run"] diff --git a/Dockerfile.scratch b/Dockerfile.scratch deleted file mode 100644 index f78f304..0000000 --- a/Dockerfile.scratch +++ /dev/null @@ -1,6 +0,0 @@ -FROM scratch - -EXPOSE 8080 -ADD sally.yaml / -ADD _tmp/sally / -ENTRYPOINT ["/sally"] diff --git a/Makefile b/Makefile index e711e05..b938911 100644 --- a/Makefile +++ b/Makefile @@ -40,33 +40,6 @@ cover: clean: rm -rf _tmp -.PHONY: docker-build-dev -docker-build-dev: - docker build -t uber/sally-dev . - -.PHONY: docker-test -docker-test: docker-build-dev - docker run uber/sally-dev make test - -.PHONY: docker-build-internal -docker-build-internal: - rm -rf _tmp - mkdir -p _tmp - CGO_ENABLED=0 go build -a -o _tmp/sally . - docker build -t uber/sally -f Dockerfile.scratch . - -.PHONY: docker-build -docker-build: docker-build-dev - docker run -v /var/run/docker.sock:/var/run/docker.sock uber/sally-dev make docker-build-internal - -.PHONY: docker-launch-dev -docker-launch-dev: docker-build-dev - docker run -p 8080:8080 uber/sally-dev - -.PHONY: docker-launch -docker-launch: docker-build - docker run -p 8080:8080 uber/sally - .PHONY: install run: install sally From c70dd0aacb63464520aa3fcf01e8fb8b9c9576c7 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Sun, 18 Dec 2022 23:01:28 -0800 Subject: [PATCH 05/17] README: Document configuration further (#61) Adds more details on how to configure sally and what the required and optional fields are. Also updates the installation instructions since you can't `go get` the binary anymore. --- README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 4cafea2..ae1888d 100644 --- a/README.md +++ b/README.md @@ -1,28 +1,69 @@ # sally -A tiny HTTP server for supporting custom Golang import paths +sally is a small HTTP service you can host +to serve vanity import paths for Go modules. ## Installation -`go get go.uber.org/sally` +```bash +go install go.uber.org/sally@latest +``` ## Usage Create a YAML file with the following structure: ```yaml -# This optional section configures godoc documentation linking. +# sally.yaml + +# Configures documentation linking. +# Optional. godoc: - # Instance of godoc server used for documentation links. Defaults to pkg.go.dev. + # Host for the Go documentation server. + # Defaults to pkg.go.dev. host: pkg.go.dev -url: google.golang.org +# Base URL for your package site. +# If you want your modules available under "example.com", +# specify example.com here. +# This field is required. +url: go.uber.org + +# Collection of packages under example.com +# and their Git repositories. packages: - grpc: - repo: github.com/grpc/grpc-go + + # The key is the name of the package following the base URL. + # For example, if you want to make a package available at + # "example.com/foo", you'd specify "foo" here. + zap: + # Path to the Git repository. + # + # This field is required. + repo: github.com/uber-go/zap + + # Branch of the Git repository that you're linking to. + # + # Defaults to "master". + branch: master + + # Alternative base URL instead of the value configured at the top-level. + # This is useful if the same sally instance is + # hosted behind multiple base URLs. + # + # Defaults to the value of the top-level url field. + url: example.com +``` + +Run sally like so: + +```shell +$ sally ``` -Then run Sally to start the HTTP server: +This will read from sally.yaml and serve on port 8080. +To use a different port and/or configuration file, +use the `-yml` and `-port` flags. ``` $ sally -yml site.yaml -port 5000 From e3604e558f6bd4dccc96b9bd071223fd8506888f Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Sun, 18 Dec 2022 23:01:59 -0800 Subject: [PATCH 06/17] Makefile: Simplify (#62) This simplifies the Makefile significantly, borrowing patterns we've used in other projects. Namely: - Set GOBIN to a bin subdirectory so that we can `go install` dependencies into it. - Use a shared TEST_FLAGS for `make test` and `make cover`. Without this, we're not running with data race detection in CI. - Build lint step out of separate golint and staticcheck steps. In the future, a gofmt step may also be added. - Move tools dependencies into an unpublished subpackage. Note: I didn't mess with the 'clean' and 'run' targets at the bottom of the file even though they're not necessary to avoid a merge conflict with #60. --- .gitignore | 5 ++-- Makefile | 49 ++++++++++++++++++++------------------ go.mod | 7 ------ go.sum | 25 ------------------- tools/doc.go | 3 +++ tools/go.mod | 16 +++++++++++++ tools/go.sum | 25 +++++++++++++++++++ tools.go => tools/tools.go | 2 +- 8 files changed, 74 insertions(+), 58 deletions(-) create mode 100644 tools/doc.go create mode 100644 tools/go.mod create mode 100644 tools/go.sum rename tools.go => tools/tools.go (89%) diff --git a/.gitignore b/.gitignore index b2dab6a..4119fb9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ -vendor -_tmp +/bin +/cover.out +/cover.html diff --git a/Makefile b/Makefile index b938911..4b9b0cd 100644 --- a/Makefile +++ b/Makefile @@ -1,39 +1,42 @@ -GOLINT = go run golang.org/x/lint/golint -STATICCHECK = go run honnef.co/go/tools/cmd/staticcheck +export GOBIN = $(shell pwd)/bin +export PATH := $(GOBIN):$(PATH) -.PHONY: all -all: test +GOLINT = bin/golint +STATICCHECK = bin/staticcheck -.PHONY: build -build: - go build +TEST_FLAGS ?= -race -.PHONY: install -install: - go install . +.PHONY: all +all: lint install test .PHONY: lint -lint: - $(GOLINT) ./... - -.PHONY: vet -vet: - go vet ./... +lint: golint staticcheck .PHONY: staticcheck -staticcheck: - $(STATICCHECK) -tests=false ./... +staticcheck: $(STATICCHECK) + $(STATICCHECK) ./... -.PHONY: pretest -pretest: lint vet staticcheck +$(STATICCHECK): tools/go.mod + cd tools && go install honnef.co/go/tools/cmd/staticcheck + +.PHONY: golint +golint: $(GOLINT) + $(GOLINT) ./... + +$(GOLINT): tools/go.mod + cd tools && go install golang.org/x/lint/golint + +.PHONY: install +install: + go install . .PHONY: test -test: pretest - go test -race ./... +test: + go test $(TEST_FLAGS) ./... .PHONY: cover cover: - go test -coverprofile=cover.out -covermode=atomic -coverpkg=./... ./... + go test $(TEST_FLAGS) -coverprofile=cover.out -covermode=atomic -coverpkg=./... ./... go tool cover -html=cover.out -o cover.html .PHONY: clean diff --git a/go.mod b/go.mod index 9f3412e..dc9b70a 100644 --- a/go.mod +++ b/go.mod @@ -6,20 +6,13 @@ require ( github.com/julienschmidt/httprouter v1.3.0 github.com/stretchr/testify v1.2.2 github.com/yosssi/gohtml v0.0.0-20180130040904-97fbf36f4aa8 - golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 gopkg.in/yaml.v2 v2.4.0 - honnef.co/go/tools v0.3.2 ) require ( - github.com/BurntSushi/toml v1.1.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/kr/pretty v0.1.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/exp/typeparams v0.0.0-20220516143420-24438e51023a // indirect - golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect - golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect - golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect ) diff --git a/go.sum b/go.sum index e1aff25..543b7a8 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,3 @@ -github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I= -github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= @@ -15,33 +13,10 @@ github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1 github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/yosssi/gohtml v0.0.0-20180130040904-97fbf36f4aa8 h1:OlIHDBRrlQk8fHa662oG2gzOO/ixBRU9sLht/M9kzK0= github.com/yosssi/gohtml v0.0.0-20180130040904-97fbf36f4aa8/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE= -golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= -golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/exp/typeparams v0.0.0-20220516143420-24438e51023a h1:3ZzsnGxuJfU/kdQ051ZSHB9yWzkzWNLMyQNYDsXCX7o= -golang.org/x/exp/typeparams v0.0.0-20220516143420-24438e51023a/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= -golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug= -golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= -golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= -golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= -golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= -golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= -golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a h1:N2T1jUrTQE9Re6TFF5PhvEHXHCguynGhKjWVsIUt5cY= -golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= -golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= -golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f h1:OKYpQQVE3DKSc3r3zHVzq46vq5YH7x8xpR3/k9ixmUg= -golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4= -golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= -honnef.co/go/tools v0.3.2 h1:ytYb4rOqyp1TSa2EPvNVwtPQJctSELKaMyLfqNP4+34= -honnef.co/go/tools v0.3.2/go.mod h1:jzwdWgg7Jdq75wlfblQxO4neNaFFSvgc1tD5Wv8U0Yw= diff --git a/tools/doc.go b/tools/doc.go new file mode 100644 index 0000000..c94c1a8 --- /dev/null +++ b/tools/doc.go @@ -0,0 +1,3 @@ +// Package tools specifies the development-time +// dependencies of sally. +package tools diff --git a/tools/go.mod b/tools/go.mod new file mode 100644 index 0000000..c722bd9 --- /dev/null +++ b/tools/go.mod @@ -0,0 +1,16 @@ +module go.uber.org/sally/tools + +require ( + golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 + honnef.co/go/tools v0.3.3 +) + +require ( + github.com/BurntSushi/toml v1.1.0 // indirect + golang.org/x/exp/typeparams v0.0.0-20220516143420-24438e51023a // indirect + golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect + golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a // indirect + golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f // indirect +) + +go 1.19 diff --git a/tools/go.sum b/tools/go.sum new file mode 100644 index 0000000..dfc0c2f --- /dev/null +++ b/tools/go.sum @@ -0,0 +1,25 @@ +github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I= +github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/exp/typeparams v0.0.0-20220516143420-24438e51023a h1:3ZzsnGxuJfU/kdQ051ZSHB9yWzkzWNLMyQNYDsXCX7o= +golang.org/x/exp/typeparams v0.0.0-20220516143420-24438e51023a/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk= +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 h1:VLliZ0d+/avPrXXH+OakdXhpJuEoBZuwh1m2j7U6Iug= +golang.org/x/lint v0.0.0-20210508222113-6edffad5e616/go.mod h1:3xt1FjdF8hUf6vQPIChWIBhFzV8gjjsPE/fR3IyQdNY= +golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= +golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a h1:N2T1jUrTQE9Re6TFF5PhvEHXHCguynGhKjWVsIUt5cY= +golang.org/x/sys v0.0.0-20220513210249-45d2b4557a2a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20200130002326-2f3ba24bd6e7/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28= +golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f h1:OKYpQQVE3DKSc3r3zHVzq46vq5YH7x8xpR3/k9ixmUg= +golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4= +golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +honnef.co/go/tools v0.3.3 h1:oDx7VAwstgpYpb3wv0oxiZlxY+foCpRAwY7Vk6XpAgA= +honnef.co/go/tools v0.3.3/go.mod h1:jzwdWgg7Jdq75wlfblQxO4neNaFFSvgc1tD5Wv8U0Yw= diff --git a/tools.go b/tools/tools.go similarity index 89% rename from tools.go rename to tools/tools.go index 1b4f8ea..5cfae35 100644 --- a/tools.go +++ b/tools/tools.go @@ -1,7 +1,7 @@ //go:build tools // +build tools -package main +package tools import ( _ "golang.org/x/lint/golint" From 48c0b5f1653077bcb529c22aeaa95d4b01a582bf Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Mon, 19 Dec 2022 06:50:12 -0800 Subject: [PATCH 07/17] Fix lint error issues (#65) Since upgrading to Go 1.19 we are seeing linter error due to usage of the deprecated io/ioutil package. This removes the usage of io/ioutil package. --- utils_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/utils_test.go b/utils_test.go index d5a4f89..1388475 100644 --- a/utils_test.go +++ b/utils_test.go @@ -1,7 +1,6 @@ package main import ( - "io/ioutil" "net/http" "net/http/httptest" "os" @@ -14,7 +13,7 @@ import ( // TempFile persists contents and returns the path and a clean func func TempFile(t *testing.T, contents string) (path string, clean func()) { content := []byte(contents) - tmpfile, err := ioutil.TempFile("", "sally-tmp") + tmpfile, err := os.CreateTemp("", "sally-tmp") if err != nil { t.Fatal("Unable to create tmpfile", err) } From bdce05d68305442e0214f80b08faf20dd12c0e25 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Wed, 21 Dec 2022 10:52:31 -0800 Subject: [PATCH 08/17] Bump github.com/stretchr/testify from 1.2.2 to 1.8.1 (#63) Bumps [github.com/stretchr/testify](https://github.com/stretchr/testify) from 1.2.2 to 1.8.1. - [Release notes](https://github.com/stretchr/testify/releases) - [Commits](https://github.com/stretchr/testify/compare/v1.2.2...v1.8.1) --- updated-dependencies: - dependency-name: github.com/stretchr/testify dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- go.mod | 3 ++- go.sum | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/go.mod b/go.mod index dc9b70a..96add78 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/julienschmidt/httprouter v1.3.0 - github.com/stretchr/testify v1.2.2 + github.com/stretchr/testify v1.8.1 github.com/yosssi/gohtml v0.0.0-20180130040904-97fbf36f4aa8 gopkg.in/yaml.v2 v2.4.0 ) @@ -15,4 +15,5 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 543b7a8..f228df5 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,4 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= @@ -9,8 +10,13 @@ github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w= -github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/yosssi/gohtml v0.0.0-20180130040904-97fbf36f4aa8 h1:OlIHDBRrlQk8fHa662oG2gzOO/ixBRU9sLht/M9kzK0= github.com/yosssi/gohtml v0.0.0-20180130040904-97fbf36f4aa8/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE= golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= @@ -20,3 +26,6 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33 gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 24b0c32c0a0aeabc2131361f9bacaeaec8988ada Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Mon, 23 Jan 2023 09:25:18 -0800 Subject: [PATCH 09/17] config: Don't require packages to be alphabetical (#66) The configuration parser requries that entries in the 'packages' section are in alphabetical order. It will fail parsing if that's not the case, even if the configuration is otherwise valid. This seems like an unnecessary artificial limitation. Enforcing such a convention should be the user's choice. This change deletes this limitation. --- config.go | 35 ++++------------------------------- config_test.go | 19 ------------------- 2 files changed, 4 insertions(+), 50 deletions(-) diff --git a/config.go b/config.go index 2588f39..d562154 100644 --- a/config.go +++ b/config.go @@ -1,16 +1,16 @@ package main import ( - "fmt" "os" - "sort" "strings" yaml "gopkg.in/yaml.v2" ) -const _defaultGodocServer = "pkg.go.dev" -const _defaultBranch = "master" +const ( + _defaultGodocServer = "pkg.go.dev" + _defaultBranch = "master" +) // Config represents the structure of the yaml file type Config struct { @@ -28,29 +28,6 @@ type Package struct { URL string `yaml:"url"` } -// ensureAlphabetical checks that the packages are listed alphabetically in the configuration. -func ensureAlphabetical(data []byte) bool { - // A yaml.MapSlice perservers ordering of keys: https://pkg.go.dev/gopkg.in/yaml.v2#MapSlice - var c struct { - Packages yaml.MapSlice `yaml:"packages"` - } - - if err := yaml.Unmarshal(data, &c); err != nil { - return false - } - - packageNames := make([]string, 0, len(c.Packages)) - for _, v := range c.Packages { - name, ok := v.Key.(string) - if !ok { - return false - } - packageNames = append(packageNames, name) - } - - return sort.StringsAreSorted(packageNames) -} - // Parse takes a path to a yaml file and produces a parsed Config func Parse(path string) (*Config, error) { var c Config @@ -64,10 +41,6 @@ func Parse(path string) (*Config, error) { return nil, err } - if !ensureAlphabetical(data) { - return nil, fmt.Errorf("packages in %s must be alphabetically ordered", path) - } - if c.Godoc.Host == "" { c.Godoc.Host = _defaultGodocServer } else { diff --git a/config_test.go b/config_test.go index eb6c463..e4146b1 100644 --- a/config_test.go +++ b/config_test.go @@ -106,22 +106,3 @@ packages: }) } } - -func TestNotAlphabetical(t *testing.T) { - path, clean := TempFile(t, ` - -url: google.golang.org -packages: - grpc: - repo: github.com/grpc/grpc-go - atomic: - repo: github.com/uber-go/atomic - -`) - defer clean() - - _, err := Parse(path) - if assert.Error(t, err, "YAML configuration is not listed alphabetically") { - assert.Contains(t, err.Error(), "must be alphabetically ordered") - } -} From cb4081162b75508710f625b5b8dac0c48c813355 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Mon, 23 Jan 2023 09:41:20 -0800 Subject: [PATCH 10/17] template: Use a more fluid layout (#67) Instead of using a table, take advantage of the grid layout. We still print a table of sorts, but it's more fluid in appearance based on width of the screen. On narrower screens, we'll show a listing where each item has a description label next to it rather than at the top. --- CHANGELOG.md | 4 ++- templates/index.html | 68 +++++++++++++++++++++++++------------------- 2 files changed, 42 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f74dc58..31295a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] -- No changes yet. +### Changed +- Use a fluid layout for the index page. + This renders better on narrow screens. [Unreleased]: https://github.com/uber-go/sally/compare/v1.2.0...HEAD diff --git a/templates/index.html b/templates/index.html index 317d403..3fc210c 100644 --- a/templates/index.html +++ b/templates/index.html @@ -3,38 +3,48 @@ +
-
- - - - - - - - - - {{ range $key, $value := .Packages }} - {{ $importPath := printf "%v/%v" $.URL $key }} - {{ if ne $value.URL "" }} - {{ $importPath = printf "%v/%v" $value.URL $key }} - {{ end }} - - - - - - {{ end }} - -
PackageSourceDocumentation
{{ $importPath }} - {{ $value.Repo }} - - - Go Reference - -
+
+
Package
+
Source
+
Documentation
+ {{ range $key, $value := .Packages }} + {{ $importPath := printf "%v/%v" $.URL $key }} + {{ if ne $value.URL "" }} + {{ $importPath = printf "%v/%v" $value.URL $key }} + {{ end }} +
+
+
+ Package: + {{ $importPath }} +
+
+ Source: + {{ $value.Repo }} +
+
+ + Go Reference + +
+
+ {{ end }}
From 9a95dcbf6ef077808a8874beea9d8a2754e60051 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Mon, 23 Jan 2023 09:53:51 -0800 Subject: [PATCH 11/17] Add support for package descriptions (#68) * template: Use a more fluid layout Instead of using a table, take advantage of the grid layout. We still print a table of sorts, but it's more fluid in appearance based on width of the screen. On narrower screens, we'll show a listing where each item has a description label next to it rather than at the top. * Add support for package descrpitions Packages may now optionally specify a description. If specified, this is printed below the package information, indented one column to make it stand out. Co-authored-by: Sung Yoon Whang --- CHANGELOG.md | 3 ++- README.md | 3 +++ config.go | 2 ++ handler_test.go | 2 ++ sally.yaml | 1 + templates/index.html | 11 +++++++++++ 6 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31295a3..3f92d7f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] -### Changed +### Added +- Add an optional `description` field to packages. - Use a fluid layout for the index page. This renders better on narrow screens. diff --git a/README.md b/README.md index ae1888d..92a8da1 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,9 @@ packages: # Defaults to "master". branch: master + # Optional description of the package. + description: A fast, structured-logging library. + # Alternative base URL instead of the value configured at the top-level. # This is useful if the same sally instance is # hosted behind multiple base URLs. diff --git a/config.go b/config.go index d562154..57b5dcd 100644 --- a/config.go +++ b/config.go @@ -26,6 +26,8 @@ type Package struct { Repo string `yaml:"repo"` Branch string `yaml:"branch"` URL string `yaml:"url"` + + Desc string `yaml:"description"` // plain text only } // Parse takes a path to a yaml file and produces a parsed Config diff --git a/handler_test.go b/handler_test.go index 96a10dd..e300d19 100644 --- a/handler_test.go +++ b/handler_test.go @@ -17,6 +17,7 @@ packages: zap: url: go.uberalt.org repo: github.com/uber-go/zap + description: A fast, structured logging library. ` @@ -27,6 +28,7 @@ func TestIndex(t *testing.T) { body := rr.Body.String() assert.Contains(t, body, "github.com/thriftrw/thriftrw-go") assert.Contains(t, body, "github.com/yarpc/yarpc-go") + assert.Contains(t, body, "A fast, structured logging library.") } func TestPackageShouldExist(t *testing.T) { diff --git a/sally.yaml b/sally.yaml index ee8ff87..c9a3af8 100644 --- a/sally.yaml +++ b/sally.yaml @@ -3,5 +3,6 @@ url: go.uber.org packages: thriftrw: repo: github.com/thriftrw/thriftrw-go + description: A customizable implementation of Thrift. yarpc: repo: github.com/yarpc/yarpc-go diff --git a/templates/index.html b/templates/index.html index 3fc210c..d193263 100644 --- a/templates/index.html +++ b/templates/index.html @@ -7,6 +7,7 @@ .separator { margin: 0.25em 0; } + .description { color: #666; } /* On narrow screens, switch to inline headers. */ .table-header { display: none; } @@ -44,6 +45,16 @@
+ {{ with .Desc }} +
+
+ +
+
+ {{ . }} +
+
+ {{ end }} {{ end }} From 86218534c831b3fc6fdfb98f5ad334262d7060ae Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Mon, 23 Jan 2023 10:27:10 -0800 Subject: [PATCH 12/17] Drop gohtml dependency (#70) This dependency is used to format and compare HTML. An additional dependency isn't needed; we can use the existing (previously transitive) x/net package to reformat and compare the HTML. --- go.mod | 3 +-- go.sum | 6 ++---- utils_test.go | 16 ++++++++++++++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index 96add78..58f7710 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.18 require ( github.com/julienschmidt/httprouter v1.3.0 github.com/stretchr/testify v1.8.1 - github.com/yosssi/gohtml v0.0.0-20180130040904-97fbf36f4aa8 + golang.org/x/net v0.5.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -13,7 +13,6 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/kr/pretty v0.1.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index f228df5..8261753 100644 --- a/go.sum +++ b/go.sum @@ -17,10 +17,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/yosssi/gohtml v0.0.0-20180130040904-97fbf36f4aa8 h1:OlIHDBRrlQk8fHa662oG2gzOO/ixBRU9sLht/M9kzK0= -github.com/yosssi/gohtml v0.0.0-20180130040904-97fbf36f4aa8/go.mod h1:+ccdNT0xMY1dtc5XBxumbYfOUhmduiGudqaDgD2rVRE= -golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f h1:OfiFi4JbukWwe3lzw+xunroH1mnC1e2Gy5cxNJApiSY= -golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.5.0 h1:GyT4nK/YDHSqa1c4753ouYCDajOYKTja9Xb/OHtgvSw= +golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= diff --git a/utils_test.go b/utils_test.go index 1388475..5eaf0ba 100644 --- a/utils_test.go +++ b/utils_test.go @@ -1,13 +1,16 @@ package main import ( + "bytes" "net/http" "net/http/httptest" "os" + "strings" "testing" "github.com/stretchr/testify/assert" - "github.com/yosssi/gohtml" + "github.com/stretchr/testify/require" + "golang.org/x/net/html" ) // TempFile persists contents and returns the path and a clean func @@ -61,5 +64,14 @@ func CallAndRecord(t *testing.T, config string, uri string) *httptest.ResponseRe // AssertResponse normalizes and asserts the body from rr against want func AssertResponse(t *testing.T, rr *httptest.ResponseRecorder, code int, want string) { assert.Equal(t, rr.Code, code) - assert.Equal(t, gohtml.Format(want), gohtml.Format(rr.Body.String())) + assert.Equal(t, reformatHTML(t, want), reformatHTML(t, rr.Body.String())) +} + +func reformatHTML(t *testing.T, s string) string { + n, err := html.Parse(strings.NewReader(s)) + require.NoError(t, err) + + var buff bytes.Buffer + require.NoError(t, html.Render(&buff, n)) + return buff.String() } From 5f327a458f6488d053815621cd1a5154f1ce76c0 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Mon, 23 Jan 2023 10:28:04 -0800 Subject: [PATCH 13/17] Drop httprouter dependency (#71) This drops the third-party HTTP router dependency. This dependency wasn't strictly necessary since our routing needs are quite basic: - `/$name` and `/$name/*` for all registered packages - `/` for root This is easily accomplished with `http.ServeMux`: - register `/$name` and `/$name/`. The latter will receive all subpackage requests. - register `/` and reject anything that isn't for exactly `/`. --- go.mod | 1 - go.sum | 2 -- handler.go | 41 +++++++++++++++++++++++++++++------------ handler_test.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 75 insertions(+), 15 deletions(-) diff --git a/go.mod b/go.mod index 58f7710..f118570 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,6 @@ module go.uber.org/sally go 1.18 require ( - github.com/julienschmidt/httprouter v1.3.0 github.com/stretchr/testify v1.8.1 golang.org/x/net v0.5.0 gopkg.in/yaml.v2 v2.4.0 diff --git a/go.sum b/go.sum index 8261753..e8b209a 100644 --- a/go.sum +++ b/go.sum @@ -1,8 +1,6 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/julienschmidt/httprouter v1.3.0 h1:U0609e9tgbseu3rBINet9P48AI/D3oJs4dN7jwJOQ1U= -github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= diff --git a/handler.go b/handler.go index 0490a83..bbefd30 100644 --- a/handler.go +++ b/handler.go @@ -4,8 +4,8 @@ import ( "fmt" "html/template" "net/http" + "strings" - "github.com/julienschmidt/httprouter" "go.uber.org/sally/templates" ) @@ -18,29 +18,36 @@ var ( // CreateHandler creates a Sally http.Handler func CreateHandler(config *Config) http.Handler { - router := httprouter.New() - router.RedirectTrailingSlash = false - - router.GET("/", indexHandler{config: config}.Handle) + mux := http.NewServeMux() + mux.Handle("/", &indexHandler{config: config}) for name, pkg := range config.Packages { handle := packageHandler{ pkgName: name, pkg: pkg, config: config, - }.Handle - router.GET(fmt.Sprintf("/%s", name), handle) - router.GET(fmt.Sprintf("/%s/*path", name), handle) + } + // Double-register so that "/foo" + // does not redirect to "/foo/" with a 300. + mux.Handle("/"+name, &handle) + mux.Handle("/"+name+"/", &handle) } - return router + return mux } type indexHandler struct { config *Config } -func (h indexHandler) Handle(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { +func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + // Index handler only supports '/'. + // ServeMux will call us for any '/foo' that is not a known package. + if r.Method != http.MethodGet || r.URL.Path != "/" { + http.NotFound(w, r) + return + } + if err := indexTemplate.Execute(w, h.config); err != nil { http.Error(w, err.Error(), 500) } @@ -52,7 +59,17 @@ type packageHandler struct { config *Config } -func (h packageHandler) Handle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +func (h *packageHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.NotFound(w, r) + return + } + + // Extract the relative path to subpackages, if any. + // "/foo/bar" => "/bar" + // "/foo" => "" + relPath := strings.TrimPrefix(r.URL.Path, "/"+h.pkgName) + baseURL := h.config.URL if h.pkg.URL != "" { baseURL = h.pkg.URL @@ -67,7 +84,7 @@ func (h packageHandler) Handle(w http.ResponseWriter, r *http.Request, ps httpro Repo: h.pkg.Repo, Branch: h.pkg.Branch, CanonicalURL: canonicalURL, - GodocURL: fmt.Sprintf("https://%s/%s%s", h.config.Godoc.Host, canonicalURL, ps.ByName("path")), + GodocURL: fmt.Sprintf("https://%s/%s%s", h.config.Godoc.Host, canonicalURL, relPath), } if err := packageTemplate.Execute(w, data); err != nil { http.Error(w, err.Error(), 500) diff --git a/handler_test.go b/handler_test.go index e300d19..b1d5d2e 100644 --- a/handler_test.go +++ b/handler_test.go @@ -1,9 +1,14 @@ package main import ( + "io" + "net/http" + "net/http/httptest" + "strings" "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) var config = ` @@ -120,3 +125,44 @@ func TestPackageLevelURL(t *testing.T) { `) } + +func TestPostRejected(t *testing.T) { + t.Parallel() + + h := CreateHandler(&Config{ + URL: "go.uberalt.org", + Packages: map[string]Package{ + "zap": { + Repo: "github.com/uber-go/zap", + }, + }, + }) + srv := httptest.NewServer(h) + t.Cleanup(srv.Close) + + tests := []struct { + desc string + path string + }{ + {desc: "index", path: "/"}, + {desc: "package", path: "/zap"}, + {desc: "subpackage", path: "/zap/zapcore"}, + } + + for _, tt := range tests { + tt := tt + t.Run(tt.desc, func(t *testing.T) { + t.Parallel() + + res, err := http.Post(srv.URL+tt.path, "text/plain", strings.NewReader("foo")) + require.NoError(t, err) + defer res.Body.Close() + + body, err := io.ReadAll(res.Body) + require.NoError(t, err) + + assert.Equal(t, http.StatusNotFound, res.StatusCode, + "expected 404, got:\n%s", string(body)) + }) + } +} From f417482fee7df5b2f6cd1ae3e235e391aba56364 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Mon, 23 Jan 2023 10:43:20 -0800 Subject: [PATCH 14/17] Upgrade to yaml.v3 (#69) * config: Don't require packages to be alphabetical The configuration parser requries that entries in the 'packages' section are in alphabetical order. It will fail parsing if that's not the case, even if the configuration is otherwise valid. This seems like an unnecessary artificial limitation. Enforcing such a convention should be the user's choice. This change deletes this limitation. * Upgrade to yaml.v3 * template: Use a more fluid layout (#67) Instead of using a table, take advantage of the grid layout. We still print a table of sorts, but it's more fluid in appearance based on width of the screen. On narrower screens, we'll show a listing where each item has a description label next to it rather than at the top. * Add support for package descriptions (#68) * template: Use a more fluid layout Instead of using a table, take advantage of the grid layout. We still print a table of sorts, but it's more fluid in appearance based on width of the screen. On narrower screens, we'll show a listing where each item has a description label next to it rather than at the top. * Add support for package descrpitions Packages may now optionally specify a description. If specified, this is printed below the package information, indented one column to make it stand out. Co-authored-by: Sung Yoon Whang Co-authored-by: Sung Yoon Whang --- config.go | 2 +- go.mod | 3 +-- go.sum | 2 -- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/config.go b/config.go index 57b5dcd..95dad7a 100644 --- a/config.go +++ b/config.go @@ -4,7 +4,7 @@ import ( "os" "strings" - yaml "gopkg.in/yaml.v2" + yaml "gopkg.in/yaml.v3" ) const ( diff --git a/go.mod b/go.mod index f118570..3e2aa57 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.18 require ( github.com/stretchr/testify v1.8.1 golang.org/x/net v0.5.0 - gopkg.in/yaml.v2 v2.4.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -13,5 +13,4 @@ require ( github.com/kr/pretty v0.1.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index e8b209a..c38a744 100644 --- a/go.sum +++ b/go.sum @@ -20,8 +20,6 @@ golang.org/x/net v0.5.0/go.mod h1:DivGGAXEgPSlEBzxGzZI+ZLohi+xUj054jfeKui00ws= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 6b99960380132dc92837e7a67e95901e144e38b0 Mon Sep 17 00:00:00 2001 From: Abhinav Gupta Date: Mon, 23 Jan 2023 10:43:39 -0800 Subject: [PATCH 15/17] Add minimal Dockerfile (#72) Adds a Dockerfile that builds sally using a phased Docker build. The first phase builds sally, and the second phase publishes a scratch image with just sally. If Uber publishes this to a container registry, a user can use it like so: % vim sally.yaml # create a sally yaml % cat > Dockerfile FROM sally:latest COPY sally.yaml /sally.yaml % docker build . Even without publishing, this provides an example of how to build sally for deployment. Testing: I verified the instructions above locally by tagging locally with: % docker build -t sally:latest I'm also using a variation of this Dockerfile in production right now for my own hosted instance of sally. --- Dockerfile | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..d6033ac --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +# This image provides the sally binary. +# It does not include a sally configuration. +# A /sally.yaml file is required for this to run. + +FROM golang:1.19-alpine + +COPY . /build +WORKDIR /build +RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o sally go.uber.org/sally + +FROM scratch +COPY --from=0 /build/sally /sally +EXPOSE 8080 +WORKDIR / +CMD ["/sally"] From 4a2550ca87f9fcbb34a1ee122ec4d1729dff0ca2 Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Mon, 23 Jan 2023 10:59:31 -0800 Subject: [PATCH 16/17] v1.3.0 release (#73) --- CHANGELOG.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f92d7f..813c427 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,13 +4,15 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## [Unreleased] +## [1.3.0] ### Added - Add an optional `description` field to packages. + +### Changed - Use a fluid layout for the index page. This renders better on narrow screens. -[Unreleased]: https://github.com/uber-go/sally/compare/v1.2.0...HEAD +[1.3.0]: https://github.com/uber-go/sally/compare/v1.2.0...HEAD ## [1.2.0] - 2022-05-17 ### Added From 2bf3065993f2fa7ec0c2318c75693008f827b8ed Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Mon, 23 Jan 2023 11:30:21 -0800 Subject: [PATCH 17/17] Fix typo on release notes (#74) Release notes for v1.3.0 had an incorrect link for changes included in v1.3.0; fixing this before we actually tag a release with this. --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 813c427..7bc6250 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,7 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Use a fluid layout for the index page. This renders better on narrow screens. -[1.3.0]: https://github.com/uber-go/sally/compare/v1.2.0...HEAD +[1.3.0]: https://github.com/uber-go/sally/compare/v1.2.0...v1.3.0 ## [1.2.0] - 2022-05-17 ### Added