Skip to content

Commit b635375

Browse files
authored
Merge pull request #55 from rulasg/improvements
feat(MyHandle): add function to retrieve GitHub username and improve logging
2 parents 7cb2329 + 344644a commit b635375

3 files changed

Lines changed: 88 additions & 39 deletions

File tree

Test/public/MyWrite.test.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ function Test_EnableMyDebug_All{
104104
Invoke-PrivateContext {
105105
param($Arguments)
106106

107-
Write-MyDebug -Message $Arguments[0]
107+
Write-MyDebug -Message $Arguments[0]
108108
Write-MyDebug -Message $Arguments[1]
109109

110110
} -Arguments $text0,$text1
@@ -261,7 +261,7 @@ function Test_EnableMyDebug_All_LoggingFilePath{
261261

262262
function Assert-DbgMsg($Presented,$Section,$Message){
263263

264-
Assert-IsTrue -Condition ($Presented -match "^\[\d{2}:\d{2}:\d{2}\.\d{3}\]\[D\]\[$Section\] $Message$")
264+
Assert-IsTrue -Condition ($Presented -match "^\[\d{2}:\d{2}:\d{2}\.\d{3}\]\[IncludeHelper\]\[D\]\[$Section\] $Message$")
265265
}
266266

267267
function Assert-DebugEnv($SectionString,$LoggingFile){

include/MyHandle.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Set-MyInvokeCommandAlias -Alias GetGhHandle -Command 'gh api user --jq ".login"'
2+
3+
function Get-MyHandle{
4+
[CmdletBinding()]
5+
param()
6+
7+
$user = Invoke-MyCommand -Command GetGhHandle
8+
9+
return $user
10+
}

include/MyWrite.ps1

Lines changed: 76 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,20 @@ function Write-MyHost {
5353
[Parameter()][string]$ForegroundColor = $OUTPUT_COLOR,
5454
[Parameter()][switch]$NoNewLine
5555
)
56+
57+
Write-MyDebug -Section "MyHost" -Message $Message
58+
5659
# Write-Host $message -ForegroundColor $OUTPUT_COLOR
5760
Write-ToConsole $message -Color $ForegroundColor -NoNewLine:$NoNewLine
5861
}
5962

63+
function Clear-MyHost {
64+
[CmdletBinding()]
65+
param()
66+
67+
Clear-Host
68+
}
69+
6070
function Write-MyDebug {
6171
[CmdletBinding()]
6272
[Alias("Write-Debug")]
@@ -77,7 +87,7 @@ function Write-MyDebug {
7787
$timestamp = Get-Date -Format 'HH:mm:ss.fff'
7888

7989
# Write on host
80-
$logMessage ="[$timestamp][D][$section] $message"
90+
$logMessage ="[$timestamp][$MODULE_NAME][D][$section] $message"
8191

8292
$logMessage | Write-ToConsole -Color $DEBUG_COLOR
8393
$logMessage | Write-MyDebugLogging
@@ -92,8 +102,7 @@ function Write-MyDebugLogging {
92102

93103
process{
94104

95-
$moduleDebugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH"
96-
$loggingFilePath = [System.Environment]::GetEnvironmentVariable($moduleDebugLoggingVarName)
105+
$loggingFilePath = get-DebugLogFile
97106

98107
# Check if logging is enabled
99108
if ([string]::IsNullOrWhiteSpace( $loggingFilePath )) {
@@ -134,8 +143,7 @@ function Test-MyVerbose {
134143
[Parameter(Position = 0)][string]$section
135144
)
136145

137-
$moduleDebugVarName = $MODULE_NAME + "_VERBOSE"
138-
$flag = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName)
146+
$flag = get-VerboseSections
139147

140148
if ([string]::IsNullOrWhiteSpace( $flag )) {
141149
return $false
@@ -156,17 +164,15 @@ function Enable-ModuleNameVerbose{
156164
$flag = $section
157165
}
158166

159-
$moduleDebugVarName = $MODULE_NAME + "_VERBOSE"
160-
[System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $flag)
167+
set-VerboseSections $flag
161168
}
162169
Copy-Item -path Function:Enable-ModuleNameVerbose -Destination Function:"Enable-$($MODULE_NAME)Verbose"
163170
Export-ModuleMember -Function "Enable-$($MODULE_NAME)Verbose"
164171

165172
function Disable-ModuleNameVerbose{
166173
param()
167174

168-
$moduleDebugVarName = $MODULE_NAME + "_VERBOSE"
169-
[System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $null)
175+
set-VerboseSections $null
170176
}
171177
Copy-Item -path Function:Disable-ModuleNameVerbose -Destination Function:"Disable-$($MODULE_NAME)Verbose"
172178
Export-ModuleMember -Function "Disable-$($MODULE_NAME)Verbose"
@@ -184,19 +190,19 @@ function Test-MyDebug {
184190
$flags = $flags.ToLower()
185191
$section = $section.ToLower()
186192

187-
return ($flags.Contains("all")) -or ( $flags.Contains($section))
193+
return ($flags.Contains("all")) -or ( $flags -eq $section)
188194
}
189195

190-
$moduleDebugVarName = $MODULE_NAME + "_DEBUG"
191-
$flagsString = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName)
196+
197+
$sectionsString = get-DebugSections
192198

193199
# No configuration means no debug
194-
if([string]::IsNullOrWhiteSpace( $flagsString )) {
200+
if([string]::IsNullOrWhiteSpace( $sectionsString )) {
195201
return $false
196202
}
197203

198-
# Get flags from flagdsString
199-
$flags = getFlagsFromSectionsString $flagsString
204+
# Get flags from sectionsString
205+
$flags = getSectionsFromSectionsString $sectionsString
200206

201207
# Add all if allow is empty.
202208
# This mean stat flagsString only contains filters.
@@ -228,57 +234,54 @@ function Enable-ModuleNameDebug{
228234
}
229235
}
230236

231-
$flagsString = $sections -join " "
237+
$sectionsString = $sections -join " "
232238
$addedFlagsString = $AddSections -join " "
233239

234240
# if no section get value from env and is still mepty set to all
235-
if([string]::IsNullOrWhiteSpace( $flagsString )) {
236-
$flagsString = get-Sections
237-
if( [string]::IsNullOrWhiteSpace( $flagsString )) {
238-
$flagsString = "all"
241+
if([string]::IsNullOrWhiteSpace( $sectionsString )) {
242+
$sectionsString = get-DebugSections
243+
if( [string]::IsNullOrWhiteSpace( $sectionsString )) {
244+
$sectionsString = "all"
239245
}
240246
}
241247

242-
# Add added to flagsString if provided
248+
# Add added to sectionsString if provided
243249
if(-Not [string]::IsNullOrWhiteSpace( $addedFlagsString )) {
244-
$flagsString += " " + $addedFlagsString
250+
$sectionsString += " " + $addedFlagsString
245251
}
246252

247-
set-Sections $flagsString
253+
set-DebugSections $sectionsString
248254

249255
}
250256
Copy-Item -path Function:Enable-ModuleNameDebug -Destination Function:"Enable-$($MODULE_NAME)Debug"
251257
Export-ModuleMember -Function "Enable-$($MODULE_NAME)Debug"
252258

253-
function getFlagsFromSectionsString($sectionsString){
254-
$flags = @{
259+
function getSectionsFromSectionsString($sectionsString){
260+
$sections = @{
255261
allow = $null
256262
filter = $null
257263
}
258264

259265
if([string]::IsNullOrWhiteSpace($sectionsString) ){
260-
$flags.allow = @("all")
261-
return $flags
266+
$sections.allow = @("all")
267+
return $sections
262268
}
263269

264270
$list = $sectionsString.Split(" ", [StringSplitOptions]::RemoveEmptyEntries)
265271

266272
$split = @($list).Where({ $_ -like '-*' }, 'Split')
267273

268-
$flags.filter = $split[0] | ForEach-Object { $_ -replace '^-', '' } # -> API, Auth
269-
$flags.allow = $split[1] # -> Sync, Cache
274+
$sections.filter = $split[0] | ForEach-Object { $_ -replace '^-', '' } # -> API, Auth
275+
$sections.allow = $split[1] # -> Sync, Cache
270276

271-
return $flags
277+
return $sections
272278
}
273279

274280
function Disable-ModuleNameDebug {
275281
param()
276282

277-
$moduleDebugVarName = $MODULE_NAME + "_DEBUG"
278-
[System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $null)
279-
280-
$moduleDEbugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH"
281-
[System.Environment]::SetEnvironmentVariable($moduleDEbugLoggingVarName, $null)
283+
set-DebugSections $null
284+
set-LogFile $null
282285
}
283286
Copy-Item -path Function:Disable-ModuleNameDebug -Destination Function:"Disable-$($MODULE_NAME)Debug"
284287
Export-ModuleMember -Function "Disable-$($MODULE_NAME)Debug"
@@ -288,8 +291,8 @@ function Get-ModuleNameDebug {
288291
param()
289292

290293
return @{
291-
Sections = get-Sections
292-
LoggingFilePath = get-LogFile
294+
Sections = get-DebugSections
295+
LoggingFilePath = get-DebugLogFile
293296
}
294297
}
295298
Copy-Item -path Function:Get-ModuleNameDebug -Destination Function:"Get-$($MODULE_NAME)Debug"
@@ -337,3 +340,39 @@ function set-LogFile($logFilePath){
337340
$moduleDEbugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH"
338341
[System.Environment]::SetEnvironmentVariable($moduleDEbugLoggingVarName, $logFilePath)
339342
}
343+
344+
function get-DebugSections(){
345+
$moduleDebugVarName = $MODULE_NAME + "_DEBUG"
346+
$sections = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName)
347+
348+
return $sections
349+
}
350+
351+
function set-DebugSections($sections){
352+
$moduleDebugVarName = $MODULE_NAME + "_DEBUG"
353+
[System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $sections)
354+
}
355+
356+
function get-DebugLogFile(){
357+
$moduleDEbugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH"
358+
$logfile = [System.Environment]::GetEnvironmentVariable($moduleDEbugLoggingVarName)
359+
360+
return $logfile
361+
}
362+
363+
function set-LogFile($logFilePath){
364+
$moduleDEbugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH"
365+
[System.Environment]::SetEnvironmentVariable($moduleDEbugLoggingVarName, $logFilePath)
366+
}
367+
368+
function get-VerboseSections{
369+
$moduleVerboseVarName = $MODULE_NAME + "_VERBOSE"
370+
$sections = [System.Environment]::GetEnvironmentVariable($moduleVerboseVarName)
371+
372+
return $sections
373+
}
374+
375+
function set-VerboseSections($sections){
376+
$moduleVerboseVarName = $MODULE_NAME + "_VERBOSE"
377+
[System.Environment]::SetEnvironmentVariable($moduleVerboseVarName, $sections)
378+
}

0 commit comments

Comments
 (0)