Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Test/public/MyWrite.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function Test_EnableMyDebug_All{
Invoke-PrivateContext {
param($Arguments)

Write-MyDebug -Message $Arguments[0]
Write-MyDebug -Message $Arguments[0]
Write-MyDebug -Message $Arguments[1]

} -Arguments $text0,$text1
Expand Down Expand Up @@ -261,7 +261,7 @@ function Test_EnableMyDebug_All_LoggingFilePath{

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

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

function Assert-DebugEnv($SectionString,$LoggingFile){
Expand Down
10 changes: 10 additions & 0 deletions include/MyHandle.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Set-MyInvokeCommandAlias -Alias GetGhHandle -Command 'gh api user --jq ".login"'

function Get-MyHandle{
[CmdletBinding()]
param()

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$user = Invoke-MyCommand -Command GetGhHandle

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
return $user
}
113 changes: 76 additions & 37 deletions include/MyWrite.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,20 @@
[Parameter()][string]$ForegroundColor = $OUTPUT_COLOR,
[Parameter()][switch]$NoNewLine
)

Write-MyDebug -Section "MyHost" -Message $Message

# Write-Host $message -ForegroundColor $OUTPUT_COLOR
Write-ToConsole $message -Color $ForegroundColor -NoNewLine:$NoNewLine
}

function Clear-MyHost {
[CmdletBinding()]
param()

Clear-Host
}

function Write-MyDebug {
[CmdletBinding()]
[Alias("Write-Debug")]
Expand All @@ -77,7 +87,7 @@
$timestamp = Get-Date -Format 'HH:mm:ss.fff'

# Write on host
$logMessage ="[$timestamp][D][$section] $message"
$logMessage ="[$timestamp][$MODULE_NAME][D][$section] $message"

$logMessage | Write-ToConsole -Color $DEBUG_COLOR
$logMessage | Write-MyDebugLogging
Expand All @@ -92,8 +102,7 @@

process{

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

# Check if logging is enabled
if ([string]::IsNullOrWhiteSpace( $loggingFilePath )) {
Expand Down Expand Up @@ -134,8 +143,7 @@
[Parameter(Position = 0)][string]$section
)

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

if ([string]::IsNullOrWhiteSpace( $flag )) {
return $false
Expand All @@ -156,17 +164,15 @@
$flag = $section
}

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

function Disable-ModuleNameVerbose{
param()

$moduleDebugVarName = $MODULE_NAME + "_VERBOSE"
[System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $null)
set-VerboseSections $null
}
Copy-Item -path Function:Disable-ModuleNameVerbose -Destination Function:"Disable-$($MODULE_NAME)Verbose"
Export-ModuleMember -Function "Disable-$($MODULE_NAME)Verbose"
Expand All @@ -184,19 +190,19 @@
$flags = $flags.ToLower()
$section = $section.ToLower()

return ($flags.Contains("all")) -or ( $flags.Contains($section))
return ($flags.Contains("all")) -or ( $flags -eq $section)
}

$moduleDebugVarName = $MODULE_NAME + "_DEBUG"
$flagsString = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName)

$sectionsString = get-DebugSections

# No configuration means no debug
if([string]::IsNullOrWhiteSpace( $flagsString )) {
if([string]::IsNullOrWhiteSpace( $sectionsString )) {
return $false
}

# Get flags from flagdsString
$flags = getFlagsFromSectionsString $flagsString
# Get flags from sectionsString
$flags = getSectionsFromSectionsString $sectionsString

# Add all if allow is empty.
# This mean stat flagsString only contains filters.
Expand Down Expand Up @@ -228,57 +234,54 @@
}
}

$flagsString = $sections -join " "
$sectionsString = $sections -join " "
$addedFlagsString = $AddSections -join " "

