@@ -26,13 +26,76 @@ OPTIONAL_PORTS=(
2626 " 5432:PostgreSQL (SonarQube)"
2727)
2828
29- # Function to check if a port is available
29+ # Function to detect operating system
30+ detect_os () {
31+ if [[ " $OSTYPE " == " msys" || " $OSTYPE " == " cygwin" || " $OSTYPE " == " win32" ]]; then
32+ echo " windows"
33+ elif [[ " $OSTYPE " == " darwin" * ]]; then
34+ echo " macos"
35+ elif [[ " $OSTYPE " == " linux-gnu" * ]]; then
36+ echo " linux"
37+ else
38+ echo " unknown"
39+ fi
40+ }
41+
42+ # Function to check if a port is available (platform-independent)
3043check_port () {
3144 local port=$1
3245 local description=$2
3346 local optional=${3:- false}
34-
35- if nc -z localhost $port 2> /dev/null; then
47+ local os=$( detect_os)
48+ local port_in_use=false
49+
50+ case $os in
51+ " windows" )
52+ # Use netstat on Windows (available by default)
53+ if netstat -an | grep -q " :$port " ; then
54+ port_in_use=true
55+ fi
56+ ;;
57+ " macos" )
58+ # Use netstat on macOS (available by default)
59+ if netstat -an | grep -q " \\ .$port " ; then
60+ port_in_use=true
61+ fi
62+ ;;
63+ " linux" )
64+ # Try multiple methods on Linux
65+ if command -v ss & > /dev/null; then
66+ # Use ss (modern replacement for netstat)
67+ if ss -tuln | grep -q " :$port " ; then
68+ port_in_use=true
69+ fi
70+ elif command -v netstat & > /dev/null; then
71+ # Fall back to netstat
72+ if netstat -tuln | grep -q " :$port " ; then
73+ port_in_use=true
74+ fi
75+ elif command -v nc & > /dev/null; then
76+ # Fall back to netcat
77+ if nc -z localhost $port 2> /dev/null; then
78+ port_in_use=true
79+ fi
80+ else
81+ echo -e " ${YELLOW} ⚠️ Cannot check port $port - no suitable tools available${NC} "
82+ return 0
83+ fi
84+ ;;
85+ * )
86+ # Unknown OS - try netcat if available
87+ if command -v nc & > /dev/null; then
88+ if nc -z localhost $port 2> /dev/null; then
89+ port_in_use=true
90+ fi
91+ else
92+ echo -e " ${YELLOW} ⚠️ Cannot check port $port - unsupported OS${NC} "
93+ return 0
94+ fi
95+ ;;
96+ esac
97+
98+ if [ " $port_in_use " = true ]; then
3699 if [ " $optional " = true ]; then
37100 echo -e " ${YELLOW} ⚠️ OPTIONAL PORT $port ($description ) is already in use${NC} "
38101 else
@@ -49,73 +112,86 @@ check_port() {
49112 return 0
50113}
51114
52- # Function to suggest alternative ports
53- suggest_alternative_port () {
54- local base_port=$1
55- local description=$2
56- local start_range=$(( base_port + 1 ))
57- local end_range=$(( base_port + 100 ))
58-
59- echo -e " ${BLUE} 💡 Searching for alternative ports for $description ...${NC} "
60-
61- for (( port= start_range; port<= end_range; port++ )) ; do
62- if ! nc -z localhost $port 2> /dev/null; then
63- echo -e " ${GREEN} Suggested alternative: $port ${NC} "
64- return 0
65- fi
66- done
67-
68- echo -e " ${YELLOW} No alternatives found in range $start_range -$end_range ${NC} "
69- return 1
70- }
115+ # Check if required tools are available and provide helpful messages
116+ check_requirements () {
117+ local os=$( detect_os)
118+ local tools_available=false
71119
72- # Function to show port change instructions
73- show_port_change_instructions () {
74- local port=$1
75- local description=$2
76- local suggested_port=$3
77-
78- echo -e " ${BLUE} 📝 To change port $port ($description ) to $suggested_port :${NC} "
79-
80- case $port in
81- 8081)
82- echo " Update containers/docker-compose.yml: ports: \" $suggested_port :80\" "
120+ case $os in
121+ " windows" )
122+ if command -v netstat & > /dev/null; then
123+ tools_available=true
124+ echo -e " ${GREEN} ✅ Using netstat for port checking (Windows)${NC} "
125+ fi
83126 ;;
84- 8001)
85- echo " Update containers/docker-compose.yml: ports: \" $suggested_port :8001\" "
127+ " macos" )
128+ if command -v netstat & > /dev/null; then
129+ tools_available=true
130+ echo -e " ${GREEN} ✅ Using netstat for port checking (macOS)${NC} "
131+ fi
86132 ;;
87- 3306)
88- echo " Update containers/docker-compose.yml: ports: \" $suggested_port :3306\" "
133+ " linux" )
134+ if command -v ss & > /dev/null; then
135+ tools_available=true
136+ echo -e " ${GREEN} ✅ Using ss for port checking (Linux)${NC} "
137+ elif command -v netstat & > /dev/null; then
138+ tools_available=true
139+ echo -e " ${GREEN} ✅ Using netstat for port checking (Linux)${NC} "
140+ elif command -v nc & > /dev/null; then
141+ tools_available=true
142+ echo -e " ${GREEN} ✅ Using netcat for port checking (Linux)${NC} "
143+ fi
89144 ;;
90- 6379)
91- echo " Update containers/docker-compose.yml: ports: \" $suggested_port :6379\" "
92- ;;
93- 3307)
94- echo " Update containers/docker-compose.test.yml: ports: \" $suggested_port :3306\" "
95- ;;
96- 6380)
97- echo " Update containers/docker-compose.test.yml: ports: \" $suggested_port :6379\" "
98- ;;
99- 9000)
100- echo " Update containers/docker-compose.sonarqube.yml: ports: \" $suggested_port :9000\" "
101- ;;
102- 5432)
103- echo " Update containers/docker-compose.sonarqube.yml: ports: \" $suggested_port :5432\" "
145+ * )
146+ if command -v nc & > /dev/null; then
147+ tools_available=true
148+ echo -e " ${GREEN} ✅ Using netcat for port checking${NC} "
149+ fi
104150 ;;
105151 esac
106- echo " "
152+
153+ if [ " $tools_available " = false ]; then
154+ echo -e " ${RED} ❌ Error: No suitable tools available for port checking.${NC} "
155+ echo " Please install one of the following:"
156+ case $os in
157+ " linux" )
158+ echo " • ss: sudo apt-get install iproute2 (Ubuntu/Debian) or sudo yum install iproute (CentOS/RHEL)"
159+ echo " • netstat: sudo apt-get install net-tools (Ubuntu/Debian) or sudo yum install net-tools (CentOS/RHEL)"
160+ echo " • netcat: sudo apt-get install netcat (Ubuntu/Debian) or sudo yum install nc (CentOS/RHEL)"
161+ ;;
162+ " macos" )
163+ echo " • netcat: brew install netcat"
164+ ;;
165+ " windows" )
166+ echo " • netstat should be available by default on Windows"
167+ echo " • If using WSL, install net-tools: sudo apt-get install net-tools"
168+ ;;
169+ * )
170+ echo " • netcat: install via your package manager"
171+ ;;
172+ esac
173+ return 1
174+ fi
175+
176+ return 0
107177}
108178
109179# Main function
110180main () {
111- echo -e " ${BLUE} 🔍 Checking port availability for Laravel Blog API Docker setup...${NC} "
181+ echo -e " ${BLUE} ��� Checking port availability for Laravel Blog API Docker setup...${NC} "
112182 echo " "
113-
183+
184+ # Check if we have the required tools
185+ if ! check_requirements; then
186+ exit 1
187+ fi
188+ echo " "
189+
114190 local unavailable_ports=()
115191 local failed_required=false
116-
192+
117193 # Check required ports
118- echo -e " ${BLUE} 📋 Checking required ports:${NC} "
194+ echo -e " ${BLUE} ��� Checking required ports:${NC} "
119195 for port_info in " ${REQUIRED_PORTS[@]} " ; do
120196 local port=" ${port_info%%:* } "
121197 local description=" ${port_info#*: } "
@@ -124,62 +200,36 @@ main() {
124200 failed_required=true
125201 fi
126202 done
127-
203+
128204 echo " "
129-
205+
130206 # Check optional ports (SonarQube)
131- echo -e " ${BLUE} 📋 Checking optional ports (SonarQube):${NC} "
207+ echo -e " ${BLUE} ��� Checking optional ports (SonarQube):${NC} "
132208 for port_info in " ${OPTIONAL_PORTS[@]} " ; do
133209 local port=" ${port_info%%:* } "
134210 local description=" ${port_info#*: } "
135211 check_port " $port " " $description " true
136212 done
137-
213+
138214 echo " "
139-
215+
140216 # Summary and recommendations
141217 if [ " $failed_required " = true ]; then
142218 echo -e " ${RED} ❌ SETUP CANNOT CONTINUE - Some required ports are unavailable!${NC} "
143219 echo " "
144- echo -e " ${YELLOW} 🔧 SOLUTIONS:${NC} "
220+ echo -e " ${YELLOW} ��� SOLUTIONS:${NC} "
145221 echo " 1. Stop the services using these ports"
146222 echo " 2. Use alternative ports (see suggestions below)"
147223 echo " 3. Modify docker-compose files with new ports"
148224 echo " "
149-
150- echo -e " ${BLUE} 💡 PORT ALTERNATIVES:${NC} "
151- for port_info in " ${unavailable_ports[@]} " ; do
152- local port=" ${port_info%%:* } "
153- local description=" ${port_info#*: } "
154-
155- # Find suggested alternative
156- local start_range=$(( port + 1 ))
157- for (( alt_port= start_range; alt_port<= start_range+ 50 ; alt_port++ )) ; do
158- if ! nc -z localhost $alt_port 2> /dev/null; then
159- show_port_change_instructions " $port " " $description " " $alt_port "
160- break
161- fi
162- done
163- done
164-
165225 echo -e " ${YELLOW} ⚠️ After making changes, run this script again to verify.${NC} "
166226 return 1
167227 else
168228 echo -e " ${GREEN} ✅ ALL REQUIRED PORTS ARE AVAILABLE!${NC} "
169- echo -e " ${GREEN} 🚀 Docker setup can proceed safely.${NC} "
229+ echo -e " ${GREEN} ��� Docker setup can proceed safely.${NC} "
170230 return 0
171231 fi
172232}
173233
174- # Check if netcat is available
175- if ! command -v nc & > /dev/null; then
176- echo -e " ${RED} ❌ Error: 'nc' (netcat) is required but not installed.${NC} "
177- echo " Please install netcat:"
178- echo " macOS: brew install netcat"
179- echo " Ubuntu/Debian: sudo apt-get install netcat"
180- echo " CentOS/RHEL: sudo yum install nc"
181- exit 1
182- fi
183-
184234# Run the main function
185235main " $@ "
0 commit comments