use crate::error::ZackstrapError;
use std::path::PathBuf;
use tokio::fs;
pub struct GitHooksGenerator {
target_dir: PathBuf,
}
impl GitHooksGenerator {
pub fn new(target_dir: PathBuf) -> Self {
Self { target_dir }
}
pub async fn generate_ruby_hooks(
&self,
template: &str,
force: bool,
) -> Result<(), ZackstrapError> {
let hooks_dir = self.target_dir.join(".git").join("hooks");
if !hooks_dir.exists() {
return Err(ZackstrapError::GitNotInitialized);
}
let pre_commit_content = self.get_ruby_pre_commit_hook(template);
self.write_hook_file(&hooks_dir.join("pre-commit"), &pre_commit_content, force)
.await?;
let pre_push_content = self.get_ruby_pre_push_hook(template);
self.write_hook_file(&hooks_dir.join("pre-push"), &pre_push_content, force)
.await?;
let commit_msg_content = self.get_commit_msg_hook();
self.write_hook_file(&hooks_dir.join("commit-msg"), &commit_msg_content, force)
.await?;
Ok(())
}
pub async fn generate_python_hooks(
&self,
template: &str,
force: bool,
) -> Result<(), ZackstrapError> {
let hooks_dir = self.target_dir.join(".git").join("hooks");
if !hooks_dir.exists() {
return Err(ZackstrapError::GitNotInitialized);
}
let pre_commit_content = self.get_python_pre_commit_hook(template);
self.write_hook_file(&hooks_dir.join("pre-commit"), &pre_commit_content, force)
.await?;
let pre_push_content = self.get_python_pre_push_hook(template);
self.write_hook_file(&hooks_dir.join("pre-push"), &pre_push_content, force)
.await?;
let commit_msg_content = self.get_commit_msg_hook();
self.write_hook_file(&hooks_dir.join("commit-msg"), &commit_msg_content, force)
.await?;
Ok(())
}
pub async fn generate_node_hooks(
&self,
template: &str,
force: bool,
) -> Result<(), ZackstrapError> {
let hooks_dir = self.target_dir.join(".git").join("hooks");
if !hooks_dir.exists() {
return Err(ZackstrapError::GitNotInitialized);
}
let pre_commit_content = self.get_node_pre_commit_hook(template);
self.write_hook_file(&hooks_dir.join("pre-commit"), &pre_commit_content, force)
.await?;
let pre_push_content = self.get_node_pre_push_hook(template);
self.write_hook_file(&hooks_dir.join("pre-push"), &pre_push_content, force)
.await?;
let commit_msg_content = self.get_commit_msg_hook();
self.write_hook_file(&hooks_dir.join("commit-msg"), &commit_msg_content, force)
.await?;
Ok(())
}
pub async fn generate_go_hooks(
&self,
template: &str,
force: bool,
) -> Result<(), ZackstrapError> {
let hooks_dir = self.target_dir.join(".git").join("hooks");
if !hooks_dir.exists() {
return Err(ZackstrapError::GitNotInitialized);
}
let pre_commit_content = self.get_go_pre_commit_hook(template);
self.write_hook_file(&hooks_dir.join("pre-commit"), &pre_commit_content, force)
.await?;
let pre_push_content = self.get_go_pre_push_hook(template);
self.write_hook_file(&hooks_dir.join("pre-push"), &pre_push_content, force)
.await?;
let commit_msg_content = self.get_commit_msg_hook();
self.write_hook_file(&hooks_dir.join("commit-msg"), &commit_msg_content, force)
.await?;
Ok(())
}
pub async fn generate_rust_hooks(
&self,
template: &str,
force: bool,
) -> Result<(), ZackstrapError> {
let hooks_dir = self.target_dir.join(".git").join("hooks");
if !hooks_dir.exists() {
return Err(ZackstrapError::GitNotInitialized);
}
let pre_commit_content = self.get_rust_pre_commit_hook(template);
self.write_hook_file(&hooks_dir.join("pre-commit"), &pre_commit_content, force)
.await?;
let pre_push_content = self.get_rust_pre_push_hook(template);
self.write_hook_file(&hooks_dir.join("pre-push"), &pre_push_content, force)
.await?;
let commit_msg_content = self.get_commit_msg_hook();
self.write_hook_file(&hooks_dir.join("commit-msg"), &commit_msg_content, force)
.await?;
Ok(())
}
pub async fn generate_bash_hooks(
&self,
template: &str,
force: bool,
) -> Result<(), ZackstrapError> {
let hooks_dir = self.target_dir.join(".git").join("hooks");
if !hooks_dir.exists() {
return Err(ZackstrapError::GitNotInitialized);
}
let pre_commit_content = self.get_bash_pre_commit_hook(template);
self.write_hook_file(&hooks_dir.join("pre-commit"), &pre_commit_content, force)
.await?;
let pre_push_content = self.get_bash_pre_push_hook(template);
self.write_hook_file(&hooks_dir.join("pre-push"), &pre_push_content, force)
.await?;
let commit_msg_content = self.get_commit_msg_hook();
self.write_hook_file(&hooks_dir.join("commit-msg"), &commit_msg_content, force)
.await?;
Ok(())
}
pub async fn generate_basic_hooks(&self, force: bool) -> Result<(), ZackstrapError> {
let hooks_dir = self.target_dir.join(".git").join("hooks");
if !hooks_dir.exists() {
return Err(ZackstrapError::GitNotInitialized);
}
let pre_commit_content = self.get_basic_pre_commit_hook();
self.write_hook_file(&hooks_dir.join("pre-commit"), &pre_commit_content, force)
.await?;
let pre_push_content = self.get_basic_pre_push_hook();
self.write_hook_file(&hooks_dir.join("pre-push"), &pre_push_content, force)
.await?;
let commit_msg_content = self.get_commit_msg_hook();
self.write_hook_file(&hooks_dir.join("commit-msg"), &commit_msg_content, force)
.await?;
Ok(())
}
async fn write_hook_file(
&self,
path: &PathBuf,
content: &str,
force: bool,
) -> Result<(), ZackstrapError> {
if path.exists() && !force {
return Err(ZackstrapError::FileExists(path.clone()));
}
fs::write(path, content).await?;
#[cfg(unix)]
{
use std::process::Command;
let _ = Command::new("chmod").arg("+x").arg(path).output();
}
Ok(())
}
fn get_ruby_pre_commit_hook(&self, template: &str) -> String {
match template {
"rails" => r#"#!/bin/bash
# Ruby Rails Pre-commit Hook
set -e
echo "๐ Running Ruby Rails pre-commit checks..."
# Check if bundle is available
if ! command -v bundle &> /dev/null; then
echo "โ Bundle not found. Please install Ruby and Bundler."
exit 1
fi
# Install dependencies if needed
if [ ! -d "vendor/bundle" ]; then
echo "๐ฆ Installing dependencies..."
bundle install
fi
# Run RuboCop
echo "๐ Running RuboCop..."
bundle exec rubocop --format simple
# Run Prettier if available
if command -v prettier &> /dev/null; then
echo "๐จ Running Prettier..."
prettier --check "**/*.{js,ts,json,md,yml,yaml}" || true
fi
# Run RSpec tests
echo "๐งช Running RSpec tests..."
bundle exec rspec --format documentation
echo "โ
Ruby Rails pre-commit checks passed!"
"#
.to_string(),
"sinatra" => r#"#!/bin/bash
# Ruby Sinatra Pre-commit Hook
set -e
echo "๐ Running Ruby Sinatra pre-commit checks..."
# Check if bundle is available
if ! command -v bundle &> /dev/null; then
echo "โ Bundle not found. Please install Ruby and Bundler."
exit 1
fi
# Install dependencies if needed
if [ ! -d "vendor/bundle" ]; then
echo "๐ฆ Installing dependencies..."
bundle install
fi
# Run RuboCop
echo "๐ Running RuboCop..."
bundle exec rubocop --format simple
# Run Prettier if available
if command -v prettier &> /dev/null; then
echo "๐จ Running Prettier..."
prettier --check "**/*.{js,ts,json,md,yml,yaml}" || true
fi
# Run RSpec tests
echo "๐งช Running RSpec tests..."
bundle exec rspec --format documentation
echo "โ
Ruby Sinatra pre-commit checks passed!"
"#
.to_string(),
"gem" => r#"#!/bin/bash
# Ruby Gem Pre-commit Hook
set -e
echo "๐ Running Ruby Gem pre-commit checks..."
# Check if bundle is available
if ! command -v bundle &> /dev/null; then
echo "โ Bundle not found. Please install Ruby and Bundler."
exit 1
fi
# Install dependencies if needed
if [ ! -d "vendor/bundle" ]; then
echo "๐ฆ Installing dependencies..."
bundle install
fi
# Run RuboCop
echo "๐ Running RuboCop..."
bundle exec rubocop --format simple
# Run Prettier if available
if command -v prettier &> /dev/null; then
echo "๐จ Running Prettier..."
prettier --check "**/*.{js,ts,json,md,yml,yaml}" || true
fi
# Run RSpec tests
echo "๐งช Running RSpec tests..."
bundle exec rspec --format documentation
# Build gem to check for errors
echo "๐จ Building gem..."
bundle exec gem build *.gemspec
echo "โ
Ruby Gem pre-commit checks passed!"
"#
.to_string(),
_ => r#"#!/bin/bash
# Ruby Pre-commit Hook
set -e
echo "๐ Running Ruby pre-commit checks..."
# Check if bundle is available
if ! command -v bundle &> /dev/null; then
echo "โ Bundle not found. Please install Ruby and Bundler."
exit 1
fi
# Install dependencies if needed
if [ ! -d "vendor/bundle" ]; then
echo "๐ฆ Installing dependencies..."
bundle install
fi
# Run RuboCop
echo "๐ Running RuboCop..."
bundle exec rubocop --format simple
# Run Prettier if available
if command -v prettier &> /dev/null; then
echo "๐จ Running Prettier..."
prettier --check "**/*.{js,ts,json,md,yml,yaml}" || true
fi
# Run RSpec tests if available
if [ -f "spec/spec_helper.rb" ] || [ -f "spec/rails_helper.rb" ]; then
echo "๐งช Running RSpec tests..."
bundle exec rspec --format documentation
fi
echo "โ
Ruby pre-commit checks passed!"
"#
.to_string(),
}
}
fn get_ruby_pre_push_hook(&self, template: &str) -> String {
match template {
"rails" => r#"#!/bin/bash
# Ruby Rails Pre-push Hook
set -e
echo "๐ Running Ruby Rails pre-push checks..."
# Run full test suite
echo "๐งช Running full RSpec test suite..."
bundle exec rspec --format progress
# Run security audit
echo "๐ Running security audit..."
bundle exec bundle audit --update
# Check for outdated dependencies
echo "๐ฆ Checking for outdated dependencies..."
bundle exec bundle outdated || true
echo "โ
Ruby Rails pre-push checks passed!"
"#
.to_string(),
_ => r#"#!/bin/bash
# Ruby Pre-push Hook
set -e
echo "๐ Running Ruby pre-push checks..."
# Run full test suite if available
if [ -f "spec/spec_helper.rb" ] || [ -f "spec/rails_helper.rb" ]; then
echo "๐งช Running full RSpec test suite..."
bundle exec rspec --format progress
fi
# Run security audit
echo "๐ Running security audit..."
bundle exec bundle audit --update || true
# Check for outdated dependencies
echo "๐ฆ Checking for outdated dependencies..."
bundle exec bundle outdated || true
echo "โ
Ruby pre-push checks passed!"
"#
.to_string(),
}
}
fn get_python_pre_commit_hook(&self, template: &str) -> String {
match template {
"django" => r#"#!/bin/bash
# Python Django Pre-commit Hook
set -e
echo "๐ Running Python Django pre-commit checks..."
# Check if Python is available
if ! command -v python &> /dev/null; then
echo "โ Python not found. Please install Python."
exit 1
fi
# Activate virtual environment if it exists
if [ -f "venv/bin/activate" ]; then
echo "๐ Activating virtual environment..."
source venv/bin/activate
elif [ -f ".venv/bin/activate" ]; then
echo "๐ Activating virtual environment..."
source .venv/bin/activate
fi
# Install dependencies if needed
if [ -f "requirements.txt" ]; then
echo "๐ฆ Installing dependencies..."
pip install -r requirements.txt
fi
if [ -f "requirements-dev.txt" ]; then
echo "๐ฆ Installing dev dependencies..."
pip install -r requirements-dev.txt
fi
# Run Black formatting check
echo "๐จ Running Black formatting check..."
black --check .
# Run Flake8 linting
echo "๐ Running Flake8 linting..."
flake8 .
# Run MyPy type checking
echo "๐ฌ Running MyPy type checking..."
mypy .
# Run Django tests
echo "๐งช Running Django tests..."
python manage.py test
echo "โ
Python Django pre-commit checks passed!"
"#
.to_string(),
"flask" => r#"#!/bin/bash
# Python Flask Pre-commit Hook
set -e
echo "๐ Running Python Flask pre-commit checks..."
# Check if Python is available
if ! command -v python &> /dev/null; then
echo "โ Python not found. Please install Python."
exit 1
fi
# Activate virtual environment if it exists
if [ -f "venv/bin/activate" ]; then
echo "๐ Activating virtual environment..."
source venv/bin/activate
elif [ -f ".venv/bin/activate" ]; then
echo "๐ Activating virtual environment..."
source .venv/bin/activate
fi
# Install dependencies if needed
if [ -f "requirements.txt" ]; then
echo "๐ฆ Installing dependencies..."
pip install -r requirements.txt
fi
if [ -f "requirements-dev.txt" ]; then
echo "๐ฆ Installing dev dependencies..."
pip install -r requirements-dev.txt
fi
# Run Black formatting check
echo "๐จ Running Black formatting check..."
black --check .
# Run Flake8 linting
echo "๐ Running Flake8 linting..."
flake8 .
# Run MyPy type checking
echo "๐ฌ Running MyPy type checking..."
mypy .
# Run Pytest tests
echo "๐งช Running Pytest tests..."
pytest
echo "โ
Python Flask pre-commit checks passed!"
"#
.to_string(),
_ => r#"#!/bin/bash
# Python Pre-commit Hook
set -e
echo "๐ Running Python pre-commit checks..."
# Check if Python is available
if ! command -v python &> /dev/null; then
echo "โ Python not found. Please install Python."
exit 1
fi
# Activate virtual environment if it exists
if [ -f "venv/bin/activate" ]; then
echo "๐ Activating virtual environment..."
source venv/bin/activate
elif [ -f ".venv/bin/activate" ]; then
echo "๐ Activating virtual environment..."
source .venv/bin/activate
fi
# Install dependencies if needed
if [ -f "requirements.txt" ]; then
echo "๐ฆ Installing dependencies..."
pip install -r requirements.txt
fi
if [ -f "requirements-dev.txt" ]; then
echo "๐ฆ Installing dev dependencies..."
pip install -r requirements-dev.txt
fi
# Run Black formatting check
echo "๐จ Running Black formatting check..."
black --check .
# Run Flake8 linting
echo "๐ Running Flake8 linting..."
flake8 .
# Run MyPy type checking
echo "๐ฌ Running MyPy type checking..."
mypy .
# Run Pytest tests if available
if [ -f "pytest.ini" ] || [ -f "pyproject.toml" ] || [ -d "tests" ]; then
echo "๐งช Running Pytest tests..."
pytest
fi
echo "โ
Python pre-commit checks passed!"
"#
.to_string(),
}
}
fn get_python_pre_push_hook(&self, template: &str) -> String {
match template {
"django" => r#"#!/bin/bash
# Python Django Pre-push Hook
set -e
echo "๐ Running Python Django pre-push checks..."
# Activate virtual environment if it exists
if [ -f "venv/bin/activate" ]; then
echo "๐ Activating virtual environment..."
source venv/bin/activate
elif [ -f ".venv/bin/activate" ]; then
echo "๐ Activating virtual environment..."
source .venv/bin/activate
fi
# Run full test suite
echo "๐งช Running full Django test suite..."
python manage.py test --parallel
# Run security check
echo "๐ Running Django security check..."
python manage.py check --deploy
# Run coverage report
echo "๐ Running coverage report..."
coverage run --source='.' manage.py test
coverage report
echo "โ
Python Django pre-push checks passed!"
"#
.to_string(),
_ => r#"#!/bin/bash
# Python Pre-push Hook
set -e
echo "๐ Running Python pre-push checks..."
# Activate virtual environment if it exists
if [ -f "venv/bin/activate" ]; then
echo "๐ Activating virtual environment..."
source venv/bin/activate
elif [ -f ".venv/bin/activate" ]; then
echo "๐ Activating virtual environment..."
source .venv/bin/activate
fi
# Run full test suite
echo "๐งช Running full Pytest test suite..."
pytest --verbose
# Run coverage report
echo "๐ Running coverage report..."
coverage run -m pytest
coverage report
echo "โ
Python pre-push checks passed!"
"#
.to_string(),
}
}
fn get_node_pre_commit_hook(&self, template: &str) -> String {
match template {
"express" => r#"#!/bin/bash
# Node.js Express Pre-commit Hook
set -e
echo "๐ Running Node.js Express pre-commit checks..."
# Check if Node.js is available
if ! command -v node &> /dev/null; then
echo "โ Node.js not found. Please install Node.js."
exit 1
fi
# Check if npm is available
if ! command -v npm &> /dev/null; then
echo "โ npm not found. Please install npm."
exit 1
fi
# Install dependencies if needed
if [ ! -d "node_modules" ]; then
echo "๐ฆ Installing dependencies..."
npm install
fi
# Run ESLint
echo "๐ Running ESLint..."
npm run lint
# Run Prettier
echo "๐จ Running Prettier..."
npm run format
# Run TypeScript check if available
if [ -f "tsconfig.json" ]; then
echo "๐ฌ Running TypeScript check..."
npx tsc --noEmit
fi
# Run Jest tests
echo "๐งช Running Jest tests..."
npm test
echo "โ
Node.js Express pre-commit checks passed!"
"#
.to_string(),
"react" => r#"#!/bin/bash
# Node.js React Pre-commit Hook
set -e
echo "๐ Running Node.js React pre-commit checks..."
# Check if Node.js is available
if ! command -v node &> /dev/null; then
echo "โ Node.js not found. Please install Node.js."
exit 1
fi
# Check if npm is available
if ! command -v npm &> /dev/null; then
echo "โ npm not found. Please install npm."
exit 1
fi
# Install dependencies if needed
if [ ! -d "node_modules" ]; then
echo "๐ฆ Installing dependencies..."
npm install
fi
# Run ESLint
echo "๐ Running ESLint..."
npm run lint
# Run Prettier
echo "๐จ Running Prettier..."
npm run format
# Run TypeScript check if available
if [ -f "tsconfig.json" ]; then
echo "๐ฌ Running TypeScript check..."
npx tsc --noEmit
fi
# Run React tests
echo "๐งช Running React tests..."
npm test -- --watchAll=false
echo "โ
Node.js React pre-commit checks passed!"
"#
.to_string(),
_ => r#"#!/bin/bash
# Node.js Pre-commit Hook
set -e
echo "๐ Running Node.js pre-commit checks..."
# Check if Node.js is available
if ! command -v node &> /dev/null; then
echo "โ Node.js not found. Please install Node.js."
exit 1
fi
# Check if npm is available
if ! command -v npm &> /dev/null; then
echo "โ npm not found. Please install npm."
exit 1
fi
# Install dependencies if needed
if [ ! -d "node_modules" ]; then
echo "๐ฆ Installing dependencies..."
npm install
fi
# Run ESLint if available
if [ -f ".eslintrc.js" ] || [ -f ".eslintrc.json" ]; then
echo "๐ Running ESLint..."
npm run lint
fi
# Run Prettier if available
if [ -f ".prettierrc" ] || [ -f ".prettierrc.js" ]; then
echo "๐จ Running Prettier..."
npm run format
fi
# Run TypeScript check if available
if [ -f "tsconfig.json" ]; then
echo "๐ฌ Running TypeScript check..."
npx tsc --noEmit
fi
# Run tests if available
if [ -f "package.json" ] && grep -q '"test"' package.json; then
echo "๐งช Running tests..."
npm test
fi
echo "โ
Node.js pre-commit checks passed!"
"#
.to_string(),
}
}
fn get_node_pre_push_hook(&self, template: &str) -> String {
match template {
"express" => r#"#!/bin/bash
# Node.js Express Pre-push Hook
set -e
echo "๐ Running Node.js Express pre-push checks..."
# Install dependencies if needed
if [ ! -d "node_modules" ]; then
echo "๐ฆ Installing dependencies..."
npm install
fi
# Run full test suite
echo "๐งช Running full Jest test suite..."
npm test -- --coverage
# Run security audit
echo "๐ Running security audit..."
npm audit
# Build project
echo "๐จ Building project..."
npm run build
echo "โ
Node.js Express pre-push checks passed!"
"#
.to_string(),
"react" => r#"#!/bin/bash
# Node.js React Pre-push Hook
set -e
echo "๐ Running Node.js React pre-push checks..."
# Install dependencies if needed
if [ ! -d "node_modules" ]; then
echo "๐ฆ Installing dependencies..."
npm install
fi
# Run full test suite
echo "๐งช Running full React test suite..."
npm test -- --coverage --watchAll=false
# Run security audit
echo "๐ Running security audit..."
npm audit
# Build project
echo "๐จ Building project..."
npm run build
echo "โ
Node.js React pre-push checks passed!"
"#
.to_string(),
_ => r#"#!/bin/bash
# Node.js Pre-push Hook
set -e
echo "๐ Running Node.js pre-push checks..."
# Install dependencies if needed
if [ ! -d "node_modules" ]; then
echo "๐ฆ Installing dependencies..."
npm install
fi
# Run full test suite if available
if [ -f "package.json" ] && grep -q '"test"' package.json; then
echo "๐งช Running full test suite..."
npm test -- --coverage
fi
# Run security audit
echo "๐ Running security audit..."
npm audit
# Build project if available
if [ -f "package.json" ] && grep -q '"build"' package.json; then
echo "๐จ Building project..."
npm run build
fi
echo "โ
Node.js pre-push checks passed!"
"#
.to_string(),
}
}
fn get_go_pre_commit_hook(&self, template: &str) -> String {
match template {
"web" => r#"#!/bin/bash
# Go Web Pre-commit Hook
set -e
echo "๐ Running Go web pre-commit checks..."
# Check if Go is available
if ! command -v go &> /dev/null; then
echo "โ Go not found. Please install Go."
exit 1
fi
# Download dependencies
echo "๐ฆ Downloading dependencies..."
go mod download
go mod tidy
# Run go fmt
echo "๐จ Running go fmt..."
go fmt ./...
# Run go vet
echo "๐ Running go vet..."
go vet ./...
# Run golangci-lint if available
if command -v golangci-lint &> /dev/null; then
echo "๐ Running golangci-lint..."
golangci-lint run
fi
# Run tests
echo "๐งช Running Go tests..."
go test ./...
echo "โ
Go web pre-commit checks passed!"
"#
.to_string(),
"cli" => r#"#!/bin/bash
# Go CLI Pre-commit Hook
set -e
echo "๐ Running Go CLI pre-commit checks..."
# Check if Go is available
if ! command -v go &> /dev/null; then
echo "โ Go not found. Please install Go."
exit 1
fi
# Download dependencies
echo "๐ฆ Downloading dependencies..."
go mod download
go mod tidy
# Run go fmt
echo "๐จ Running go fmt..."
go fmt ./...
# Run go vet
echo "๐ Running go vet..."
go vet ./...
# Run golangci-lint if available
if command -v golangci-lint &> /dev/null; then
echo "๐ Running golangci-lint..."
golangci-lint run
fi
# Run tests
echo "๐งช Running Go tests..."
go test ./...
# Build CLI to check for errors
echo "๐จ Building CLI..."
go build -o /tmp/cli ./cmd/cli
echo "โ
Go CLI pre-commit checks passed!"
"#
.to_string(),
_ => r#"#!/bin/bash
# Go Pre-commit Hook
set -e
echo "๐ Running Go pre-commit checks..."
# Check if Go is available
if ! command -v go &> /dev/null; then
echo "โ Go not found. Please install Go."
exit 1
fi
# Download dependencies
echo "๐ฆ Downloading dependencies..."
go mod download
go mod tidy
# Run go fmt
echo "๐จ Running go fmt..."
go fmt ./...
# Run go vet
echo "๐ Running go vet..."
go vet ./...
# Run golangci-lint if available
if command -v golangci-lint &> /dev/null; then
echo "๐ Running golangci-lint..."
golangci-lint run
fi
# Run tests
echo "๐งช Running Go tests..."
go test ./...
echo "โ
Go pre-commit checks passed!"
"#
.to_string(),
}
}
fn get_go_pre_push_hook(&self, template: &str) -> String {
match template {
"web" => r#"#!/bin/bash
# Go Web Pre-push Hook
set -e
echo "๐ Running Go web pre-push checks..."
# Download dependencies
echo "๐ฆ Downloading dependencies..."
go mod download
go mod tidy
# Run full test suite with coverage
echo "๐งช Running full Go test suite with coverage..."
go test -coverprofile=coverage.out ./...
go tool cover -func=coverage.out
# Run golangci-lint with all checks
if command -v golangci-lint &> /dev/null; then
echo "๐ Running golangci-lint with all checks..."
golangci-lint run --enable-all
fi
# Build for multiple platforms
echo "๐จ Building for multiple platforms..."
GOOS=linux GOARCH=amd64 go build -o /tmp/app-linux-amd64 .
GOOS=darwin GOARCH=amd64 go build -o /tmp/app-darwin-amd64 .
GOOS=windows GOARCH=amd64 go build -o /tmp/app-windows-amd64.exe .
echo "โ
Go web pre-push checks passed!"
"#
.to_string(),
_ => r#"#!/bin/bash
# Go Pre-push Hook
set -e
echo "๐ Running Go pre-push checks..."
# Download dependencies
echo "๐ฆ Downloading dependencies..."
go mod download
go mod tidy
# Run full test suite with coverage
echo "๐งช Running full Go test suite with coverage..."
go test -coverprofile=coverage.out ./...
go tool cover -func=coverage.out
# Run golangci-lint with all checks
if command -v golangci-lint &> /dev/null; then
echo "๐ Running golangci-lint with all checks..."
golangci-lint run --enable-all
fi
echo "โ
Go pre-push checks passed!"
"#
.to_string(),
}
}
fn get_rust_pre_commit_hook(&self, template: &str) -> String {
match template {
"web" => r#"#!/bin/bash
# Rust Web Pre-commit Hook
set -e
echo "๐ Running Rust web pre-commit checks..."
# Check if Rust is available
if ! command -v cargo &> /dev/null; then
echo "โ Cargo not found. Please install Rust."
exit 1
fi
# Check code
echo "๐ Running cargo check..."
cargo check
# Run clippy
echo "๐ Running clippy..."
cargo clippy --all-targets --all-features -- -D warnings
# Format code
echo "๐จ Running rustfmt..."
cargo fmt --all -- --check
# Run tests
echo "๐งช Running Rust tests..."
cargo test
echo "โ
Rust web pre-commit checks passed!"
"#
.to_string(),
"cli" => r#"#!/bin/bash
# Rust CLI Pre-commit Hook
set -e
echo "๐ Running Rust CLI pre-commit checks..."
# Check if Rust is available
if ! command -v cargo &> /dev/null; then
echo "โ Cargo not found. Please install Rust."
exit 1
fi
# Check code
echo "๐ Running cargo check..."
cargo check
# Run clippy
echo "๐ Running clippy..."
cargo clippy --all-targets --all-features -- -D warnings
# Format code
echo "๐จ Running rustfmt..."
cargo fmt --all -- --check
# Run tests
echo "๐งช Running Rust tests..."
cargo test
# Build CLI to check for errors
echo "๐จ Building CLI..."
cargo build
echo "โ
Rust CLI pre-commit checks passed!"
"#
.to_string(),
_ => r#"#!/bin/bash
# Rust Pre-commit Hook
set -e
echo "๐ Running Rust pre-commit checks..."
# Check if Rust is available
if ! command -v cargo &> /dev/null; then
echo "โ Cargo not found. Please install Rust."
exit 1
fi
# Check code
echo "๐ Running cargo check..."
cargo check
# Run clippy
echo "๐ Running clippy..."
cargo clippy --all-targets --all-features -- -D warnings
# Format code
echo "๐จ Running rustfmt..."
cargo fmt --all -- --check
# Run tests
echo "๐งช Running Rust tests..."
cargo test
echo "โ
Rust pre-commit checks passed!"
"#
.to_string(),
}
}
fn get_rust_pre_push_hook(&self, template: &str) -> String {
match template {
"web" => r#"#!/bin/bash
# Rust Web Pre-push Hook
set -e
echo "๐ Running Rust web pre-push checks..."
# Run full test suite
echo "๐งช Running full Rust test suite..."
cargo test --all-features
# Run clippy with all checks
echo "๐ Running clippy with all checks..."
cargo clippy --all-targets --all-features -- -D warnings
# Run tests with coverage if available
if command -v cargo-llvm-cov &> /dev/null; then
echo "๐ Running tests with coverage..."
cargo llvm-cov --html
fi
# Build for release
echo "๐จ Building for release..."
cargo build --release
echo "โ
Rust web pre-push checks passed!"
"#
.to_string(),
_ => r#"#!/bin/bash
# Rust Pre-push Hook
set -e
echo "๐ Running Rust pre-push checks..."
# Run full test suite
echo "๐งช Running full Rust test suite..."
cargo test --all-features
# Run clippy with all checks
echo "๐ Running clippy with all checks..."
cargo clippy --all-targets --all-features -- -D warnings
# Run tests with coverage if available
if command -v cargo-llvm-cov &> /dev/null; then
echo "๐ Running tests with coverage..."
cargo llvm-cov --html
fi
# Build for release
echo "๐จ Building for release..."
cargo build --release
echo "โ
Rust pre-push checks passed!"
"#
.to_string(),
}
}
fn get_bash_pre_commit_hook(&self, template: &str) -> String {
match template {
"devops" => r#"#!/bin/bash
# Bash DevOps Pre-commit Hook
set -e
echo "๐ Running Bash DevOps pre-commit checks..."
# Check if ShellCheck is available
if ! command -v shellcheck &> /dev/null; then
echo "โ ShellCheck not found. Please install ShellCheck."
exit 1
fi
# Run ShellCheck on all shell scripts
echo "๐ Running ShellCheck..."
find . -name '*.sh' -not -path './vendor/*' -exec shellcheck {} +
# Check formatting with shfmt if available
if command -v shfmt &> /dev/null; then
echo "๐จ Checking formatting with shfmt..."
shfmt -d -i 2 -ci .
fi
# Validate syntax
echo "๐ Checking Bash syntax..."
find . -name '*.sh' -not -path './vendor/*' -exec bash -n {} +
# Run BATS tests if available
if [ -d "test" ] && command -v bats &> /dev/null; then
echo "๐งช Running BATS tests..."
bats test/
fi
echo "โ
Bash DevOps pre-commit checks passed!"
"#
.to_string(),
"cli" => r#"#!/bin/bash
# Bash CLI Pre-commit Hook
set -e
echo "๐ Running Bash CLI pre-commit checks..."
# Check if ShellCheck is available
if ! command -v shellcheck &> /dev/null; then
echo "โ ShellCheck not found. Please install ShellCheck."
exit 1
fi
# Run ShellCheck on all shell scripts
echo "๐ Running ShellCheck..."
find . -name '*.sh' -not -path './vendor/*' -exec shellcheck {} +
# Check formatting with shfmt if available
if command -v shfmt &> /dev/null; then
echo "๐จ Checking formatting with shfmt..."
shfmt -d -i 2 -ci .
fi
# Validate syntax
echo "๐ Checking Bash syntax..."
find . -name '*.sh' -not -path './vendor/*' -exec bash -n {} +
# Run BATS tests if available
if [ -d "test" ] && command -v bats &> /dev/null; then
echo "๐งช Running BATS tests..."
bats test/
fi
# Build check - verify main script runs
echo "๐จ Checking main script..."
bash -n main.sh
echo "โ
Bash CLI pre-commit checks passed!"
"#
.to_string(),
_ => r#"#!/bin/bash
# Bash Pre-commit Hook
set -e
echo "๐ Running Bash pre-commit checks..."
# Check if ShellCheck is available
if ! command -v shellcheck &> /dev/null; then
echo "โ ShellCheck not found. Please install ShellCheck."
exit 1
fi
# Run ShellCheck on all shell scripts
echo "๐ Running ShellCheck..."
find . -name '*.sh' -not -path './vendor/*' -exec shellcheck {} +
# Check formatting with shfmt if available
if command -v shfmt &> /dev/null; then
echo "๐จ Checking formatting with shfmt..."
shfmt -d -i 2 -ci .
fi
# Validate syntax
echo "๐ Checking Bash syntax..."
find . -name '*.sh' -not -path './vendor/*' -exec bash -n {} +
# Run BATS tests if available
if [ -d "test" ] && command -v bats &> /dev/null; then
echo "๐งช Running BATS tests..."
bats test/
fi
echo "โ
Bash pre-commit checks passed!"
"#
.to_string(),
}
}
fn get_bash_pre_push_hook(&self, template: &str) -> String {
match template {
"devops" => r#"#!/bin/bash
# Bash DevOps Pre-push Hook
set -e
echo "๐ Running Bash DevOps pre-push checks..."
# Run ShellCheck on all shell scripts
echo "๐ Running ShellCheck..."
find . -name '*.sh' -not -path './vendor/*' -exec shellcheck {} +
# Validate syntax
echo "๐ Checking Bash syntax..."
find . -name '*.sh' -not -path './vendor/*' -exec bash -n {} +
# Run full BATS test suite if available
if [ -d "test" ] && command -v bats &> /dev/null; then
echo "๐งช Running full BATS test suite..."
bats test/
fi
# Check for TODO/FIXME markers
echo "๐ Checking for TODO/FIXME markers..."
find . -name '*.sh' -not -path './vendor/*' -exec grep -Hn 'TODO\|FIXME' {} + || true
echo "โ
Bash DevOps pre-push checks passed!"
"#
.to_string(),
_ => r#"#!/bin/bash
# Bash Pre-push Hook
set -e
echo "๐ Running Bash pre-push checks..."
# Run ShellCheck on all shell scripts
echo "๐ Running ShellCheck..."
find . -name '*.sh' -not -path './vendor/*' -exec shellcheck {} +
# Validate syntax
echo "๐ Checking Bash syntax..."
find . -name '*.sh' -not -path './vendor/*' -exec bash -n {} +
# Run full BATS test suite if available
if [ -d "test" ] && command -v bats &> /dev/null; then
echo "๐งช Running full BATS test suite..."
bats test/
fi
echo "โ
Bash pre-push checks passed!"
"#
.to_string(),
}
}
fn get_basic_pre_commit_hook(&self) -> String {
r#"#!/bin/bash
# Basic Pre-commit Hook
set -e
echo "๐ Running basic pre-commit checks..."
# Run Prettier if available
if command -v prettier &> /dev/null; then
echo "๐จ Running Prettier..."
prettier --check "**/*.{js,ts,json,md,yml,yaml}" || true
fi
# Run any available linters
if [ -f ".eslintrc.js" ] || [ -f ".eslintrc.json" ]; then
echo "๐ Running ESLint..."
npx eslint . || true
fi
if [ -f ".rubocop.yml" ]; then
echo "๐ Running RuboCop..."
bundle exec rubocop || true
fi
if [ -f "pyproject.toml" ]; then
echo "๐ Running Black..."
black --check . || true
fi
echo "โ
Basic pre-commit checks passed!"
"#
.to_string()
}
fn get_basic_pre_push_hook(&self) -> String {
r#"#!/bin/bash
# Basic Pre-push Hook
set -e
echo "๐ Running basic pre-push checks..."
# Run any available tests
if [ -f "package.json" ] && grep -q '"test"' package.json; then
echo "๐งช Running npm tests..."
npm test || true
fi
if [ -f "Gemfile" ]; then
echo "๐งช Running RSpec tests..."
bundle exec rspec || true
fi
if [ -f "pytest.ini" ] || [ -f "pyproject.toml" ]; then
echo "๐งช Running Pytest tests..."
pytest || true
fi
if [ -f "Cargo.toml" ]; then
echo "๐งช Running Cargo tests..."
cargo test || true
fi
if [ -f "go.mod" ]; then
echo "๐งช Running Go tests..."
go test ./... || true
fi
echo "โ
Basic pre-push checks passed!"
"#
.to_string()
}
fn get_commit_msg_hook(&self) -> String {
r#"#!/bin/bash
# Commit Message Hook
set -e
# Get the commit message
commit_msg_file=$1
commit_msg=$(cat "$commit_msg_file")
# Check if commit message is empty
if [ -z "$commit_msg" ]; then
echo "โ Commit message cannot be empty"
exit 1
fi
# Check if commit message starts with a type
if ! echo "$commit_msg" | grep -qE "^(feat|fix|docs|style|refactor|test|chore|perf|ci|build|revert)(\(.+\))?: .+"; then
echo "โ Commit message must follow conventional commits format:"
echo " <type>(<scope>): <description>"
echo " Examples:"
echo " feat: add new feature"
echo " fix(api): resolve authentication bug"
echo " docs: update README"
exit 1
fi
# Check minimum length
if [ ${#commit_msg} -lt 10 ]; then
echo "โ Commit message must be at least 10 characters long"
exit 1
fi
echo "โ
Commit message is valid"
"#.to_string()
}
}