-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·208 lines (173 loc) · 5.44 KB
/
run.sh
File metadata and controls
executable file
·208 lines (173 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/bin/bash
# =============================================================================
# Generic Project Starter Script
# =============================================================================
# Description: Configurable script to start development servers
# Usage: ./run.sh [target_subdirectory]
# Author: Marc Haye
# Version: 1.0
# =============================================================================
# -----------------------------------------------------------------------------
# Configuration Variables
# -----------------------------------------------------------------------------
# Export .env variables
set -a # auto-export enable
source docker/.env
set +a # auto-export disable
# Project config
PROJECT_NAME="falloutdle"
PROJECT_PATH="$HOME/dev/perso/$PROJECT_NAME"
# Main config
MAIN_PATH="cmd/server" # Default server path
MAIN_FILE="main.go" # Main executable file
FULL_PATH="$PROJECT_PATH/$MAIN_PATH"
# -----------------------------------------------------------------------------
# Utility Functions
# -----------------------------------------------------------------------------
# Print colored output for better visibility
print_info() {
echo -e "\e[32m[INFO]\e[0m $1"
}
print_error() {
echo -e "\e[31m[ERROR]\e[0m $1"
}
print_warning() {
echo -e "\e[33m[WARNING]\e[0m $1"
}
# Navigate to project subdirectory
# Usage: goto_project [subdirectory]
goto_project() {
local target_path="$1"
if [[ -n "$target_path" ]]; then
local full_target="$PROJECT_PATH/$target_path"
if [[ -d "$full_target" ]]; then
cd "$full_target"
print_info "Navigated to: $(pwd)"
else
print_error "Directory $full_target doesn't exist"
return 1
fi
else
cd "$PROJECT_PATH"
print_info "Navigated to project root: $(pwd)"
fi
}
# Check if required dependencies are installed
check_dependencies() {
print_info "Checking dependencies..."
# Check for Go installation
if ! command -v go &> /dev/null; then
print_error "Go is not installed or not in PATH"
exit 1
fi
print_info "All dependencies are available"
}
# Validate project structure
validate_project_structure() {
print_info "Validating project structure..."
# Check if project directory exists
if [[ ! -d "$PROJECT_PATH" ]]; then
print_error "Project directory $PROJECT_PATH doesn't exist"
exit 1
fi
# Check if main path exists
if [[ ! -d "$FULL_PATH" ]]; then
print_error "Main path $FULL_PATH doesn't exist"
exit 1
fi
# Check if main file exists
if [[ ! -f "$FULL_PATH/$MAIN_FILE" ]]; then
print_error "Main file $FULL_PATH/$MAIN_FILE doesn't exist"
exit 1
fi
print_info "Project structure is valid"
}
# Setup environment variables
setup_environment() {
print_info "Setting up environment..."
# Set common environment variables
export APP_ENV="${APP_ENV:-development}"
export LOG_LEVEL="$LOG_LEVEL"
export PORT="$PORT"
print_info "Environment configured for $APP_ENV mode"
}
# Display help information
show_help() {
cat << EOF
Usage: $0 [OPTIONS] [SUBDIRECTORY]
Generic project starter script for $PROJECT_NAME
OPTIONS:
-h, --help Show this help message
-p, --port PORT Set custom port (default: $PORT)
-v, --verbose Enable verbose output
--dry-run Show what would be executed without running
EXAMPLES:
$0 # Start server in default location
$0 api/v1 # Start server in api/v1 subdirectory
$0 --port 3000 # Start server on port 3000
EOF
}
# -----------------------------------------------------------------------------
# Main Execution Logic
# -----------------------------------------------------------------------------
# Parse command line arguments
while [[ $# -gt 0 ]]; do
case $1 in
-h|--help)
show_help
exit 0
;;
-p|--port)
PORT="$2"
shift 2
;;
-v|--verbose)
set -x # Enable verbose mode
shift
;;
-dr|--dry-run)
DRY_RUN=true
shift
;;
-*)
print_error "Unknown option: $1"
show_help
exit 1
;;
*)
TARGET_SUBDIRECTORY="$1"
shift
;;
esac
done
# Set up signal handlers for graceful shutdown
trap 'print_warning "Interrupted by user"; exit 130' INT
# Pre-flight checks
print_info "Starting $PROJECT_NAME server..."
check_dependencies
validate_project_structure
setup_environment
# Navigate to target directory if specified
if [[ -n "$TARGET_SUBDIRECTORY" ]]; then
if ! goto_project "$TARGET_SUBDIRECTORY"; then
exit 1
fi
EXECUTION_PATH="$PROJECT_PATH/$TARGET_SUBDIRECTORY"
else
EXECUTION_PATH="$FULL_PATH"
fi
# Prepare the command to execute
COMMAND="$BUILD_COMMAND $EXECUTION_PATH/$MAIN_FILE"
# Display execution information
print_info "Execution path: $EXECUTION_PATH"
print_info "Command: $COMMAND"
print_info "Port: $PORT"
print_info "Environment: $APP_ENV"
# Execute the command (or show what would be executed in dry-run mode)
if [[ "$DRY_RUN" == "true" ]]; then
print_warning "DRY RUN - Would execute: $COMMAND"
else
cd "$EXECUTION_PATH"
print_info "Starting server... (Press Ctrl+C to stop)"
exec $COMMAND
fi