From 65375091d8211d6613e91bcc2495ff1bed4d6700 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 23 May 2022 12:09:40 +0300 Subject: [PATCH 01/10] fix: Ensure ~/go/bin dir exists during make install --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index fa1ea76cafe1a..92d42633d4d83 100644 --- a/Makefile +++ b/Makefile @@ -47,6 +47,7 @@ fmt: fmt/prettier fmt/terraform gen: coderd/database/querier.go peerbroker/proto/peerbroker.pb.go provisionersdk/proto/provisioner.pb.go provisionerd/proto/provisionerd.pb.go site/src/api/typesGenerated.ts install: build + mkdir -p $(INSTALL_DIR) @echo "--- Copying from bin to $(INSTALL_DIR)" cp -r ./dist/coder-$(GOOS)_$(GOOS)_$(GOARCH)*/* $(INSTALL_DIR) @echo "-- CLI available at $(shell ls $(INSTALL_DIR)/coder*)" From 75068929acf18045fc482d68a792ebadd2bfc5cf Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 23 May 2022 12:09:52 +0300 Subject: [PATCH 02/10] chore: Add .editorconfig to project root --- .editorconfig | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 .editorconfig diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000000..253ce1c3aabe5 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true +indent_style = tab + +[*.{md,json,yaml}] +indent_style = space +indent_size = 2 From afbaeb80d5210359814930a10231daa5571a06fb Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 23 May 2022 12:11:14 +0300 Subject: [PATCH 03/10] chore: Perform dir changes in subshell and run shfmt --- coderd/audit/generate.sh | 10 ++- coderd/database/generate.sh | 76 ++++++++++--------- .../database/migrations/create_migration.sh | 17 +++-- scripts/check_unstaged.sh | 38 ++++++---- scripts/develop.sh | 14 ++-- scripts/sign_macos.sh | 30 +++++--- scripts/yarn_install.sh | 58 +++++++------- 7 files changed, 136 insertions(+), 107 deletions(-) diff --git a/coderd/audit/generate.sh b/coderd/audit/generate.sh index d6763c7b9780c..4a876c3a1548b 100755 --- a/coderd/audit/generate.sh +++ b/coderd/audit/generate.sh @@ -8,8 +8,12 @@ # Usage: # ./generate.sh ... - set -euo pipefail -cd "$(dirname "$0")" && cd "$(git rev-parse --show-toplevel)" -go run ./scripts/auditgen ./coderd/database "$@" +SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") +PROJECT_ROOT=${SCRIPT_DIR}/.. + +( + cd "$PROJECT_ROOT" + go run ./scripts/auditgen ./coderd/database "$@" +) diff --git a/coderd/database/generate.sh b/coderd/database/generate.sh index e8d9a24d217b1..e00b0ae73a425 100755 --- a/coderd/database/generate.sh +++ b/coderd/database/generate.sh @@ -8,39 +8,43 @@ set -euo pipefail -cd "$(dirname "$0")" - -# The logic below depends on the exact version being correct :( -go run github.com/kyleconroy/sqlc/cmd/sqlc@v1.13.0 generate - -first=true -for fi in queries/*.sql.go; do - # Find the last line from the imports section and add 1. - cut=$(grep -n ')' "$fi" | head -n 1 | cut -d: -f1) - cut=$((cut + 1)) - - # Copy the header from the first file only, ignoring the source comment. - if $first; then - head -n 6 < "$fi" | grep -v "source" > queries.sql.go - first=false - fi - - # Append the file past the imports section into queries.sql.go. - tail -n "+$cut" < "$fi" >> queries.sql.go -done - -# Move the files we want. -mv queries/querier.go . -mv queries/models.go . - -# Remove temporary go files. -rm -f queries/*.go - -# Fix struct/interface names. -gofmt -w -r 'Querier -> querier' -- *.go -gofmt -w -r 'Queries -> sqlQuerier' -- *.go - -# Ensure correct imports exist. Modules must all be downloaded so we get correct -# suggestions. -go mod download -goimports -w queries.sql.go +SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") + +( + cd "$SCRIPT_DIR" + + # The logic below depends on the exact version being correct :( + go run github.com/kyleconroy/sqlc/cmd/sqlc@v1.13.0 generate + + first=true + for fi in queries/*.sql.go; do + # Find the last line from the imports section and add 1. + cut=$(grep -n ')' "$fi" | head -n 1 | cut -d: -f1) + cut=$((cut + 1)) + + # Copy the header from the first file only, ignoring the source comment. + if $first; then + head -n 6 <"$fi" | grep -v "source" >queries.sql.go + first=false + fi + + # Append the file past the imports section into queries.sql.go. + tail -n "+$cut" <"$fi" >>queries.sql.go + done + + # Move the files we want. + mv queries/querier.go . + mv queries/models.go . + + # Remove temporary go files. + rm -f queries/*.go + + # Fix struct/interface names. + gofmt -w -r 'Querier -> querier' -- *.go + gofmt -w -r 'Queries -> sqlQuerier' -- *.go + + # Ensure correct imports exist. Modules must all be downloaded so we get correct + # suggestions. + go mod download + goimports -w queries.sql.go +) diff --git a/coderd/database/migrations/create_migration.sh b/coderd/database/migrations/create_migration.sh index 459d0935ebaa6..3046e875e3b9d 100755 --- a/coderd/database/migrations/create_migration.sh +++ b/coderd/database/migrations/create_migration.sh @@ -7,14 +7,17 @@ set -euo pipefail -cd "$(dirname "$0")" +SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") +( + cd "$SCRIPT_DIR" -# if migration name is an empty string exit -[[ -z "${*}" ]] && (echo "Must provide a migration name" && exit 1) + # if migration name is an empty string exit + [[ -z "${*}" ]] && (echo "Must provide a migration name" && exit 1) -# " " && "-" -> "_" -title="$(echo "${@}" | tr "[:upper:]" "[:lower:]" | sed -E -e "s/( |-)/_/g")" + # " " && "-" -> "_" + title="$(echo "${@}" | tr "[:upper:]" "[:lower:]" | sed -E -e "s/( |-)/_/g")" -migrate create -ext sql -dir . -seq "$title" + migrate create -ext sql -dir . -seq "$title" -echo "Run \"make gen\" to generate models." + echo "Run \"make gen\" to generate models." +) diff --git a/scripts/check_unstaged.sh b/scripts/check_unstaged.sh index 1f3d92679e49c..3c4abeb986211 100755 --- a/scripts/check_unstaged.sh +++ b/scripts/check_unstaged.sh @@ -2,23 +2,29 @@ set -euo pipefail -FILES="$(git ls-files --other --modified --exclude-standard)" -if [[ "$FILES" != "" ]]; then - mapfile -t files <<<"$FILES" +SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") +PROJECT_ROOT=${SCRIPT_DIR}/.. - echo "The following files contain unstaged changes:" - echo - for file in "${files[@]}"; do - echo " - $file" - done - echo +( + cd "${PROJECT_ROOT}" - echo "These are the changes:" - echo - for file in "${files[@]}"; do - git --no-pager diff "$file" - done - exit 1 -fi + FILES="$(git ls-files --other --modified --exclude-standard)" + if [[ "$FILES" != "" ]]; then + mapfile -t files <<<"$FILES" + echo "The following files contain unstaged changes:" + echo + for file in "${files[@]}"; do + echo " - $file" + done + echo + + echo "These are the changes:" + echo + for file in "${files[@]}"; do + git --no-pager diff "$file" + done + exit 1 + fi +) exit 0 diff --git a/scripts/develop.sh b/scripts/develop.sh index da481317ad2dc..7d42dafa5c78c 100755 --- a/scripts/develop.sh +++ b/scripts/develop.sh @@ -2,8 +2,8 @@ set -euo pipefail -PROJECT_ROOT="$(git rev-parse --show-toplevel)" -cd "${PROJECT_ROOT}" +SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") +PROJECT_ROOT=${SCRIPT_DIR}/.. echo '== Run "make build" before running this command to build binaries.' echo '== Without these binaries, workspaces will fail to start!' @@ -19,8 +19,10 @@ export CODER_DEV_ADMIN_PASSWORD=password # to kill both at the same time. For more details, see: # https://stackoverflow.com/questions/3004811/how-do-you-run-multiple-programs-in-parallel-from-a-bash-script ( - trap 'kill 0' SIGINT - CODERV2_HOST=http://127.0.0.1:3000 INSPECT_XSTATE=true yarn --cwd=./site dev & - go run -tags embed cmd/coder/main.go server --dev --tunnel=true & - wait + cd "${PROJECT_ROOT}" + + trap 'kill 0' SIGINT + CODERV2_HOST=http://127.0.0.1:3000 INSPECT_XSTATE=true yarn --cwd=./site dev & + go run -tags embed cmd/coder/main.go server --dev --tunnel=true & + wait ) diff --git a/scripts/sign_macos.sh b/scripts/sign_macos.sh index 999039ce77ad7..e068695be8d37 100755 --- a/scripts/sign_macos.sh +++ b/scripts/sign_macos.sh @@ -1,17 +1,23 @@ #!/usr/bin/env bash set -euo pipefail -cd "$(git rev-parse --show-toplevel)" -codesign -s $AC_APPLICATION_IDENTITY -f -v --timestamp --options runtime $1 +SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") +PROJECT_ROOT=${SCRIPT_DIR}/.. -config="$(mktemp -d)/gon.json" -jq -r --null-input --arg path "$(pwd)/$1" '{ - "notarize": [ - { - "path": $path, - "bundle_id": "com.coder.cli" - } - ] -}' > $config -gon $config +( + cd "${PROJECT_ROOT}" + + codesign -s "$AC_APPLICATION_IDENTITY" -f -v --timestamp --options runtime "$1" + + config=$(mktemp -d)/gon.json + jq -r --null-input --arg path "$(pwd)/$1" '{ + "notarize": [ + { + "path": $path, + "bundle_id": "com.coder.cli" + } + ] + }' >"$config" + gon "$config" +) diff --git a/scripts/yarn_install.sh b/scripts/yarn_install.sh index 743a56a1cd6ca..50958185a68f0 100755 --- a/scripts/yarn_install.sh +++ b/scripts/yarn_install.sh @@ -7,33 +7,37 @@ set -euo pipefail -PROJECT_ROOT=$(git rev-parse --show-toplevel) -cd "$PROJECT_ROOT/site" +SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") +PROJECT_ROOT=${SCRIPT_DIR}/.. -yarn_flags=( - # Do not execute install scripts - # TODO: check if build works properly with this enabled - # --ignore-scripts +( + cd "$PROJECT_ROOT/site" - # Check if existing node_modules are valid - # TODO: determine if this is necessary - # --check-files -) + yarn_flags=( + # Do not execute install scripts + # TODO: check if build works properly with this enabled + # --ignore-scripts + + # Check if existing node_modules are valid + # TODO: determine if this is necessary + # --check-files + ) + + if [ -n "${CI:-}" ]; then + yarn_flags+=( + # Install dependencies from lockfile, ensuring builds are fully + # reproducible + --frozen-lockfile + # Suppress progress information + --silent + # Disable interactive prompts for build + --non-interactive + ) + fi -if [ -n "${CI:-}" ]; then - yarn_flags+=( - # Install dependencies from lockfile, ensuring builds are fully - # reproducible - --frozen-lockfile - # Suppress progress information - --silent - # Disable interactive prompts for build - --non-interactive - ) -fi - -# Append whatever is specified on the command line -yarn_flags+=("$@") - -echo "+ yarn install ${yarn_flags[*]}" -yarn install "${yarn_flags[@]}" + # Append whatever is specified on the command line + yarn_flags+=("$@") + + echo "+ yarn install ${yarn_flags[*]}" + yarn install "${yarn_flags[@]}" +) From 4d64125eb673e50628de6b946379dd432c7b79cf Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 23 May 2022 12:33:41 +0300 Subject: [PATCH 04/10] chore: Add .editorconfig for site/ --- site/.editorconfig | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 site/.editorconfig diff --git a/site/.editorconfig b/site/.editorconfig new file mode 100644 index 0000000000000..7bd5108ac1762 --- /dev/null +++ b/site/.editorconfig @@ -0,0 +1,7 @@ +[*] +indent_style = space +indent_size = 2 + +[*.go] +indent_style = tab +indent_size = unset From d8da3ff21cbb806cd346ab62b9db350cb7e3b606 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 23 May 2022 12:36:50 +0300 Subject: [PATCH 05/10] Set indent for tf and sql files too --- .editorconfig | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 253ce1c3aabe5..50f00710d9098 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,6 +7,10 @@ trim_trailing_whitespace = true insert_final_newline = true indent_style = tab -[*.{md,json,yaml}] +[*.{md,json,yaml,tf,tfvars}] indent_style = space indent_size = 2 + +[*.sql] +indent_style = space +indent_size = 4 From 2c491bd7731dbd4177b7f67b7a9ddb769a97dd7c Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 23 May 2022 12:40:00 +0300 Subject: [PATCH 06/10] Fix dump.sql formatting exception --- .editorconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 50f00710d9098..b54df526a6832 100644 --- a/.editorconfig +++ b/.editorconfig @@ -11,6 +11,6 @@ indent_style = tab indent_style = space indent_size = 2 -[*.sql] +[coderd/database/dump.sql] indent_style = space indent_size = 4 From 76760a696abac063d5b7b77526e9423d283d053e Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 23 May 2022 13:01:38 +0300 Subject: [PATCH 07/10] Revert git rev-parse removal --- coderd/audit/generate.sh | 2 +- scripts/check_unstaged.sh | 2 +- scripts/develop.sh | 2 +- scripts/sign_macos.sh | 2 +- scripts/yarn_install.sh | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/coderd/audit/generate.sh b/coderd/audit/generate.sh index 4a876c3a1548b..48ba4fa62fa55 100755 --- a/coderd/audit/generate.sh +++ b/coderd/audit/generate.sh @@ -11,7 +11,7 @@ set -euo pipefail SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") -PROJECT_ROOT=${SCRIPT_DIR}/.. +PROJECT_ROOT=$(cd "$SCRIPT_DIR" && git rev-parse --show-toplevel) ( cd "$PROJECT_ROOT" diff --git a/scripts/check_unstaged.sh b/scripts/check_unstaged.sh index 3c4abeb986211..47bdd265ce407 100755 --- a/scripts/check_unstaged.sh +++ b/scripts/check_unstaged.sh @@ -3,7 +3,7 @@ set -euo pipefail SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") -PROJECT_ROOT=${SCRIPT_DIR}/.. +PROJECT_ROOT=$(cd "$SCRIPT_DIR" && git rev-parse --show-toplevel) ( cd "${PROJECT_ROOT}" diff --git a/scripts/develop.sh b/scripts/develop.sh index 7d42dafa5c78c..41418e4ae3f09 100755 --- a/scripts/develop.sh +++ b/scripts/develop.sh @@ -3,7 +3,7 @@ set -euo pipefail SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") -PROJECT_ROOT=${SCRIPT_DIR}/.. +PROJECT_ROOT=$(cd "$SCRIPT_DIR" && git rev-parse --show-toplevel) echo '== Run "make build" before running this command to build binaries.' echo '== Without these binaries, workspaces will fail to start!' diff --git a/scripts/sign_macos.sh b/scripts/sign_macos.sh index e068695be8d37..26d3fd181bbcb 100755 --- a/scripts/sign_macos.sh +++ b/scripts/sign_macos.sh @@ -3,7 +3,7 @@ set -euo pipefail SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") -PROJECT_ROOT=${SCRIPT_DIR}/.. +PROJECT_ROOT=$(cd "$SCRIPT_DIR" && git rev-parse --show-toplevel) ( cd "${PROJECT_ROOT}" diff --git a/scripts/yarn_install.sh b/scripts/yarn_install.sh index 50958185a68f0..94cb0369cb048 100755 --- a/scripts/yarn_install.sh +++ b/scripts/yarn_install.sh @@ -8,7 +8,7 @@ set -euo pipefail SCRIPT_DIR=$(dirname "${BASH_SOURCE[0]}") -PROJECT_ROOT=${SCRIPT_DIR}/.. +PROJECT_ROOT=$(cd "$SCRIPT_DIR" && git rev-parse --show-toplevel) ( cd "$PROJECT_ROOT/site" From 0a91dd6e6c9c12dbc50f0c91955ce24541db8cda Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 23 May 2022 16:16:10 +0300 Subject: [PATCH 08/10] Add make targets: fmt/shfmt and lint/shellcheck --- Makefile | 22 +++++++++++++++++++--- site/.editorconfig | 3 +++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 92d42633d4d83..7ff2f6dfc6f18 100644 --- a/Makefile +++ b/Makefile @@ -41,7 +41,16 @@ fmt/terraform: $(wildcard *.tf) terraform fmt -recursive .PHONY: fmt/terraform -fmt: fmt/prettier fmt/terraform +fmt/shfmt: $(shell shfmt -f .) + @echo "--- shfmt" +# Only do diff check in CI, errors on diff. +ifdef CI + shfmt -d $(shell shfmt -f .) +else + shfmt -w $(shell shfmt -f .) +endif + +fmt: fmt/prettier fmt/terraform fmt/shfmt .PHONY: fmt gen: coderd/database/querier.go peerbroker/proto/peerbroker.pb.go provisionersdk/proto/provisioner.pb.go provisionerd/proto/provisionerd.pb.go site/src/api/typesGenerated.ts @@ -53,9 +62,16 @@ install: build @echo "-- CLI available at $(shell ls $(INSTALL_DIR)/coder*)" .PHONY: install -lint: +lint: lint/shellcheck lint/go + +lint/go: golangci-lint run -.PHONY: lint +.PHONY: lint/go + +# Use shfmt to determine the shell files, takes editorconfig into consideration. +lint/shellcheck: $(shell shfmt -f .) + @echo "--- shellcheck" + shellcheck $(shell shfmt -f .) peerbroker/proto/peerbroker.pb.go: peerbroker/proto/peerbroker.proto protoc \ diff --git a/site/.editorconfig b/site/.editorconfig index 7bd5108ac1762..2d95ff0f265a2 100644 --- a/site/.editorconfig +++ b/site/.editorconfig @@ -5,3 +5,6 @@ indent_size = 2 [*.go] indent_style = tab indent_size = unset + +[node_modules/**] +ignore = true From ceedec8eecc32e9d564e108738980a533b057b38 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 23 May 2022 16:16:29 +0300 Subject: [PATCH 09/10] Run shfmt and shellcheck in CI --- .github/workflows/coder.yaml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/.github/workflows/coder.yaml b/.github/workflows/coder.yaml index 6ded192d465ab..9975364a670f7 100644 --- a/.github/workflows/coder.yaml +++ b/.github/workflows/coder.yaml @@ -44,6 +44,17 @@ jobs: with: version: v1.46.0 + style-lint-shellcheck: + name: style/lint/shellcheck + timeout-minutes: 5 + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + - name: Run ShellCheck + uses: ludeeus/action-shellcheck@1.1.0 + with: + ignore: node_modules + style-lint-typescript: name: "style/lint/typescript" timeout-minutes: 5 @@ -133,6 +144,9 @@ jobs: - name: Install node_modules run: ./scripts/yarn_install.sh + - name: Install shfmt + run: go install mvdan.cc/sh/v3/cmd/shfmt@v3.5.0 + - run: "make --output-sync -j -B fmt" test-go: From 945ddc9ad48481cd9e8985eca6f027d1a038a179 Mon Sep 17 00:00:00 2001 From: Mathias Fredriksson Date: Mon, 23 May 2022 16:23:21 +0300 Subject: [PATCH 10/10] Add $GOPATH/bin to path in CI for shfmt --- .github/workflows/coder.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/coder.yaml b/.github/workflows/coder.yaml index 9975364a670f7..b441d39d599d4 100644 --- a/.github/workflows/coder.yaml +++ b/.github/workflows/coder.yaml @@ -147,7 +147,9 @@ jobs: - name: Install shfmt run: go install mvdan.cc/sh/v3/cmd/shfmt@v3.5.0 - - run: "make --output-sync -j -B fmt" + - run: | + export PATH=${PATH}:$(go env GOPATH)/bin + make --output-sync -j -B fmt test-go: name: "test/go"