Skip to content

Commit e9a5afd

Browse files
committed
fix: streamline script structure and enhance logging in Windows script
- Removed redundant echo statements to improve script clarity and reduce clutter in the output. - Introduced a main subroutine to organize script flow and enhance readability. - Added structured comments to delineate sections of the script, improving maintainability and understanding of the code.
1 parent e27a003 commit e9a5afd

1 file changed

Lines changed: 14 additions & 25 deletions

File tree

  • apps/portal/src/app/api/download-agent/scripts

apps/portal/src/app/api/download-agent/scripts/windows.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,6 @@ title CompAI Device Setup
88
setlocal EnableExtensions EnableDelayedExpansion
99
color 0A
1010
11-
echo ------------------------------------------------------------
12-
echo CompAI Device Setup
13-
echo Organization: ${orgId}
14-
echo Employee: ${employeeId}
15-
echo Date: %date% %time%
16-
echo ------------------------------------------------------------
17-
echo.
18-
1911
REM =========================
2012
REM Variables
2113
REM =========================
@@ -35,15 +27,18 @@ set "nl=^
3527
REM --- bootstrap log (updated once CHOSEN_DIR is known) ---
3628
set "LOG_FILE=%~dp0setup.log"
3729
38-
REM --- log a message to console and log ---
30+
goto :main
31+
32+
REM =======================================================
33+
REM Subroutines (placed AFTER main to avoid early execution)
34+
REM =======================================================
3935
:log_msg
4036
setlocal EnableDelayedExpansion
4137
set "msg=%~1"
4238
echo [%date% %time%] !msg!
4339
>>"%LOG_FILE%" echo [%date% %time%] !msg!
4440
endlocal & exit /b 0
4541
46-
REM --- run a command with logging and error capture ---
4742
:log_run
4843
setlocal EnableDelayedExpansion
4944
set "cmdline=%*"
@@ -58,11 +53,13 @@ if not "!rc!"=="0" (
5853
endlocal & set "LAST_RC=%rc%"
5954
exit /b %LAST_RC%
6055
56+
REM =========================
57+
REM Main
58+
REM =========================
59+
:main
6160
call :log_msg "Script starting"
6261
63-
REM =========================
6462
REM Admin check
65-
REM =========================
6663
whoami /groups | find "S-1-16-12288" >nul 2>&1
6764
if errorlevel 1 (
6865
color 0E
@@ -74,9 +71,7 @@ if errorlevel 1 (
7471
exit /b 5
7572
)
7673
77-
REM =========================
7874
REM Relaunch persistent window
79-
REM =========================
8075
if not "%PERSIST%"=="1" (
8176
set "PERSIST=1"
8277
call :log_msg "Re-launching in a persistent window"
@@ -92,9 +87,7 @@ cd /d "%~dp0"
9287
call :log_msg "New current directory: %cd%"
9388
echo.
9489
95-
REM =========================
9690
REM Choose writable directory
97-
REM =========================
9891
call :log_msg "Choosing destination directory; primary=%PRIMARY_DIR% fallback=%FALLBACK_DIR%"
9992
if exist "%PRIMARY_DIR%\\NUL" set "CHOSEN_DIR=%PRIMARY_DIR%"
10093
if not defined CHOSEN_DIR call :log_run mkdir "%PRIMARY_DIR%"
@@ -131,9 +124,7 @@ if not defined CHOSEN_DIR (
131124
echo Logs will be written to: %LOG_FILE%
132125
echo.
133126
134-
REM =========================
135127
REM Write marker files
136-
REM =========================
137128
if defined CHOSEN_DIR (
138129
call :log_msg "Writing organization marker file"
139130
call :log_msg "Preparing to write org marker to !MARKER_DIR!!ORG_ID!"
@@ -164,9 +155,7 @@ if defined CHOSEN_DIR (
164155
)
165156
)
166157
167-
REM =========================
168158
REM Permissions
169-
REM =========================
170159
if defined CHOSEN_DIR (
171160
call :log_msg "Setting permissions on marker directory"
172161
call :log_run icacls "!MARKER_DIR!" /inheritance:e
@@ -178,9 +167,7 @@ if defined CHOSEN_DIR (
178167
call :log_run icacls "!MARKER_DIR!!EMPLOYEE_ID!" /grant *S-1-5-18:R *S-1-5-32-544:R
179168
)
180169
181-
REM =========================
182170
REM Verify
183-
REM =========================
184171
echo.
185172
echo Verifying markers...
186173
if defined CHOSEN_DIR (
@@ -198,9 +185,7 @@ if defined CHOSEN_DIR (
198185
)
199186
rem Skipping registry checks per request
200187
201-
REM =========================
202188
REM Result / Exit
203-
REM =========================
204189
echo.
205190
echo ------------------------------------------------------------
206191
if "%HAS_ERROR%"=="0" (
@@ -227,7 +212,11 @@ echo ------------------------------------------------------------
227212
echo.
228213
echo Press any key to close this window. This will not affect installation.
229214
pause
230-
if "%HAS_ERROR%"=="0" (exit /b 0) else (exit /b %EXIT_CODE%)`;
215+
if "%HAS_ERROR%"=="0" (exit /b 0) else (exit /b %EXIT_CODE%)
216+
217+
REM End of main
218+
goto :eof
219+
`;
231220

232221
return script.replace(/\n/g, '\r\n');
233222
}

0 commit comments

Comments
 (0)