initial commit

Change-Id: Id5c073b65d875a454eeb8258683762ce09f760a4
diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md
new file mode 100644
index 0000000..a619123
--- /dev/null
+++ b/CONTRIBUTING.md
@@ -0,0 +1,12 @@
+# Contributing to Go
+
+This repository is part of the Go open source project.
+
+It is the work of hundreds of contributors. We appreciate your help!
+
+## Contributing code
+
+Please read the [Contribution Guidelines](https://golang.org/doc/contribute.html) before sending patches.
+
+Unless otherwise noted, the Go source files are distributed under
+the BSD-style license found in the LICENSE file.
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..fdd1a1f
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,27 @@
+Copyright (c) 2023 The Go Authors. All rights reserved.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions are
+met:
+
+   * Redistributions of source code must retain the above copyright
+notice, this list of conditions and the following disclaimer.
+   * Redistributions in binary form must reproduce the above
+copyright notice, this list of conditions and the following disclaimer
+in the documentation and/or other materials provided with the
+distribution.
+   * Neither the name of Google Inc. nor the names of its
+contributors may be used to endorse or promote products derived from
+this software without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
diff --git a/PATENTS b/PATENTS
new file mode 100644
index 0000000..7330990
--- /dev/null
+++ b/PATENTS
@@ -0,0 +1,22 @@
+Additional IP Rights Grant (Patents)
+
+"This implementation" means the copyrightable works distributed by
+Google as part of the Go project.
+
+Google hereby grants to You a perpetual, worldwide, non-exclusive,
+no-charge, royalty-free, irrevocable (except as stated in this section)
+patent license to make, have made, use, offer to sell, sell, import,
+transfer and otherwise run, modify and propagate the contents of this
+implementation of Go, where such license applies only to those patent
+claims, both currently owned or controlled by Google and acquired in
+the future, licensable by Google that are necessarily infringed by this
+implementation of Go.  This grant does not include claims that would be
+infringed only as a consequence of further modification of this
+implementation.  If you or your agent or exclusive licensee institute or
+order or agree to the institution of patent litigation against any
+entity (including a cross-claim or counterclaim in a lawsuit) alleging
+that this implementation of Go or any code incorporated within this
+implementation of Go constitutes direct or contributory patent
+infringement, or inducement of patent infringement, then any patent
+rights granted to you under this License for this implementation of Go
+shall terminate as of the date such litigation is filed.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..43eda09
--- /dev/null
+++ b/README.md
@@ -0,0 +1,12 @@
+# pkgsite-metrics
+
+This repository contains code that serves pkg.go.dev/metrics.
+
+## Report Issues / Send Patches
+
+This repository uses Gerrit for code changes. To learn how to submit changes to
+this repository, see https://golang.org/doc/contribute.html.
+
+The main issue tracker for the time repository is located at
+https://github.com/golang/go/issues. Prefix your issue with
+"x/pkgsite-metrics:" in the subject line, so it is easy to find.
diff --git a/all_test.go b/all_test.go
new file mode 100644
index 0000000..8e24c5b
--- /dev/null
+++ b/all_test.go
@@ -0,0 +1,28 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build go1.17 && !windows
+// +build go1.17,!windows
+
+package main
+
+import (
+	"os"
+	"os/exec"
+	"testing"
+)
+
+func Test(t *testing.T) {
+	bash, err := exec.LookPath("bash")
+	if err != nil {
+		t.Skipf("skipping: %v", err)
+	}
+
+	cmd := exec.Command(bash, "./checks.bash")
+	cmd.Stdout = os.Stdout
+	cmd.Stderr = os.Stderr
+	if err := cmd.Run(); err != nil {
+		t.Fatal(err)
+	}
+}
diff --git a/checks.bash b/checks.bash
new file mode 100755
index 0000000..09955b4
--- /dev/null
+++ b/checks.bash
@@ -0,0 +1,133 @@
+#!/usr/bin/env bash
+# Copyright 2023 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+# This file will be run by `go test`.
+# See all_test.go in this directory.
+
+go version
+
+# Ensure that installed go binaries are on the path.
+# This bash expression follows the algorithm described at the top of
+# `go install help`: first try $GOBIN, then $GOPATH/bin, then $HOME/go/bin.
+go_install_dir=${GOBIN:-${GOPATH:-$HOME/go}/bin}
+PATH=$PATH:$go_install_dir
+
+source devtools/lib.sh
+
+# ensure_go_binary verifies that a binary exists in $PATH corresponding to the
+# given go-gettable URI. If no such binary exists, it is fetched via `go get`.
+ensure_go_binary() {
+  local binary=$(basename $1)
+  if ! [ -x "$(command -v $binary)" ]; then
+    info "Installing: $1"
+    # Install the binary in a way that doesn't affect our go.mod file.
+    go install $1
+  fi
+}
+
+# verify_header checks that all given files contain the standard header for Go
+# projects.
+verify_header() {
+  if [[ "$@" != "" ]]; then
+    for FILE in $@
+    do
+        line="$(head -4 $FILE)"
+        if [[ ! $line == *"The Go Authors. All rights reserved."* ]] &&
+         [[ ! $line == "// DO NOT EDIT. This file was copied from" ]]; then
+              err "missing license header: $FILE"
+        fi
+    done
+  fi
+}
+
+# check_headers checks that all source files that have been staged in this
+# commit, and all other non-third-party files in the repo, have a license
+# header.
+check_headers() {
+  if [[ $# -gt 0 ]]; then
+    info "Checking listed files for license header"
+    verify_header $*
+  else
+    info "Checking go and sh files for license header"
+    # Ignore files in testdata directories.
+    verify_header $(find . -name testdata -prune \
+      -o -name '*.go' -print \
+      -o -name '*.sh' -print)
+  fi
+}
+
+
+# check_unparam runs unparam on source files.
+check_unparam() {
+  if [[ $(go version) = *go1.17* ]]; then
+    ensure_go_binary mvdan.cc/unparam
+    runcmd unparam ./...
+  fi
+}
+
+# check_vet runs go vet on source files.
+check_vet() {
+  runcmd go vet -all ./...
+}
+
+# check_staticcheck runs staticcheck on source files.
+check_staticcheck() {
+  if [[ $(go version) = *go1.17* ]]; then
+    ensure_go_binary honnef.co/go/tools/cmd/staticcheck
+    runcmd staticcheck ./...
+  fi
+}
+
+# check_misspell runs misspell on source files.
+check_misspell() {
+  ensure_go_binary github.com/client9/misspell/cmd/misspell
+  runcmd misspell -error $(find . -name .git -prune -o -type f -not -name drydock-20220802.csv -not -name modules.txt)
+}
+
+go_linters() {
+  check_vet
+  check_staticcheck
+  check_misspell
+  check_unparam
+}
+
+go_modtidy() {
+  runcmd go mod tidy
+}
+
+runchecks() {
+  check_headers
+  go_linters
+  go_modtidy
+}
+
+usage() {
+  cat <<EOUSAGE
+Usage: $0 [subcommand]
+Available subcommands:
+  help           - display this help message
+EOUSAGE
+}
+
+main() {
+  case "$1" in
+    "-h" | "--help" | "help")
+      usage
+      exit 0
+      ;;
+    "")
+      runchecks
+      ;;
+    *)
+      usage
+      exit 1
+  esac
+  if [[ $EXIT_CODE != 0 ]]; then
+    err "FAILED; see errors above"
+  fi
+  exit $EXIT_CODE
+}
+
+main $@
diff --git a/cmd/worker/main.go b/cmd/worker/main.go
new file mode 100644
index 0000000..cf7301e
--- /dev/null
+++ b/cmd/worker/main.go
@@ -0,0 +1,14 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+// Command worker runs the worker server.
+package main
+
+import (
+	"fmt"
+)
+
+func main() {
+	fmt.Println("Hello World!")
+}
diff --git a/devtools/lib.sh b/devtools/lib.sh
new file mode 100644
index 0000000..09e0e64
--- /dev/null
+++ b/devtools/lib.sh
@@ -0,0 +1,78 @@
+# Copyright 2023 The Go Authors. All rights reserved.
+# Use of this source code is governed by a BSD-style
+# license that can be found in the LICENSE file.
+
+# Library of useful bash functions and variables.
+
+RED=; GREEN=; YELLOW=; NORMAL=;
+MAXWIDTH=0
+
+if tput setaf 1 >& /dev/null; then
+  RED=`tput setaf 1`
+  GREEN=`tput setaf 2`
+  YELLOW=`tput setaf 3`
+  NORMAL=`tput sgr0`
+  MAXWIDTH=$(( $(tput cols) - 2 ))
+fi
+
+EXIT_CODE=0
+
+info() { echo -e "${GREEN}$@${NORMAL}" 1>&2; }
+warn() { echo -e "${YELLOW}$@${NORMAL}" 1>&2; }
+err() { echo -e "${RED}$@${NORMAL}" 1>&2; EXIT_CODE=1; }
+
+die() {
+  err $@
+  exit 1
+}
+
+dryrun=false
+
+# runcmd prints an info log describing the command that is about to be run, and
+# then runs it. It sets EXIT_CODE to non-zero if the command fails, but does not exit
+# the script.
+runcmd() {
+  msg="$@"
+  if $dryrun; then
+    echo -e "${YELLOW}dryrun${GREEN}\$ $msg${NORMAL}"
+    return 0
+  fi
+  # Truncate command logging for narrow terminals.
+  # Account for the 2 characters of '$ '.
+  if [[ $MAXWIDTH -gt 0 && ${#msg} -gt $MAXWIDTH ]]; then
+    msg="${msg::$(( MAXWIDTH - 3 ))}..."
+  fi
+
+  echo -e "$@\n" 1>&2;
+  $@ || err "command failed"
+}
+
+# tfvar NAME returns the value of NAME in the terraform.tfvars file.
+tfvar() {
+  local name=$1
+  awk '$1 == "'$name'" { print substr($3, 2, length($3)-2) }' terraform/terraform.tfvars
+}
+
+worker_url() {
+  local env=$1
+  case $env in
+    prod) echo https://prod-metrics-worker-7x5g2mdvca-uc.a.run.app;;
+    dev) echo https://dev-metrics-worker-7x5g2mdvca-uc.a.run.app;;
+    *) die "usage: $0 (dev | prod)";;
+  esac
+}
+
+impersonation_service_account() {
+  local env=$1
+  case $env in
+    prod|dev) echo impersonate@go-ecosystem.iam.gserviceaccount.com;;
+    *) die "usage: $0 (dev | prod)";;
+  esac
+}
+
+impersonation_token() {
+  local env=$1
+  gcloud --impersonate-service-account $(impersonation_service_account $env) \
+    auth print-identity-token \
+    --include-email
+}
diff --git a/go.mod b/go.mod
new file mode 100644
index 0000000..f071dc6
--- /dev/null
+++ b/go.mod
@@ -0,0 +1,17 @@
+module golang.org/x/pkgsite-metrics
+
+go 1.18
+
+require (
+	github.com/client9/misspell v0.3.4
+	honnef.co/go/tools v0.3.3
+	mvdan.cc/unparam v0.0.0-20230125043941-70a0ce6e7b95
+)
+
+require (
+	github.com/BurntSushi/toml v0.4.1 // indirect
+	golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e // indirect
+	golang.org/x/mod v0.7.0 // indirect
+	golang.org/x/sys v0.4.0 // indirect
+	golang.org/x/tools v0.5.0 // indirect
+)
diff --git a/go.sum b/go.sum
new file mode 100644
index 0000000..50533cd
--- /dev/null
+++ b/go.sum
@@ -0,0 +1,19 @@
+github.com/BurntSushi/toml v0.4.1 h1:GaI7EiDXDRfa8VshkTj7Fym7ha+y8/XxIgD2okUIjLw=
+github.com/BurntSushi/toml v0.4.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
+github.com/client9/misspell v0.3.4 h1:ta993UF76GwbvJcIo3Y68y/M3WxlpEHPWIGDkJYwzJI=
+github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
+github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e h1:aoZm08cpOy4WuID//EZDgcC4zIxODThtZNPirFr42+A=
+github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
+golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e h1:qyrTQ++p1afMkO4DPEeLGq/3oTsdlvdH4vqZUBWzUKM=
+golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e/go.mod h1:AbB0pIl9nAr9wVwH+Z2ZpaocVmF5I4GyWCDIsVjR0bk=
+golang.org/x/mod v0.7.0 h1:LapD9S96VoQRhi/GrNTqeBJFrUjs5UHCAtTlgwA5oZA=
+golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
+golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
+golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18=
+golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/tools v0.5.0 h1:+bSpV5HIeWkuvgaMfI3UmKRThoTA5ODJTUd8T17NO+4=
+golang.org/x/tools v0.5.0/go.mod h1:N+Kgy78s5I24c24dU8OfWNEotWjutIs8SnJvn5IDq+k=
+honnef.co/go/tools v0.3.3 h1:oDx7VAwstgpYpb3wv0oxiZlxY+foCpRAwY7Vk6XpAgA=
+honnef.co/go/tools v0.3.3/go.mod h1:jzwdWgg7Jdq75wlfblQxO4neNaFFSvgc1tD5Wv8U0Yw=
+mvdan.cc/unparam v0.0.0-20230125043941-70a0ce6e7b95 h1:n/xhncJPSt0YzfOhnyn41XxUdrWQNgmLBG72FE27Fqw=
+mvdan.cc/unparam v0.0.0-20230125043941-70a0ce6e7b95/go.mod h1:2vU506e8nGWodqcci641NLi4im2twWSq4Lod756epHQ=
diff --git a/tools_test.go b/tools_test.go
new file mode 100644
index 0000000..da66ca6
--- /dev/null
+++ b/tools_test.go
@@ -0,0 +1,14 @@
+// Copyright 2023 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+//go:build tools
+// +build tools
+
+package main
+
+import (
+	_ "github.com/client9/misspell/cmd/misspell"
+	_ "honnef.co/go/tools/cmd/staticcheck"
+	_ "mvdan.cc/unparam"
+)