-
Notifications
You must be signed in to change notification settings - Fork 0
/
main-wincred.bat
366 lines (292 loc) · 9.48 KB
/
main-wincred.bat
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
::------------------------------------------------------------------------------
:: Upddates the global git user.name and user.email.
:: Manages git target credentials in the Windows Credential Manager:
:: - Deletes the user's active git password
:: - Sets the new git target with Personal Access Token (PAT)
::
:: Succeeding git fetch/pull/push will not prompt for the user's Git password or
:: Personal Access Token, since its stored in the Windows Credential Manager
:: weaponsforge;20240905
::------------------------------------------------------------------------------
@echo off
GOTO Init
:: Check for required software (Git)
:Init
:: Local temporary files
set "LOCAL_SETTINGS_FILE=.settings"
set "LOCAL_GIT_PROVIDER="
set "envFile=.env"
:: Constants
set TARGET_GITHUB=git:https://github.com
set TARGET_GITLAB=git:https://gitlab.com
set TARGET_BITBUCKET=git:https://bitbucket.org
set PLAT_GITHUB=github
set PLAT_GITLAB=gitlab
set PLAT_BITBUCKET=bitbucket
:: Check required software
CALL :CheckInstalled git
set /A errCount += %errorlevel%
CALL :CheckInstalled cmdKey
set /A errCount += %errorlevel%
if %errCount%==0 (
GOTO Main
) else (
echo [ERROR]: GitBash and Windows Credential Manager are required to run this script.
pause
)
EXIT /B 0
:: Display the main menu
:Main
:: Clear the input variables
set "GIT_PROVIDER="
set "GIT_USERNAME="
set "GIT_EMAIL="
set "PERSONAL_ACCESS_TOKEN="
set "GPG_KEY="
set "targetname="
set /A choice=1
set /A gitrepository=4
CALL :ReadUserPreferenceFile
cls
echo ----------------------------------------------------------
echo VIEWING GIT SWITCHER OPTIONS
echo ----------------------------------------------------------
echo [1] View git user config
echo [2] Edit git user config
echo [3] Exit
set /p choice="Select option:"
(if %choice% EQU 1 (
GOTO ViewUserConfig
) else if %choice% EQU 2 (
GOTO PromptUserInput
) else if %choice% EQU 3 (
EXIT /B 0
))
EXIT /B 0
:: Prompt for git config username and git provider
:PromptUserInput
setlocal enabledelayedexpansion
cls
echo ----------------------------------------------------------
echo EDIT GIT USER CONFIG DETAILS
echo ----------------------------------------------------------
set /p GIT_USERNAME="Enter git user.name:"
echo.
echo Which Git account would you like to edit?
echo [1] Github
echo [2] Gitlab
echo [3] BitBucket
echo [4] Exit
set /p gitrepository="Select option:"
(if %gitrepository% EQU 1 (
CALL :SetWinCredTarget %PLAT_GITHUB%
) else if %gitrepository% EQU 2 (
CALL :SetWinCredTarget %PLAT_GITLAB%
) else if %gitrepository% EQU 3 (
CALL :SetWinCredTarget %PLAT_BITBUCKET%
) else (
GOTO Main
))
:: Read user git data from file
CALL :ReadFile %GIT_PROVIDER% %GIT_USERNAME%
:: Set new git global user config and reset Win Credential Store data
:: if there are no errors reading the file
if %errorlevel% == 1 (
GOTO ProcessError
) else (
CALL :ResetPassword
:: Set the password in Windows Credential Manager if its present in the .env file
if defined PERSONAL_ACCESS_TOKEN (
CALL :SetPassword
) else (
echo [INFO]: Personal Access Token not detected. Skipping set password...
)
if !errorlevel! == 0 (
GOTO SetUserConfig
) else (
pause
GOTO Main
)
)
EXIT /B 0
:: Display the current git user config
:ViewUserConfig
cls
echo ----------------------------------------------------------
echo GIT USER CONFIG DETAILS (global)
echo ----------------------------------------------------------
echo Git Provider: %LOCAL_GIT_PROVIDER%
echo|set /p="Username: "
git config --get user.name
echo|set /p="Email: "
git config --get user.email
echo|set /p="GPG Key: "
git config --get user.signingkey
if "%LOCAL_GIT_PROVIDER%" NEQ "not yet set" (
CALL :ViewWinCredConfig
)
echo.
set /p choice=Press Enter to continue...
GOTO Main
EXIT /B 0
:: Logs the Windows Credential information of the active Git target
:ViewWinCredConfig
cmdKey /list:%targetname%
EXIT /B 0
:: Set the new git user config
:: Unsets the global commit.signingkey if its not provided
:SetUserConfig
git config --global user.name "%GIT_USERNAME%"
git config --global user.email "%GIT_EMAIL%"
if defined GIT_SIGNING_KEY (
:: Check if gpg is available
CALL :CheckInstalled gpg
if !errorlevel!==0 (
git config --global user.signingkey %GIT_SIGNING_KEY%!
git config --global commit.gpgsign true
echo [INFO]: Setting the global git signing key and commit settings.
) else (
echo [INFO]: gpg program not found. Skipping commit.gpgsign configuration.
)
) else (
git config --global --unset user.signingkey
git config --global --unset commit.gpgsign
echo [INFO]: GPG signing key not defined.
echo [INFO]: Resetting global git signing key and commit settings.
)
CALL :WriteUserPreference
echo.
echo [SUCCESS]: New global Git user config set.
GOTO ProcessError
EXIT /B 0
:: Deletes the password in the Windows Credential Manager
:: for the newly-set git user so it will be prompted on the next git operation
:ResetPassword
set "doreset="
echo.
set /p doreset=Would you like to reset the password? [Y/n]:
if /i "%doreset%"=="Y" (
:: Delete Git credentials in the Windows Credential Manager
for /f "delims=" %%i in ('cmdKey /delete:%targetname% 2^>^&1') do set "output=%%i"
echo.
echo Deleting %targetname% in the Windows Credential Manager...
echo !output!
echo The Git credentials were successfully deleted from the
echo Windows Credential Manager, if they were present.
)
EXIT /B 0
:: Sets a new target entry with password in the Windows Credential Manager
:SetPassword
cmdKey /generic:%targetname% /user:%GIT_USERNAME% /pass:%PERSONAL_ACCESS_TOKEN%
:: Check if the command failed
if errorlevel 1 (
echo [ERROR]: Credentials not set in Windows Credential Manager.
EXIT /B 1
) else (
echo [SUCCESS]: New Git credentials set in Windows Credential Manager.
)
EXIT /B 0
:: Read user data from the .env
:: @param 1: git provider name
:: @param 2: git user name
:: @returns: Sets the global system variable errorlevel=1 on file reading errors, else 0
:ReadFile
set "USER_DATA_STRING="
set "gitProvider=%~1"
set "gitUsername=%~2"
set hasError=false
for /f "tokens=*" %%a in ('findstr /r "^%gitProvider%|%gitUsername%" "%envFile%"') do (
set USER_DATA_STRING="%%a"
)
if defined USER_DATA_STRING (
for /f "tokens=1,2,3,4,5 delims=|" %%c in (!USER_DATA_STRING!) do (
set GIT_EMAIL=%%e
set GIT_SIGNING_KEY=%%f
set PERSONAL_ACCESS_TOKEN=%%g
)
if "!GIT_EMAIL!"=="" (
echo [ERROR]: git.email is required.
set hasError=true
)
if "!GIT_SIGNING_KEY!"=="" (
echo [WARNING]: git.signingkey is undefined.
)
if "!PERSONAL_ACCESS_TOKEN!"=="" (
echo [WARNING]: Personal Access Token is undefined.
)
) else (
echo [ERROR]: %gitProvider%/%gitUsername% - undefined Git account in the settings file.
set hasError=true
)
:: Reset the GIT_SIGNING_KEY if its value is "-"
if "%GIT_SIGNING_KEY%"=="-" (
set "GIT_SIGNING_KEY="
)
:: Set the errorlevel system variable to 1
if %hasError% == true (
EXIT /B 1
)
EXIT /B 0
:: Reads the local LOCAL_SETTINGS_FILE user-preference file into variables
:: Initializes the Windows Credential Manager target from user preference file
:ReadUserPreferenceFile
if exist %LOCAL_SETTINGS_FILE% (
for /f "tokens=1,2 delims==" %%a in (%LOCAL_SETTINGS_FILE%) do (
if "%%a"=="GIT_PROVIDER" (
set LOCAL_GIT_PROVIDER=%%b
CALL :SetWinCredTarget %%b
)
)
) else (
set LOCAL_GIT_PROVIDER=not yet set
)
EXIT /B 0
:: Writes new user-preference values to the LOCAL_SETTINGS_FILE file
:WriteUserPreference
set hasblank=false
if "%GIT_PROVIDER%"=="" set hasblank=true
if %hasblank% == true (
EXIT /B 0
)
if exist %LOCAL_SETTINGS_FILE% (
del %LOCAL_SETTINGS_FILE%
)
echo GIT_PROVIDER=%GIT_PROVIDER%>>%LOCAL_SETTINGS_FILE%
EXIT /B 0
:: Checks if program is installed
:: @param 1: executable program name
:: @returns: Sets the global system variable errorlevel=0 if a program is installed, else 1
:CheckInstalled
set "program=%~1"
where %program% >nul 2>&1
if %errorlevel% == 0 (
echo [INFO]: %program% IS installed
EXIT /B 0
) else (
echo [INFO]: %program% not installed
EXIT /B 1
)
EXIT /B 0
:: Sets the Windows Credential Manager domain target
:: Sets the GIT_PROVIDER and targetname global variables
:: @param 1: Git provider platform (github|gitlab|bitbucket)
:SetWinCredTarget
set "gitProvider=%~1"
set GIT_PROVIDER=%gitProvider%
(if "%GIT_PROVIDER%"=="%PLAT_GITHUB%" (
set targetname=%TARGET_GITHUB%
) else if "%GIT_PROVIDER%"=="%PLAT_GITLAB%" (
set targetname=%TARGET_GITLAB%
) else if "%GIT_PROVIDER%"=="%PLAT_BITBUCKET%" (
set targetname=%TARGET_BITBUCKET%
))
EXIT /B 0
:: Process warning messages
:ProcessError
set /p choice=Press Enter to continue...
GOTO Main
EXIT /B 0
:: Exit for critical errors
:ProcessExit
set /p choice=Press Enter to continue...
EXIT /B 0