# if no section get value from env and is still mepty set to all
if([string]::IsNullOrWhiteSpace( $flagsString )) {
$flagsString = get-Sections
if( [string]::IsNullOrWhiteSpace( $flagsString )) {
$flagsString = "all"
if([string]::IsNullOrWhiteSpace( $sectionsString )) {
$sectionsString = get-DebugSections
if( [string]::IsNullOrWhiteSpace( $sectionsString )) {
$sectionsString = "all"
}
}

# Add added to flagsString if provided
# Add added to sectionsString if provided
if(-Not [string]::IsNullOrWhiteSpace( $addedFlagsString )) {
$flagsString += " " + $addedFlagsString
$sectionsString += " " + $addedFlagsString
}

set-Sections $flagsString
set-DebugSections $sectionsString

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

function getFlagsFromSectionsString($sectionsString){
$flags = @{
function getSectionsFromSectionsString($sectionsString){
$sections = @{
allow = $null
filter = $null
}

if([string]::IsNullOrWhiteSpace($sectionsString) ){
$flags.allow = @("all")
return $flags
$sections.allow = @("all")
return $sections
}

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

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

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

return $flags
return $sections
}

function Disable-ModuleNameDebug {
param()

$moduleDebugVarName = $MODULE_NAME + "_DEBUG"
[System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $null)

$moduleDEbugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH"
[System.Environment]::SetEnvironmentVariable($moduleDEbugLoggingVarName, $null)
set-DebugSections $null
set-LogFile $null
}
Copy-Item -path Function:Disable-ModuleNameDebug -Destination Function:"Disable-$($MODULE_NAME)Debug"
Export-ModuleMember -Function "Disable-$($MODULE_NAME)Debug"
Expand All @@ -288,8 +291,8 @@
param()

return @{
Sections = get-Sections
LoggingFilePath = get-LogFile
Sections = get-DebugSections
LoggingFilePath = get-DebugLogFile
}
}
Copy-Item -path Function:Get-ModuleNameDebug -Destination Function:"Get-$($MODULE_NAME)Debug"
Expand Down Expand Up @@ -337,3 +340,39 @@
$moduleDEbugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH"
[System.Environment]::SetEnvironmentVariable($moduleDEbugLoggingVarName, $logFilePath)
}

function get-DebugSections(){

Check warning

Code scanning / PSScriptAnalyzer

The cmdlet 'get-DebugSections' uses a plural noun. A singular noun should be used instead. Warning

The cmdlet 'get-DebugSections' uses a plural noun. A singular noun should be used instead.
$moduleDebugVarName = $MODULE_NAME + "_DEBUG"
$sections = [System.Environment]::GetEnvironmentVariable($moduleDebugVarName)

return $sections
}

function set-DebugSections($sections){

Check warning

Code scanning / PSScriptAnalyzer

Function 'set-DebugSections' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning

Function 'set-DebugSections' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.

Check warning

Code scanning / PSScriptAnalyzer

The cmdlet 'set-DebugSections' uses a plural noun. A singular noun should be used instead. Warning

The cmdlet 'set-DebugSections' uses a plural noun. A singular noun should be used instead.
$moduleDebugVarName = $MODULE_NAME + "_DEBUG"
[System.Environment]::SetEnvironmentVariable($moduleDebugVarName, $sections)
}

function get-DebugLogFile(){
$moduleDEbugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH"
$logfile = [System.Environment]::GetEnvironmentVariable($moduleDEbugLoggingVarName)

return $logfile
}

function set-LogFile($logFilePath){

Check warning

Code scanning / PSScriptAnalyzer

Function 'set-LogFile' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning

Function 'set-LogFile' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.
$moduleDEbugLoggingVarName = $MODULE_NAME + "_DEBUG_LOGGING_FILEPATH"
[System.Environment]::SetEnvironmentVariable($moduleDEbugLoggingVarName, $logFilePath)
}

function get-VerboseSections{

Check warning

Code scanning / PSScriptAnalyzer

The cmdlet 'get-VerboseSections' uses a plural noun. A singular noun should be used instead. Warning

The cmdlet 'get-VerboseSections' uses a plural noun. A singular noun should be used instead.
$moduleVerboseVarName = $MODULE_NAME + "_VERBOSE"
$sections = [System.Environment]::GetEnvironmentVariable($moduleVerboseVarName)

return $sections
}

function set-VerboseSections($sections){

Check warning

Code scanning / PSScriptAnalyzer

Function 'set-VerboseSections' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'. Warning

Function 'set-VerboseSections' has verb that could change system state. Therefore, the function has to support 'ShouldProcess'.

Check warning

Code scanning / PSScriptAnalyzer

The cmdlet 'set-VerboseSections' uses a plural noun. A singular noun should be used instead. Warning

The cmdlet 'set-VerboseSections' uses a plural noun. A singular noun should be used instead.
$moduleVerboseVarName = $MODULE_NAME + "_VERBOSE"
[System.Environment]::SetEnvironmentVariable($moduleVerboseVarName, $sections)
}
Loading