Skip to content

Commit

Permalink
feat: Add precommit hooks and add taskfile (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaikelVeen authored Sep 9, 2024
1 parent 1dd9273 commit bab51f5
Show file tree
Hide file tree
Showing 9 changed files with 245 additions and 71 deletions.
4 changes: 0 additions & 4 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,12 @@ linters:
- asciicheck # checks that your code does not contain non-ASCII identifiers
- bidichk # checks for dangerous unicode character sequences
- bodyclose # checks whether HTTP response body is closed successfully
- canonicalheader # checks whether net/http.Header uses canonical header
- copyloopvar # detects places where loop variables are copied
- cyclop # checks function and package cyclomatic complexity
- dupl # tool for code clone detection
- durationcheck # checks for two durations multiplied together
- errname # checks that sentinel errors are prefixed with the Err and error types are suffixed with the Error
- errorlint # finds code that will cause problems with the error wrapping scheme introduced in Go 1.13
- exhaustive # checks exhaustiveness of enum switch statements
- fatcontext # detects nested contexts in loops
#- forbidigo # forbids identifiers
- funlen # tool for detection of long functions
- gocheckcompilerdirectives # validates go compiler directive comments (//go:)
Expand All @@ -249,7 +246,6 @@ linters:
- gomodguard # allow and block lists linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations
- goprintffuncname # checks that printf-like functions are named with f at the end
- gosec # inspects source code for security problems
- intrange # finds places where for loops could make use of an integer range
- lll # reports long lines
- loggercheck # checks key value pairs for common logger libraries (kitlog,klog,logr,zap)
- makezero # finds slice declarations with non-zero initial length
Expand Down
65 changes: 0 additions & 65 deletions Makefile

This file was deleted.

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ toolchain go1.23.0

require (
github.com/djherbis/times v1.6.0
github.com/google/uuid v1.6.0
github.com/lib/pq v1.10.9
github.com/lmittmann/tint v1.0.4
github.com/matryer/moq v0.5.0
Expand All @@ -30,7 +31,6 @@ require (
github.com/getkin/kin-openapi v0.124.0 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/swag v0.22.8 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/invopop/yaml v0.2.0 // indirect
Expand Down
7 changes: 7 additions & 0 deletions scripts/changelog.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

origin_head_sha=$(git rev-parse origin/HEAD)

git cliff "$origin_head_sha"..HEAD | pbcopy
echo Changelog has been copied to the clipboard.
pbpaste
15 changes: 15 additions & 0 deletions scripts/hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -euo pipefail

linter() {
echo "Running Go linter..."
if task lint; then
echo "Linter passed!"
else
echo "Linter failed. Please fix the issues before committing."
exit 1
fi
}

linter
14 changes: 14 additions & 0 deletions scripts/hooks/prepare-commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

COMMIT_MSG_FILE=$1
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
REGEX="^((Merge[ a-z-]* branch.*)|(Revert*)|((build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\(.*\))?!?: .*))"

if [[ ! "$COMMIT_MSG" =~ $REGEX ]]; then
RED='\033[0;31m'
RESET='\033[0m'

echo -e "${RED}Error: Commit message does not follow the Conventional Commits format.${RESET}"
echo "Example: feat(ui): add new button component"
exit 1
fi
153 changes: 153 additions & 0 deletions scripts/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#!/usr/bin/env zsh

# Exit when a command fails.
set -euo pipefail

BREW_PACKAGES=(go-task jq golangci-lint git-cliff)
NODE_VERSION="20.17.0"

BLACK="\033[30m"
RED="\033[31m"
GREEN="\033[32m"
YELLOW="\033[33m"
BLUE="\033[34m"
MAGENTA="\033[35m"
CYAN="\033[36m"
WHITE="\033[37m"
DEFAULT="\033[39m"
RESET="\033[0m"
BOLD="\033[1m"
UNDERLINE="\033[4m"
REVERSED="\033[7m"

pretty_print() {
local message="$1"
local color="${2:-$DEFAULT}"
local style="${3:-}"

echo -e "${color}${style}$message${RESET}"
}

header() {
pretty_print "\n========================================================================"
pretty_print "$1" $BLUE $BOLD
pretty_print "========================================================================\n"
}

success() {
pretty_print ""
pretty_print "$1" $GREEN
}

install_go () {
header "Setting up Go 🐹"

if command -v go; then
echo "Go is already installed"
go version
return
else
curl -L https://git.io/vQhTU | bash -s -- --version 1.23.1
fi

success "Finished setting up Go!"
}

# Install Homebrew, a package manager for macOS
install_brew() {
header "Setting up Homebrew 🍺"

if command -v brew; then
echo "Homebrew is already installed"
eval "$(brew shellenv)"
else
/bin/zsh -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
INSTALL_DIR="/opt/homebrew/bin"

if read -q "confirm? -> add $INSTALL_DIR to path in ~/.zprofile? [y/N] "; then
echo 'eval "$('$INSTALL_DIR'/brew shellenv)"' >>~/.zprofile
fi

echo
eval "$($INSTALL_DIR/brew shellenv)"

if ! command -v brew >/dev/null; then
echo "Brew installation failed!"
exit 1
fi
fi

success "Finished setting up Homebrew!"
}

# Install Volta, a JavaScript tool manager
install_volta() {
header "Setting up Volta ⚡️"

if command -v volta; then
echo "Volta is already installed"
else
curl https://get.volta.sh | bash
fi

success "Finished setting up Volta!"
}

# Install Node.js using Volta
install_node() {
header "Installing Node.js 🚀"

if command -v node; then
echo "Node.js is already installed"
node --version
return
else
volta install node@${NODE_VERSION}
# Run node version to verify installation
node --version
fi

success "Finished installing Node.js!"
}

# Install or upgrade a package with Homebrew
brew_install() {
if brew ls --versions "$1"; then
brew upgrade "$1"
else
brew install "$1"
fi
}

# Install packages with Homebrew
install_packages() {
header "Installing packages with Homebrew 🍺"

for package in "${BREW_PACKAGES[@]}"; do
echo "Installing or upgrading $package..."
brew_install "$package"
echo "Installed or upgraded $package"
done

success "Finished installing packages with Homebrew!"
}

# Setup git hooks.
setup_git_hooks() {
header "Setting up git hooks 🎣"

task setup-git-hooks
success "Finished setting up git hooks!"
}

echo "Setting up your development environment..."

install_go
install_brew
install_volta
install_node
install_packages

setup_git_hooks

pretty_print "\nFinished setting up your development environment! 🎉" $GREEN $BOLD
51 changes: 51 additions & 0 deletions taskfile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
version: '3'

vars:
SHELL_TO_USE:
sh: |
echo ${SHELL_TO_USE:-$(basename $SHELL)}
tasks:
build:
desc: "Build the project"
cmds:
- "go build -o {{.CLI_ARGS}}"

test:
desc: "Run tests"
cmds:
- "go test -race -short -v ./..."

coverage:
desc: "Run tests with coverage"
cmds:
- "go test -cover -covermode=count -coverprofile=coverage.out ./..."
- "go tool cover -func coverage.out"

typespec:
desc: "Generate OpenAPI spec"
cmds:
- "{{.SHELL_TO_USE}} scripts/compile-spec.sh"
- "go generate ./..."

lint-fix:
desc: "Lint Go Code"
cmds:
- "golangci-lint run --fix"

lint:
desc: "Lint all modules"
cmds:
- "golangci-lint run"

changelog:
desc: "Generate a changelog for a pull request"
cmds:
- "{{.SHELL_TO_USE}} scripts/generate-pr-changelog.sh"

setup-git-hooks:
desc: "Set up Git hooks"
cmds:
- "chmod +x ./scripts/hooks/pre-commit"
- "chmod +x ./scripts/hooks/prepare-commit-msg"
- "git config core.hooksPath ./scripts/hooks"
5 changes: 4 additions & 1 deletion typespec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"@typespec/openapi3": "latest",
"@typespec/versioning": "latest"
},
"private": true
"private": true,
"volta": {
"node": "20.17.0"
}
}

0 comments on commit bab51f5

Please sign in to comment.