Skip to content

Commit 845ed41

Browse files
authored
Refactor install.ps1 for better error reporting
Enhance error handling and improve output messages in install.ps1.
1 parent 6dded84 commit 845ed41

File tree

1 file changed

+127
-52
lines changed

1 file changed

+127
-52
lines changed

install.ps1

Lines changed: 127 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ function Write-ColorOutput($ForegroundColor) {
1919
if ($args) {
2020
Write-Output $args
2121
}
22-
$host.UI.RawUI.ForegroundColor = $fc
22+
$host.UI.RawUI. ForegroundColor = $fc
2323
}
2424

2525
function Write-Info($message) {
@@ -54,7 +54,7 @@ function Show-Banner {
5454
|_____/|_| |_| |_| |_|\____/|_| \_| |_|
5555
5656
Network Scanner & Vulnerability Assessment Tool
57-
Version: 4.0.3
57+
Version: 4.0.3
5858
https://github.com/Pymmdrza/SpyHunt
5959
6060
"@ -ForegroundColor Cyan
@@ -66,19 +66,23 @@ function Test-Python {
6666

6767
try {
6868
$pythonVersion = python --version 2>&1
69-
if ($pythonVersion -match "Python 3\.([7-9]|1[0-9])") {
70-
Write-Success "Python found: $pythonVersion"
69+
if ($pythonVersion -match "Python 3\. ([7-9]|1[0-9])") {
70+
Write-Success "Python found: $pythonVersion"
7171
return $true
7272
}
73-
} catch {}
73+
} catch {
74+
Write-Warning "Python command not found"
75+
}
7476

7577
try {
7678
$python3Version = python3 --version 2>&1
77-
if ($python3Version -match "Python 3\.([7-9]|1[0-9])") {
79+
if ($python3Version -match "Python 3\. ([7-9]|1[0-9])") {
7880
Write-Success "Python3 found: $python3Version"
7981
return $true
8082
}
81-
} catch {}
83+
} catch {
84+
Write-Warning "Python3 command not found"
85+
}
8286

8387
return $false
8488
}
@@ -89,73 +93,122 @@ function Install-Python {
8993

9094
# Check for winget
9195
if (Get-Command winget -ErrorAction SilentlyContinue) {
92-
winget install Python.Python.3.11 --silent
96+
try {
97+
winget install Python. Python.3.11 --silent --accept-source-agreements --accept-package-agreements
98+
Write-Success "Python installed via winget"
99+
} catch {
100+
Write-Error "Failed to install Python via winget: $_"
101+
exit 1
102+
}
93103
}
94104
# Check for chocolatey
95105
elseif (Get-Command choco -ErrorAction SilentlyContinue) {
96-
choco install python3 -y
106+
try {
107+
choco install python3 -y
108+
Write-Success "Python installed via chocolatey"
109+
} catch {
110+
Write-Error "Failed to install Python via chocolatey: $_"
111+
exit 1
112+
}
97113
}
98114
else {
99-
Write-Error "Please install Python 3.7+ manually from https://python.org"
115+
Write-Error "No package manager found. Please install Python 3.7+ manually from https://python.org"
116+
Write-Info "Or install winget/chocolatey first"
100117
exit 1
101118
}
102119

103120
# Refresh PATH
104121
$env:Path = [System.Environment]:: GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
105122

106-
Write-Success "Python installed successfully"
123+
# Wait for PATH to update
124+
Start-Sleep -Seconds 2
125+
126+
# Verify installation
127+
if (-not (Test-Python)) {
128+
Write-Warning "Python installed but not found in PATH. Please restart PowerShell and run the installer again."
129+
exit 1
130+
}
107131
}
108132

109133
# Install SpyHunt
110134
function Install-SpyHunt {
111135
Write-Info "Installing SpyHunt..."
112136

113-
# Upgrade pip
114-
python -m pip install --upgrade pip
115-
116-
# Install SpyHunt
117-
python -m pip install spyhunt
118-
119-
Write-Success "SpyHunt installed successfully"
137+
try {
138+
# Upgrade pip
139+
Write-Info "Upgrading pip..."
140+
python -m pip install --upgrade pip --quiet
141+
142+
# Install SpyHunt
143+
Write-Info "Installing SpyHunt package..."
144+
python -m pip install spyhunt --upgrade
145+
146+
Write-Success "SpyHunt installed successfully"
147+
} catch {
148+
Write-Error "Failed to install SpyHunt: $_"
149+
Write-Info "Trying alternative installation method..."
150+
Install-FromSource
151+
}
120152
}
121153

122154
# Install from source
123155
function Install-FromSource {
124156
Write-Info "Installing SpyHunt from source..."
125157

126-
$installDir = "$env: USERPROFILE\.spyhunt"
127-
128-
# Create directory
129-
if (-not (Test-Path $installDir)) {
130-
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
158+
# Check for git
159+
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
160+
Write-Error "Git is not installed. Please install Git first or use: .\install.ps1 -Pip"
161+
exit 1
131162
}
132163

133-
# Clone repository
134-
if (Test-Path "$installDir\SpyHunt") {
135-
Write-Info "Updating existing installation..."
164+
$installDir = "$env: USERPROFILE\.spyhunt"
165+
166+
try {
167+
# Create directory
168+
if (-not (Test-Path $installDir)) {
169+
New-Item -ItemType Directory -Path $installDir -Force | Out-Null
170+
}
171+
172+
# Clone or update repository
173+
if (Test-Path "$installDir\SpyHunt") {
174+
Write-Info "Updating existing installation..."
175+
Set-Location "$installDir\SpyHunt"
176+
git pull origin main
177+
} else {
178+
Write-Info "Cloning repository..."
179+
Set-Location $installDir
180+
git clone --depth 1 https://github.com/Pymmdrza/SpyHunt.git
181+
}
182+
183+
# Install
136184
Set-Location "$installDir\SpyHunt"
137-
git pull origin main
138-
} else {
139-
Set-Location $installDir
140-
git clone --depth 1 https://github.com/Pymmdrza/SpyHunt.git
185+
python -m pip install -e .
186+
187+
Write-Success "SpyHunt installed from source"
188+
} catch {
189+
Write-Error "Failed to install from source: $_"
190+
exit 1
141191
}
142-
143-
# Install
144-
Set-Location "$installDir\SpyHunt"
145-
python -m pip install -e .
146-
147-
Write-Success "SpyHunt installed from source"
148192
}
149193

150194
# Uninstall
151195
function Uninstall-SpyHunt {
152196
Write-Info "Uninstalling SpyHunt..."
153197

154-
python -m pip uninstall spyhunt -y
198+
try {
199+
python -m pip uninstall spyhunt -y 2>$null
200+
} catch {
201+
Write-Warning "SpyHunt package not found in pip"
202+
}
155203

156-
$installDir = "$env: USERPROFILE\.spyhunt"
204+
$installDir = "$env:USERPROFILE\.spyhunt"
157205
if (Test-Path $installDir) {
158-
Remove-Item -Recurse -Force $installDir
206+
try {
207+
Remove-Item -Recurse -Force $installDir
208+
Write-Success "Removed installation directory"
209+
} catch {
210+
Write-Warning "Could not remove installation directory: $_"
211+
}
159212
}
160213

161214
Write-Success "SpyHunt uninstalled successfully"
@@ -174,7 +227,7 @@ Options:
174227
175228
Examples:
176229
irm https://raw.githubusercontent.com/Pymmdrza/SpyHunt/main/install.ps1 | iex
177-
.\install.ps1 -Pip
230+
.\install. ps1 -Pip
178231
.\install.ps1 -Uninstall
179232
180233
"@
@@ -196,7 +249,14 @@ function Main {
196249

197250
# Check Python
198251
if (-not (Test-Python)) {
199-
Install-Python
252+
Write-Warning "Python 3.7+ is required but not found"
253+
$install = Read-Host "Would you like to install Python now? (Y/N)"
254+
if ($install -eq 'Y' -or $install -eq 'y') {
255+
Install-Python
256+
} else {
257+
Write-Error "Installation cancelled. Please install Python manually and try again."
258+
exit 1
259+
}
200260
}
201261

202262
# Install
@@ -206,16 +266,31 @@ function Main {
206266
Install-SpyHunt
207267
}
208268

209-
# Verify
210-
Write-Host ""
211-
Write-Host "╔════════════════════════════════════════════════════════════════╗" -ForegroundColor Green
212-
Write-Host "║ Installation Complete! ║" -ForegroundColor Green
213-
Write-Host "╚════════════════════════════════════════════════════════════════╝" -ForegroundColor Green
214-
Write-Host ""
215-
Write-Host "Usage:" -ForegroundColor White
216-
Write-Host " spyhunt --help Show help message" -ForegroundColor Cyan
217-
Write-Host " spyhunt -u example.com Scan a target" -ForegroundColor Cyan
218-
Write-Host ""
269+
# Verify installation
270+
try {
271+
$verifyOutput = python -m pip show spyhunt 2>&1
272+
if ($LASTEXITCODE -eq 0) {
273+
Write-Host ""
274+
Write-Host "╔════════════════════════════════════════════════════════════════╗" -ForegroundColor Green
275+
Write-Host "║ Installation Complete! ║" -ForegroundColor Green
276+
Write-Host "╚════════════════════════════════════════════════════════════════╝" -ForegroundColor Green
277+
Write-Host ""
278+
Write-Host "Usage:" -ForegroundColor White
279+
Write-Host " spyhunt --help Show help message" -ForegroundColor Cyan
280+
Write-Host " spyhunt -u example.com Scan a target" -ForegroundColor Cyan
281+
Write-Host ""
282+
} else {
283+
Write-Warning "Installation completed but verification failed"
284+
}
285+
} catch {
286+
Write-Warning "Could not verify installation"
287+
}
219288
}
220289

221-
Main
290+
# Run main function
291+
try {
292+
Main
293+
} catch {
294+
Write-Error "An unexpected error occurred: $_"
295+
exit 1
296+
}

0 commit comments

Comments
 (0)