Skip to content

Commit

Permalink
Update service template with go tool support
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Johnson committed Feb 14, 2025
1 parent 325fdf0 commit 9058723
Show file tree
Hide file tree
Showing 9 changed files with 1,215 additions and 1,322 deletions.
9 changes: 7 additions & 2 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ version: 2.1

orbs:
slack: circleci/slack@5.1.1
go: circleci/go@2.2.4

workflows:
main-workflow:
Expand Down Expand Up @@ -77,6 +78,9 @@ jobs:
- run:
name: go tidy
command: go mod tidy -v
- run:
name: run goimports
command: ./do run-goimports
- run:
name: verify no changes
command: git diff --ignore-matching-lines='Generated on ' --exit-code
Expand Down Expand Up @@ -270,8 +274,9 @@ commands:
setup:
steps:
- checkout
- run: go mod download
- run: ./do install-devtools
- go/with-cache:
steps:
- run: go mod download

store_results:
steps:
Expand Down
3 changes: 1 addition & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ run:

# Do not treat these as gospel, adjust as appropriate
linters-settings:
govet:
check-shadowing: false
shadow: false
golint:
min-confidence: 0
gocyclo:
Expand Down
1 change: 0 additions & 1 deletion cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/circleci/ex/system"
"github.com/circleci/ex/termination"

/* todo rename 'ex-service-template' to 'your-service' */
"github.com/circleci/ex-service-template/api/api"
"github.com/circleci/ex-service-template/books"
"github.com/circleci/ex-service-template/cmd"
Expand Down
47 changes: 8 additions & 39 deletions do
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ set -eu -o pipefail

_version=1.0.${CIRCLE_BUILD_NUM-0}-$(git rev-parse --short HEAD 2>/dev/null || echo latest)
reportDir="test-reports"
serviceName="ex-service-template"
serviceName="ex-service-template" #FIXME: update with the new service name
package="github.com/circleci/${serviceName}"

make-target() {
mkdir -p "target"
Expand Down Expand Up @@ -35,7 +36,7 @@ help_build="Build the binaries for production"
build() {
local date ldflags
date=$(date "+%FT%T%z")
ldflags="-s -w -X github.com/circleci/${serviceName}/cmd.Version=$_version -X github.com/circleci/${serviceName}/cmd.Date=$date"
ldflags="-s -w -X ${package}/cmd.Version=$_version -X ${package}/cmd.Date=$date"

GOOS=linux GOARCH=amd64 binary "$ldflags" &

Expand All @@ -59,7 +60,7 @@ build() {
# shellcheck disable=SC2034
help_lint="Run golanci-lint to lint go files."
lint() {
exec ./bin/golangci-lint run "${@:-./...}"
exec go tool golangci-lint run "${@:-./...}"
}

# This variable is used, but shellcheck can't tell.
Expand All @@ -79,26 +80,25 @@ help_test="Run normal unit tests"
test() {
mkdir -p "${reportDir}"
# -count=1 is used to forcibly disable test result caching
./bin/gotestsum --junitfile="${reportDir}/junit.xml" -- -race -count=1 "${@:-./...}"
go tool gotestsum --junitfile="${reportDir}/junit.xml" -- -race -count=1 "${@:-./...}"
}

# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_run_goimports="Run goimports for package"
run-goimports () {
./bin/gosimports -local "github.com/circleci/ex-service-template" -w
go tool gosimports -local "${package}" -w .
}

# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_godoc="Run godoc to read documentation."
godoc() {
install-go-bin "golang.org/x/tools/cmd/godoc@v0.1.3"
local url
url="http://localhost:6060/pkg/github.com/circleci/${serviceName}/"
url="http://localhost:6060/pkg/${package}/"
command -v xdg-open && xdg-open $url &
command -v open && open $url &
./bin/godoc -http=127.0.0.1:6060
go tool godoc -http=127.0.0.1:6060
}

# This variable is used, but shellcheck can't tell.
Expand All @@ -108,29 +108,6 @@ go-mod-tidy() {
go mod tidy -v
}

install-go-bin() {
local binDir="$PWD/bin"
for pkg in "${@}"; do
echo "${pkg}"
(
cd tools
GOBIN="${binDir}" go install "${pkg}"
)
done
}

# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_install_devtools="Install tools that other tasks expect into ./bin"
install-devtools() {
local tools=()
while IFS='' read -r value; do
tools+=("$value")
done < <(grep _ tools/tools.go | awk -F'"' '{print $2}')

install-go-bin "${tools[@]}"
}

# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_create_stub_test_files="Create an empty pkg_test in all directories with no tests.
Expand All @@ -145,14 +122,6 @@ create-stub-test-files() {
xargs -r --max-args=2 bash -c 'echo "package $0" > "$1/pkg_test.go"'
}

# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_run_goimports="Run goimports for package"
run-goimports () {
command -v ./bin/goimports || install-go-bin "golang.org/x/tools/cmd/goimports@v0.0.0-20201208183658-cc330816fc52"
./bin/goimports -local "github.com/circleci/${serviceName}" -w "${@:-.}"
}

# This variable is used, but shellcheck can't tell.
# shellcheck disable=SC2034
help_version="Print version"
Expand Down
Loading

0 comments on commit 9058723

Please sign in to comment.