feat: init project
This commit is contained in:
345
scripts/build.sh
Executable file
345
scripts/build.sh
Executable file
@@ -0,0 +1,345 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Database Render Application - Build Script
|
||||
# This script provides comprehensive build functionality for the application
|
||||
|
||||
set -e
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Configuration
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
|
||||
BINARY_NAME="database-render"
|
||||
DIST_DIR="$PROJECT_DIR/dist"
|
||||
BIN_DIR="$PROJECT_DIR/bin"
|
||||
|
||||
# Functions
|
||||
log_info() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
log_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
log_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# Check if Go is installed
|
||||
check_go() {
|
||||
if ! command -v go >/dev/null 2>&1; then
|
||||
log_error "Go is not installed. Please install Go first."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
local go_version=$(go version | awk '{print $3}')
|
||||
log_info "Go version: $go_version"
|
||||
}
|
||||
|
||||
# Check if required tools are installed
|
||||
check_tools() {
|
||||
local tools=("go")
|
||||
|
||||
for tool in "${tools[@]}"; do
|
||||
if command -v "$tool" >/dev/null 2>&1; then
|
||||
log_success "$tool is available"
|
||||
else
|
||||
log_error "$tool is not installed"
|
||||
exit 1
|
||||
fi
|
||||
done
|
||||
}
|
||||
|
||||
# Clean build artifacts
|
||||
clean() {
|
||||
log_info "Cleaning build artifacts..."
|
||||
|
||||
if [ -d "$BIN_DIR" ]; then
|
||||
rm -rf "$BIN_DIR"
|
||||
log_success "Removed $BIN_DIR"
|
||||
fi
|
||||
|
||||
if [ -d "$DIST_DIR" ]; then
|
||||
rm -rf "$DIST_DIR"
|
||||
log_success "Removed $DIST_DIR"
|
||||
fi
|
||||
|
||||
go clean
|
||||
log_success "Clean completed"
|
||||
}
|
||||
|
||||
# Install dependencies
|
||||
install_deps() {
|
||||
log_info "Installing dependencies..."
|
||||
|
||||
cd "$PROJECT_DIR"
|
||||
go mod tidy
|
||||
go mod download
|
||||
|
||||
log_success "Dependencies installed"
|
||||
}
|
||||
|
||||
# Build for current platform
|
||||
build_current() {
|
||||
log_info "Building for current platform..."
|
||||
|
||||
mkdir -p "$BIN_DIR"
|
||||
cd "$PROJECT_DIR"
|
||||
|
||||
local build_time=$(date -u '+%Y-%m-%d_%H:%M:%S')
|
||||
local git_commit=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
|
||||
|
||||
go build \
|
||||
-ldflags "-X main.Version=dev -X main.BuildTime=$build_time -X main.GitCommit=$git_commit" \
|
||||
-o "$BIN_DIR/$BINARY_NAME" \
|
||||
./cmd/server
|
||||
|
||||
log_success "Built for current platform: $BIN_DIR/$BINARY_NAME"
|
||||
}
|
||||
|
||||
# Build for multiple platforms
|
||||
build_all() {
|
||||
log_info "Building for multiple platforms..."
|
||||
|
||||
mkdir -p "$DIST_DIR"
|
||||
cd "$PROJECT_DIR"
|
||||
|
||||
local build_time=$(date -u '+%Y-%m-%d_%H:%M:%S')
|
||||
local git_commit=$(git rev-parse --short HEAD 2>/dev/null || echo "unknown")
|
||||
local ldflags="-X main.Version=dev -X main.BuildTime=$build_time -X main.GitCommit=$git_commit"
|
||||
|
||||
# Build targets
|
||||
local targets=(
|
||||
"linux/amd64"
|
||||
"linux/arm64"
|
||||
"darwin/amd64"
|
||||
"darwin/arm64"
|
||||
"windows/amd64"
|
||||
)
|
||||
|
||||
for target in "${targets[@]}"; do
|
||||
local os=$(echo "$target" | cut -d'/' -f1)
|
||||
local arch=$(echo "$target" | cut -d'/' -f2)
|
||||
local ext=""
|
||||
|
||||
if [ "$os" = "windows" ]; then
|
||||
ext=".exe"
|
||||
fi
|
||||
|
||||
log_info "Building for $os/$arch..."
|
||||
|
||||
GOOS=$os GOARCH=$arch go build \
|
||||
-ldflags "$ldflags" \
|
||||
-o "$DIST_DIR/${BINARY_NAME}-${os}-${arch}${ext}" \
|
||||
./cmd/server
|
||||
|
||||
log_success "Built: ${BINARY_NAME}-${os}-${arch}${ext}"
|
||||
done
|
||||
|
||||
log_success "Multi-platform build completed in $DIST_DIR/"
|
||||
}
|
||||
|
||||
# Run tests
|
||||
run_tests() {
|
||||
log_info "Running tests..."
|
||||
|
||||
cd "$PROJECT_DIR"
|
||||
go test -v ./...
|
||||
|
||||
log_success "Tests completed"
|
||||
}
|
||||
|
||||
# Run tests with coverage
|
||||
run_tests_coverage() {
|
||||
log_info "Running tests with coverage..."
|
||||
|
||||
cd "$PROJECT_DIR"
|
||||
go test -v -coverprofile=coverage.out ./...
|
||||
go tool cover -html=coverage.out -o coverage.html
|
||||
|
||||
log_success "Tests completed with coverage report: coverage.html"
|
||||
}
|
||||
|
||||
# Format code
|
||||
format_code() {
|
||||
log_info "Formatting code..."
|
||||
|
||||
cd "$PROJECT_DIR"
|
||||
go fmt ./...
|
||||
|
||||
log_success "Code formatted"
|
||||
}
|
||||
|
||||
# Check code formatting
|
||||
check_format() {
|
||||
log_info "Checking code formatting..."
|
||||
|
||||
cd "$PROJECT_DIR"
|
||||
local unformatted=$(gofmt -l .)
|
||||
|
||||
if [ -n "$unformatted" ]; then
|
||||
log_error "Code is not properly formatted. Run 'go fmt ./...' to fix."
|
||||
echo "$unformatted"
|
||||
exit 1
|
||||
else
|
||||
log_success "Code formatting is correct"
|
||||
fi
|
||||
}
|
||||
|
||||
# Run linter
|
||||
run_linter() {
|
||||
if command -v golangci-lint >/dev/null 2>&1; then
|
||||
log_info "Running linter..."
|
||||
cd "$PROJECT_DIR"
|
||||
golangci-lint run
|
||||
log_success "Linter completed"
|
||||
else
|
||||
log_warning "golangci-lint not found. Install with: go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest"
|
||||
fi
|
||||
}
|
||||
|
||||
# Security scan
|
||||
security_scan() {
|
||||
if command -v gosec >/dev/null 2>&1; then
|
||||
log_info "Running security scan..."
|
||||
cd "$PROJECT_DIR"
|
||||
gosec ./...
|
||||
log_success "Security scan completed"
|
||||
else
|
||||
log_warning "gosec not found. Install with: go install github.com/securecodewarrior/gosec/v2/cmd/gosec@latest"
|
||||
fi
|
||||
}
|
||||
|
||||
# Initialize project
|
||||
init_project() {
|
||||
log_info "Initializing project..."
|
||||
|
||||
# Create directories
|
||||
mkdir -p "$PROJECT_DIR/config"
|
||||
mkdir -p "$PROJECT_DIR/data"
|
||||
mkdir -p "$PROJECT_DIR/logs"
|
||||
|
||||
# Copy example configuration if it doesn't exist
|
||||
if [ ! -f "$PROJECT_DIR/config/config.yaml" ]; then
|
||||
cp "$PROJECT_DIR/config/config.example.yaml" "$PROJECT_DIR/config/config.yaml"
|
||||
log_success "Created config.yaml from example"
|
||||
fi
|
||||
|
||||
# Copy schema if it doesn't exist
|
||||
if [ ! -f "$PROJECT_DIR/config/schema.sql" ]; then
|
||||
cp "$PROJECT_DIR/config/schema.example.sql" "$PROJECT_DIR/config/schema.sql"
|
||||
log_success "Created schema.sql from example"
|
||||
fi
|
||||
|
||||
install_deps
|
||||
|
||||
log_success "Project initialized successfully!"
|
||||
log_info "Please edit config/config.yaml to configure your database connection"
|
||||
log_info "Then run: $0 build-current"
|
||||
}
|
||||
|
||||
# Show help
|
||||
show_help() {
|
||||
echo "Database Render Application - Build Script"
|
||||
echo ""
|
||||
echo "Usage: $0 [command]"
|
||||
echo ""
|
||||
echo "Commands:"
|
||||
echo " init - Initialize project with sample configuration"
|
||||
echo " deps - Install dependencies"
|
||||
echo " clean - Clean build artifacts"
|
||||
echo " build - Build for current platform"
|
||||
echo " build-all - Build for multiple platforms"
|
||||
echo " test - Run tests"
|
||||
echo " test-cov - Run tests with coverage"
|
||||
echo " fmt - Format code"
|
||||
echo " fmt-check - Check code formatting"
|
||||
echo " lint - Run linter"
|
||||
echo " security - Run security scan"
|
||||
echo " all - Run full build pipeline (clean, deps, test, build)"
|
||||
echo " help - Show this help message"
|
||||
echo ""
|
||||
echo "Examples:"
|
||||
echo " $0 init"
|
||||
echo " $0 build"
|
||||
echo " $0 test-cov"
|
||||
}
|
||||
|
||||
# Main function
|
||||
main() {
|
||||
case "${1:-help}" in
|
||||
"init")
|
||||
check_go
|
||||
init_project
|
||||
;;
|
||||
"deps")
|
||||
check_go
|
||||
install_deps
|
||||
;;
|
||||
"clean")
|
||||
clean
|
||||
;;
|
||||
"build")
|
||||
check_go
|
||||
check_tools
|
||||
install_deps
|
||||
build_current
|
||||
;;
|
||||
"build-all")
|
||||
check_go
|
||||
check_tools
|
||||
install_deps
|
||||
build_all
|
||||
;;
|
||||
"test")
|
||||
check_go
|
||||
run_tests
|
||||
;;
|
||||
"test-cov")
|
||||
check_go
|
||||
run_tests_coverage
|
||||
;;
|
||||
"fmt")
|
||||
check_go
|
||||
format_code
|
||||
;;
|
||||
"fmt-check")
|
||||
check_go
|
||||
check_format
|
||||
;;
|
||||
"lint")
|
||||
check_go
|
||||
run_linter
|
||||
;;
|
||||
"security")
|
||||
security_scan
|
||||
;;
|
||||
"all")
|
||||
check_go
|
||||
check_tools
|
||||
clean
|
||||
install_deps
|
||||
check_format
|
||||
run_tests
|
||||
build_current
|
||||
log_success "Full build pipeline completed!"
|
||||
;;
|
||||
"help"|*)
|
||||
show_help
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Run main function with all arguments
|
||||
main "$@"
|
||||
360
scripts/dev.sh
Executable file
360
scripts/dev.sh
Executable file
@@ -0,0 +1,360 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Database Render Application - Development Script
|
||||
# This script provides development utilities for the application
|
||||
|
||||
set -e
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Configuration
|
||||
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
|
||||
BINARY_NAME="database-render"
|
||||
CONFIG_FILE="$PROJECT_DIR/config/config.yaml"
|
||||
|
||||
# Functions
|
||||
log_info() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
log_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
log_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
|
||||
log_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# Check if service is running
|
||||
check_service() {
|
||||
local port=${1:-8080}
|
||||
if curl -sf http://localhost:$port/health >/dev/null; then
|
||||
return 0
|
||||
else
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Wait for service to be ready
|
||||
wait_for_service() {
|
||||
local port=${1:-8080}
|
||||
local max_attempts=30
|
||||
local attempt=1
|
||||
|
||||
log_info "Waiting for service to be ready on port $port..."
|
||||
|
||||
while [ $attempt -le $max_attempts ]; do
|
||||
if check_service $port; then
|
||||
log_success "Service is ready!"
|
||||
return 0
|
||||
fi
|
||||
|
||||
log_info "Attempt $attempt/$max_attempts - waiting..."
|
||||
sleep 2
|
||||
((attempt++))
|
||||
done
|
||||
|
||||
log_error "Service failed to start within $max_attempts attempts"
|
||||
return 1
|
||||
}
|
||||
|
||||
# Initialize SQLite database
|
||||
init_sqlite() {
|
||||
local db_file="$PROJECT_DIR/data/app.db"
|
||||
|
||||
if [ ! -f "$db_file" ]; then
|
||||
log_info "Creating SQLite database at $db_file..."
|
||||
mkdir -p "$(dirname "$db_file")"
|
||||
sqlite3 "$db_file" < "$PROJECT_DIR/config/schema.example.sql"
|
||||
log_success "SQLite database initialized"
|
||||
else
|
||||
log_info "SQLite database already exists at $db_file"
|
||||
fi
|
||||
}
|
||||
|
||||
# Generate sample data
|
||||
generate_data() {
|
||||
local db_file="$PROJECT_DIR/data/app.db"
|
||||
|
||||
if [ -f "$db_file" ]; then
|
||||
log_info "Generating sample data..."
|
||||
sqlite3 "$db_file" <<EOF
|
||||
-- Insert sample data if tables are empty
|
||||
INSERT OR IGNORE INTO users (username, email, full_name, role) VALUES
|
||||
('admin', 'admin@example.com', 'System Administrator', 'admin'),
|
||||
('developer', 'dev@example.com', 'Developer User', 'user'),
|
||||
('editor', 'editor@example.com', 'Content Editor', 'user');
|
||||
|
||||
INSERT OR IGNORE INTO posts (title, content, category, tags, author_id, status) VALUES
|
||||
('Welcome to Database Render', '# Welcome to Database Render
|
||||
|
||||
This is your first post created by the development script!
|
||||
|
||||
## Features
|
||||
|
||||
- Easy database visualization
|
||||
- Modern card-based UI
|
||||
- Responsive design
|
||||
- Multiple database support
|
||||
|
||||
## Getting Started
|
||||
|
||||
1. Configure your database connection in config/config.yaml
|
||||
2. Run the application
|
||||
3. Start exploring your data!', 'Technology', 'welcome,getting-started', 1, 'published'),
|
||||
|
||||
('Development Tips', '# Development Tips
|
||||
|
||||
Here are some useful development tips:
|
||||
|
||||
## Hot Reload
|
||||
|
||||
Use air for hot reloading during development:
|
||||
```bash
|
||||
make watch
|
||||
```
|
||||
|
||||
## Database Setup
|
||||
|
||||
The development script can initialize your SQLite database automatically.
|
||||
|
||||
## Configuration
|
||||
|
||||
Edit config/config.yaml to customize your setup.', 'Programming', 'development,tips', 2, 'published');
|
||||
EOF
|
||||
log_success "Sample data generated"
|
||||
else
|
||||
log_error "Database file not found: $db_file"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Start development server
|
||||
start_dev() {
|
||||
log_info "Starting development server..."
|
||||
|
||||
# Ensure config exists
|
||||
if [ ! -f "$CONFIG_FILE" ]; then
|
||||
log_info "Creating development configuration..."
|
||||
cp "$PROJECT_DIR/config/config.example.yaml" "$CONFIG_FILE"
|
||||
|
||||
# Update config for development
|
||||
sed -i.bak 's/path: "data\/app.db"/path: "data\/app.db"/' "$CONFIG_FILE"
|
||||
sed -i.bak 's/debug: false/debug: true/' "$CONFIG_FILE"
|
||||
sed -i.bak 's/level: "info"/level: "debug"/' "$CONFIG_FILE"
|
||||
rm -f "$CONFIG_FILE.bak"
|
||||
|
||||
log_success "Development configuration created"
|
||||
fi
|
||||
|
||||
# Initialize database
|
||||
init_sqlite
|
||||
|
||||
# Generate sample data
|
||||
generate_data
|
||||
|
||||
# Build and run
|
||||
cd "$PROJECT_DIR"
|
||||
if [ -f "Makefile" ]; then
|
||||
log_info "Building with Makefile..."
|
||||
make build
|
||||
|
||||
if [ -f "$PROJECT_DIR/bin/$BINARY_NAME" ]; then
|
||||
log_success "Starting application..."
|
||||
CONFIG_FILE="$CONFIG_FILE" "$PROJECT_DIR/bin/$BINARY_NAME"
|
||||
else
|
||||
log_error "Binary not found: $PROJECT_DIR/bin/$BINARY_NAME"
|
||||
exit 1
|
||||
fi
|
||||
else
|
||||
log_error "Makefile not found"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Start with hot reload
|
||||
start_hot_reload() {
|
||||
log_info "Starting with hot reload..."
|
||||
|
||||
# Check if air is installed
|
||||
if command -v air >/dev/null 2>&1; then
|
||||
log_info "Using air for hot reload..."
|
||||
cd "$PROJECT_DIR"
|
||||
air
|
||||
else
|
||||
log_warning "air not found. Installing..."
|
||||
go install github.com/cosmtrek/air@latest
|
||||
|
||||
if command -v air >/dev/null 2>&1; then
|
||||
cd "$PROJECT_DIR"
|
||||
air
|
||||
else
|
||||
log_error "Failed to install air"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
# Database utilities
|
||||
reset_db() {
|
||||
local db_file="$PROJECT_DIR/data/app.db"
|
||||
|
||||
if [ -f "$db_file" ]; then
|
||||
log_info "Resetting database..."
|
||||
rm -f "$db_file"
|
||||
init_sqlite
|
||||
generate_data
|
||||
log_success "Database reset completed"
|
||||
else
|
||||
log_info "Database not found, creating new..."
|
||||
init_sqlite
|
||||
generate_data
|
||||
fi
|
||||
}
|
||||
|
||||
# Health check
|
||||
health_check() {
|
||||
local port=${1:-8080}
|
||||
|
||||
if check_service $port; then
|
||||
log_success "Service is healthy on port $port"
|
||||
curl -s http://localhost:$port/health | jq . 2>/dev/null || curl -s http://localhost:$port/health
|
||||
else
|
||||
log_error "Service is not responding on port $port"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Show logs
|
||||
tail_logs() {
|
||||
local log_file="$PROJECT_DIR/logs/app.log"
|
||||
|
||||
if [ -f "$log_file" ]; then
|
||||
log_info "Tailing application logs..."
|
||||
tail -f "$log_file"
|
||||
else
|
||||
log_info "Log file not found, checking stdout..."
|
||||
log_info "Application is logging to stdout/stderr"
|
||||
fi
|
||||
}
|
||||
|
||||
# Open browser
|
||||
open_browser() {
|
||||
local port=${1:-8080}
|
||||
local url="http://localhost:$port"
|
||||
|
||||
log_info "Opening browser at $url..."
|
||||
|
||||
case "$(uname -s)" in
|
||||
Darwin*)
|
||||
open "$url"
|
||||
;;
|
||||
Linux*)
|
||||
if command -v xdg-open >/dev/null 2>&1; then
|
||||
xdg-open "$url"
|
||||
elif command -v gnome-open >/dev/null 2>&1; then
|
||||
gnome-open "$url"
|
||||
else
|
||||
log_info "Please open your browser and navigate to: $url"
|
||||
fi
|
||||
;;
|
||||
CYGWIN*|MINGW*|MSYS*)
|
||||
start "$url"
|
||||
;;
|
||||
*)
|
||||
log_info "Please open your browser and navigate to: $url"
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Show help
|
||||
show_help() {
|
||||
echo "Database Render Application - Development Script"
|
||||
echo ""
|
||||
echo "Usage: $0 [command] [options]"
|
||||
echo ""
|
||||
echo "Commands:"
|
||||
echo " start - Start development server"
|
||||
echo " watch - Start with hot reload (using air)"
|
||||
echo " init-db - Initialize SQLite database"
|
||||
echo " reset-db - Reset database with sample data"
|
||||
echo " health - Check service health"
|
||||
echo " logs - Tail application logs"
|
||||
echo " open - Open browser to application"
|
||||
echo " help - Show this help message"
|
||||
echo ""
|
||||
echo "Options:"
|
||||
echo " --port PORT - Specify port (default: 8080)"
|
||||
echo ""
|
||||
echo "Examples:"
|
||||
echo " $0 start"
|
||||
echo " $0 watch"
|
||||
echo " $0 reset-db"
|
||||
echo " $0 health --port 8080"
|
||||
echo " $0 open --port 8080"
|
||||
}
|
||||
|
||||
# Parse command line arguments
|
||||
parse_args() {
|
||||
local command="$1"
|
||||
local port=8080
|
||||
|
||||
while [[ $# -gt 0 ]]; do
|
||||
case $1 in
|
||||
--port)
|
||||
port="$2"
|
||||
shift 2
|
||||
;;
|
||||
*)
|
||||
shift
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
case "$command" in
|
||||
"start")
|
||||
start_dev
|
||||
;;
|
||||
"watch")
|
||||
start_hot_reload
|
||||
;;
|
||||
"init-db")
|
||||
init_sqlite
|
||||
generate_data
|
||||
;;
|
||||
"reset-db")
|
||||
reset_db
|
||||
;;
|
||||
"health")
|
||||
health_check $port
|
||||
;;
|
||||
"logs")
|
||||
tail_logs
|
||||
;;
|
||||
"open")
|
||||
open_browser $port
|
||||
;;
|
||||
"help"|*)
|
||||
show_help
|
||||
;;
|
||||
esac
|
||||
}
|
||||
|
||||
# Main execution
|
||||
main() {
|
||||
cd "$PROJECT_DIR"
|
||||
parse_args "$@"
|
||||
}
|
||||
|
||||
# Run main function
|
||||
main "$@"
|
||||
Reference in New Issue
Block a user