|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +# Copyright (c) 2025 IBM |
| 5 | + |
| 6 | +set -e |
| 7 | + |
| 8 | +# Parse command line arguments |
| 9 | +SKIP_SECURITY=false |
| 10 | +for arg in "$@"; do |
| 11 | + case $arg in |
| 12 | + --skip-security) |
| 13 | + SKIP_SECURITY=true |
| 14 | + shift |
| 15 | + ;; |
| 16 | + --help|-h) |
| 17 | + echo "Usage: $0 [--skip-security]" |
| 18 | + echo " --skip-security Skip security checks (govulncheck, gosec)" |
| 19 | + exit 0 |
| 20 | + ;; |
| 21 | + esac |
| 22 | +done |
| 23 | + |
| 24 | +echo "🔍 Running linter on Maestro MCP Server project..." |
| 25 | + |
| 26 | +# Colors for output |
| 27 | +RED='\033[0;31m' |
| 28 | +GREEN='\033[0;32m' |
| 29 | +YELLOW='\033[1;33m' |
| 30 | +BLUE='\033[0;34m' |
| 31 | +NC='\033[0m' # No Color |
| 32 | + |
| 33 | +print_status() { |
| 34 | + echo -e "${GREEN}[INFO]${NC} $1" |
| 35 | +} |
| 36 | + |
| 37 | +print_warning() { |
| 38 | + echo -e "${YELLOW}[WARNING]${NC} $1" |
| 39 | +} |
| 40 | + |
| 41 | +print_error() { |
| 42 | + echo -e "${RED}[ERROR]${NC} $1" |
| 43 | +} |
| 44 | + |
| 45 | +print_header() { |
| 46 | + echo -e "${BLUE}[LINT]${NC} $1" |
| 47 | +} |
| 48 | + |
| 49 | +print_success() { |
| 50 | + echo -e "${GREEN}[SUCCESS]${NC} $1" |
| 51 | +} |
| 52 | + |
| 53 | +# Function to check if command exists |
| 54 | +command_exists() { |
| 55 | + command -v "$1" >/dev/null 2>&1 |
| 56 | +} |
| 57 | + |
| 58 | +# Go linting |
| 59 | +echo "📁 Checking Go files..." |
| 60 | +if command_exists go; then |
| 61 | + print_header "Running Go linter..." |
| 62 | + |
| 63 | + # Install golangci-lint if not present |
| 64 | + if ! command_exists golangci-lint; then |
| 65 | + print_status "Installing golangci-lint..." |
| 66 | + if command_exists curl; then |
| 67 | + # Use go install to build with current Go version for compatibility |
| 68 | + go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest |
| 69 | + else |
| 70 | + print_warning "curl not found, please install golangci-lint manually" |
| 71 | + print_status "Visit: https://golangci-lint.run/usage/install/" |
| 72 | + fi |
| 73 | + fi |
| 74 | + |
| 75 | + # Run golangci-lint if available |
| 76 | + if command_exists golangci-lint; then |
| 77 | + print_status "Running golangci-lint..." |
| 78 | + if golangci-lint run --timeout 5m ./src/...; then |
| 79 | + print_success "Go linting passed!" |
| 80 | + else |
| 81 | + print_error "Go linting failed!" |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | + else |
| 85 | + # Fallback to go vet and go fmt |
| 86 | + print_status "Running go vet..." |
| 87 | + if go vet ./src/...; then |
| 88 | + print_success "go vet passed!" |
| 89 | + else |
| 90 | + print_error "go vet failed!" |
| 91 | + exit 1 |
| 92 | + fi |
| 93 | + |
| 94 | + print_status "Checking go fmt..." |
| 95 | + if [ "$(gofmt -s -l src/)" ]; then |
| 96 | + print_warning "Code is not formatted with go fmt!" |
| 97 | + print_status "Auto-fixing formatting..." |
| 98 | + gofmt -s -w src/ |
| 99 | + print_success "Code formatting fixed!" |
| 100 | + else |
| 101 | + print_success "go fmt check passed!" |
| 102 | + fi |
| 103 | + fi |
| 104 | + |
| 105 | + # Check for common Go issues |
| 106 | + print_status "Checking for common Go issues..." |
| 107 | + |
| 108 | + # Check for unused imports |
| 109 | + if command_exists goimports; then |
| 110 | + print_status "Checking imports..." |
| 111 | + if [ "$(goimports -l src/)" ]; then |
| 112 | + print_warning "Unused imports found. Run 'goimports -w src/' to fix" |
| 113 | + else |
| 114 | + print_success "Import check passed!" |
| 115 | + fi |
| 116 | + fi |
| 117 | + |
| 118 | + # Check for race conditions |
| 119 | + print_status "Checking for race conditions..." |
| 120 | + if go test -race ./src/... >/dev/null 2>&1; then |
| 121 | + print_success "Race condition check passed!" |
| 122 | + else |
| 123 | + print_warning "Race condition check failed or tests not available" |
| 124 | + fi |
| 125 | + |
| 126 | + print_success "Go linting checks passed!" |
| 127 | +else |
| 128 | + print_error "Go is not installed, skipping Go linting" |
| 129 | + exit 1 |
| 130 | +fi |
| 131 | + |
| 132 | +# JSON linting |
| 133 | +echo "📄 Checking JSON files..." |
| 134 | +if find . -name "*.json" -not -path "./src/vendor/*" -not -path "./node_modules/*" | grep -q .; then |
| 135 | + find . -name "*.json" -not -path "./src/vendor/*" -not -path "./node_modules/*" -print0 | while IFS= read -r -d '' json_file; do |
| 136 | + if ! python3 -m json.tool "$json_file" >/dev/null 2>&1; then |
| 137 | + print_error "Invalid JSON found in $json_file" |
| 138 | + exit 1 |
| 139 | + fi |
| 140 | + done |
| 141 | + print_success "JSON files are valid!" |
| 142 | +else |
| 143 | + echo "ℹ️ No JSON files found to validate" |
| 144 | +fi |
| 145 | + |
| 146 | +# YAML linting (excluding GitHub Actions workflows with false positives) |
| 147 | +echo "📋 Checking YAML files..." |
| 148 | +if find . -name "*.yml" -o -name "*.yaml" | grep -q .; then |
| 149 | + if command_exists yamllint; then |
| 150 | + # Use .yamllint config file to exclude GitHub Actions workflows with false positives |
| 151 | + if yamllint .; then |
| 152 | + print_success "YAML linting passed!" |
| 153 | + else |
| 154 | + print_warning "YAML linting issues found" |
| 155 | + fi |
| 156 | + else |
| 157 | + print_warning "yamllint not found, skipping YAML linting" |
| 158 | + print_status "Install yamllint: pip install yamllint" |
| 159 | + fi |
| 160 | +else |
| 161 | + echo "ℹ️ No YAML files found to lint" |
| 162 | +fi |
| 163 | + |
| 164 | +# Markdown linting (excluding docs directory with extended formats) |
| 165 | +echo "📝 Checking Markdown files..." |
| 166 | +if find . -name "*.md" -not -path "./src/vendor/*" -not -path "./node_modules/*" -not -path "./docs/*" | grep -q .; then |
| 167 | + if command_exists markdownlint; then |
| 168 | + if npx markdownlint "**/*.md" --ignore node_modules --ignore src/vendor --ignore docs; then |
| 169 | + print_success "Markdown linting passed!" |
| 170 | + else |
| 171 | + print_warning "Markdown linting issues found" |
| 172 | + fi |
| 173 | + else |
| 174 | + print_warning "markdownlint not found, skipping Markdown linting" |
| 175 | + print_status "Install markdownlint: npm install -g markdownlint-cli" |
| 176 | + fi |
| 177 | +else |
| 178 | + echo "ℹ️ No Markdown files found to lint (excluding docs directory with extended formats)" |
| 179 | +fi |
| 180 | + |
| 181 | +# Shell script linting |
| 182 | +echo "🐚 Checking shell scripts..." |
| 183 | +if find . -name "*.sh" | grep -q .; then |
| 184 | + if command_exists shellcheck; then |
| 185 | + find . -name "*.sh" -print0 | while IFS= read -r -d '' sh_file; do |
| 186 | + if shellcheck "$sh_file"; then |
| 187 | + print_success "Shell script $sh_file passed!" |
| 188 | + else |
| 189 | + print_error "Shell script $sh_file has issues" |
| 190 | + exit 1 |
| 191 | + fi |
| 192 | + done |
| 193 | + else |
| 194 | + print_warning "shellcheck not found, skipping shell script linting" |
| 195 | + print_status "Install shellcheck: brew install shellcheck (macOS) or apt-get install shellcheck (Ubuntu)" |
| 196 | + fi |
| 197 | +else |
| 198 | + echo "ℹ️ No shell scripts found to lint" |
| 199 | +fi |
| 200 | + |
| 201 | +# Security checks (optional) |
| 202 | +if [ "$SKIP_SECURITY" = true ]; then |
| 203 | + echo "🔒 Skipping security checks (--skip-security flag used)" |
| 204 | +else |
| 205 | + echo "🔒 Running security checks..." |
| 206 | + if command_exists govulncheck; then |
| 207 | + print_status "Running govulncheck vulnerability scanner..." |
| 208 | + if govulncheck ./src/...; then |
| 209 | + print_success "Vulnerability scan passed!" |
| 210 | + else |
| 211 | + print_warning "Vulnerabilities found" |
| 212 | + fi |
| 213 | + else |
| 214 | + print_status "govulncheck not available - using go mod audit as alternative" |
| 215 | + if go list -json -m all | grep -q '"Indirect":true'; then |
| 216 | + print_status "Checking for known vulnerabilities in dependencies..." |
| 217 | + if go mod audit; then |
| 218 | + print_success "Dependency audit passed!" |
| 219 | + else |
| 220 | + print_warning "Potential dependency issues found" |
| 221 | + fi |
| 222 | + else |
| 223 | + print_status "No indirect dependencies to audit" |
| 224 | + fi |
| 225 | + fi |
| 226 | + |
| 227 | + # Additional security checks with gosec if available |
| 228 | + if command_exists gosec; then |
| 229 | + print_status "Running gosec security scanner..." |
| 230 | + if gosec ./src/...; then |
| 231 | + print_success "Security scan passed!" |
| 232 | + else |
| 233 | + print_warning "Security issues found" |
| 234 | + fi |
| 235 | + else |
| 236 | + print_status "gosec not available - using go vet as security alternative" |
| 237 | + print_status "Running go vet for basic security checks..." |
| 238 | + if go vet -unsafeptr=false ./src/...; then |
| 239 | + print_success "Basic security checks passed!" |
| 240 | + else |
| 241 | + print_warning "Basic security checks found issues" |
| 242 | + fi |
| 243 | + fi |
| 244 | +fi |
| 245 | + |
| 246 | +# Dependency checks |
| 247 | +echo "📦 Checking dependencies..." |
| 248 | +if command_exists go; then |
| 249 | + print_status "Checking for outdated dependencies..." |
| 250 | + if command_exists go-mod-outdated; then |
| 251 | + if go-mod-outdated -update -direct; then |
| 252 | + print_success "Dependencies are up to date!" |
| 253 | + else |
| 254 | + print_warning "Some dependencies may be outdated" |
| 255 | + fi |
| 256 | + else |
| 257 | + print_status "Checking go.mod..." |
| 258 | + if go mod verify; then |
| 259 | + print_success "Dependencies verified!" |
| 260 | + else |
| 261 | + print_error "Dependency verification failed!" |
| 262 | + exit 1 |
| 263 | + fi |
| 264 | + fi |
| 265 | +fi |
| 266 | + |
| 267 | +print_success "All code quality checks completed successfully!" |
| 268 | +echo "🎯 Linting completed successfully!" |
0 commit comments