Skip to content

Commit

Permalink
Merge pull request #10 from leojonathanoh/enhancement/add-missing-wha…
Browse files Browse the repository at this point in the history
…tif-parameter-and-rename-g-debugflag-to-whatif

Enhancement: Add missing -WhatIf parameter and rename $g_debugFlag to $WhatIf
  • Loading branch information
leojonathanoh authored May 21, 2021
2 parents e2438be + 058f45c commit da8b119
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 49 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,10 @@ We'll decide to use a `Log-Rotate` *state* file in *C:\var\Log-Rotate\Log-Rotate

We'll decide to log the *Task* to *C:\logs\Log-Rotate.log*. This file will capture all the Powershell output streams.

Run the Command line with the `-Debug` parameter to make sure everything is working.
Run the Command line with the `-WhatIf` parameter to make sure everything is working.

```powershell
Powershell 'Import-Module Log-Rotate; Log-Rotate -Config "C:\configs\Log-Rotate\Log-Rotate.conf" -State "C:\var\Log-Rotate\Log-Rotate.status" -Verbose -Debug'
Powershell 'Import-Module Log-Rotate; Log-Rotate -Config "C:\configs\Log-Rotate\Log-Rotate.conf" -State "C:\var\Log-Rotate\Log-Rotate.status" -Verbose -WhatIf'
```

Task Command line:
Expand Down Expand Up @@ -176,10 +176,10 @@ We'll decide to use a `Log-Rotate` *state* file in */var/lib/Log-Rotate/Log-Rota

We'll decide to log the *cron* to */var/log/Log-Rotate.log*. This file will capture all the Powershell output streams.

Run the Command line with the `-Debug` parameter to make sure everything is working.
Run the Command line with the `-WhatIf` parameter to make sure everything is working.

```powershell
Powershell 'Import-Module Log-Rotate; Log-Rotate -Config "/etc/Log-Rotate.conf" -State "/var/lib/Log-Rotate/Log-Rotate.status" -Verbose -Debug'
Powershell 'Import-Module Log-Rotate; Log-Rotate -Config "/etc/Log-Rotate.conf" -State "/var/lib/Log-Rotate/Log-Rotate.status" -Verbose -WhatIf'
```

Cron command line:
Expand Down Expand Up @@ -208,7 +208,7 @@ PARAMETERS
Later config files will override earlier ones.
The best method is to use a single config file that includes other config files by using the 'include' directive.
-Debug [<SwitchParameter>]
-WhatIf [<SwitchParameter>]
In debug mode, no logs are rotated. Use this to validate your configs or observe rotation logic.
-Force [<SwitchParameter>]
Expand Down
1 change: 0 additions & 1 deletion src/Log-Rotate/classes/BlockFactory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ $BlockFactory = [PSCustomObject]@{
$yes = $_.Value

if ( $child.ContainsKey($yes) -and (!$child.ContainsKey($no)) -and $parent.ContainsKey($no) ) {
if ($g_debugFlag -band 4) { Write-Verbose "I said $yes, I didn't say $no, although my parent said $no, I'll still go ahead." }
$my_options.Remove($no)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Log-Rotate/classes/LogFactory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ $LogFactory | Add-Member -Name 'InitStatus' -MemberType ScriptMethod -Value {
# The reason for using the following code is only because the cmdlets such as Convert-Path, Resolve-Path must point to an existing item.
# If debugging didn't create the file, we would have to manually normalize the status file path (i.e. get it's absolute path).
<#
if ($g_debugFlag) {
if ($WhatIf) {
$is_home = $statusfile_path -match '^~'
if ($is_home) {
# It's an absolute path
Expand Down Expand Up @@ -127,7 +127,7 @@ $LogFactory | Add-Member -Name 'InitStatus' -MemberType ScriptMethod -Value {
# Always test for write permissions on the status file
try {
'' | Out-File $this.StatusFile_FullName -Append -Force
if (!$status -and $g_debugFlag) {
if (!$status -and $WhatIf) {
# We're running Log-Rotate the first time in debug mode.
Remove-Item $this.StatusFile_FullName
}
Expand Down Expand Up @@ -161,7 +161,7 @@ $LogFactory | Add-Member -Name 'GetAll' -MemberType ScriptMethod -Value {
$LogFactory | Add-Member -Name 'DumpStatus' -MemberType ScriptMethod -Value {

try {
if (!$g_debugFlag) {
if (!$WhatIf) {
# Update my state with each logs rotation status
$this.GetAll() | Where-Object { $_.Status['rotation_datetime'] } | ForEach-Object {
$rotationDateISO = $_.Status['rotation_datetime'].ToString('s')
Expand Down
24 changes: 12 additions & 12 deletions src/Log-Rotate/classes/LogObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ $LogObject = [PSCustomObject]@{
# File exists.
Write-Verbose "Error creating output file $my_previous_fullname`: File exists"
}else {
if (!$g_debugFlag) {
if (!$WhatIf) {
Copy-Item $my_fullname $my_previous_fullname -ErrorAction Stop
}
if ($copytruncate) {
Write-Verbose "Truncating $my_fullname"
if (!$g_debugFlag) {
if (!$WhatIf) {
Clear-Content $my_fullname
}
}else {
Expand All @@ -41,12 +41,12 @@ $LogObject = [PSCustomObject]@{
# File exists.
Write-Verbose "Error creating output file $my_previous_fullname`: File exists"
}else {
if (!$g_debugFlag) {
if (!$WhatIf) {
Move-Item $my_fullname $my_previous_fullname -Force
}
if ($create) {
Write-Verbose "Creating new log file $my_fullname"
if (!$g_debugFlag) {
if (!$WhatIf) {
$newitem = New-Item $my_fullname -ItemType File | Out-Null
if ($newitem) {

Expand Down Expand Up @@ -102,7 +102,7 @@ $LogObject = [PSCustomObject]@{

# Rename old logs
Write-Verbose "Renaming $source_fullName to $destination_fullName (rotatecount $rotate, logstart $start, i $i)"
if ($g_debugFlag) { continue }
if ($WhatIf) { continue }
if (Test-Path $source_fullName) {
Try {
Move-Item -Path $source_fullName -Destination $destination_fullName -Force
Expand Down Expand Up @@ -135,7 +135,7 @@ $LogObject = [PSCustomObject]@{
$params = @( 'rn', $fullName, '*', $baseName )
try {
Write-Verbose "Rename log inside compressed archive $fullName to $baseName"
if ($g_debugFlag) {
if ($WhatIf) {
continue
}

Expand Down Expand Up @@ -189,7 +189,7 @@ $LogObject = [PSCustomObject]@{
try {
Write-Verbose "Compressing log with: $compresscmd"
Write-Verbose "Compress command line: $compresscmd $( $params -join ' ' )"
if ($g_debugFlag) {
if ($WhatIf) {
return
}

Expand Down Expand Up @@ -342,7 +342,7 @@ $LogObject = [PSCustomObject]@{
}

# Delete file
if (!$g_debugFlag) {
if (!$WhatIf) {
Remove-Item $file_fullname
}else {
# For debugging to simulate deleted file
Expand Down Expand Up @@ -1047,8 +1047,8 @@ $LogObject | Add-Member -Name 'PostPostRotate' -MemberType ScriptMethod -Value {
if ($rotate -gt 0) {
$keep_prev_count = $rotate
$prev_files = Get-Files $my_previous_noncompressed_regex $my_previous_directory | Where-Object {
!$g_debugFlag -or
($g_debugFlag -and $_.FullName -notin $debug_my_prevfilespurged_fullnames )
!$WhatIf -or
($WhatIf -and $_.FullName -notin $debug_my_prevfilespurged_fullnames )
}

Remove-Old-Files $prev_files $keep_prev_count 1
Expand Down Expand Up @@ -1091,8 +1091,8 @@ $LogObject | Add-Member -Name 'PostPostRotate' -MemberType ScriptMethod -Value {
# Delete old compressed logs
if ($rotate -gt 0) {
$prev_files = Get-Files $my_previous_compressed_regex $my_previous_directory | Where-Object {
!$g_debugFlag -or
($g_debugFlag -and $_.FullName -notin $debug_my_prevfilespurged_fullnames )
!$WhatIf -or
($WhatIf -and $_.FullName -notin $debug_my_prevfilespurged_fullnames )
}
Remove-Old-Files $prev_files $keep_prev_compressed_count 1
}
Expand Down
1 change: 0 additions & 1 deletion src/Log-Rotate/classes/New-BlockFactory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ function New-BlockFactory {
$yes = $_.Value

if ( $child.ContainsKey($yes) -and (!$child.ContainsKey($no)) -and $parent.ContainsKey($no) ) {
if ($g_debugFlag -band 4) { Write-Verbose "I said $yes, I didn't say $no, although my parent said $no, I'll still go ahead." }
$my_options.Remove($no)
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/Log-Rotate/classes/New-LogFactory.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ function New-LogFactory {
# The reason for using the following code is only because the cmdlets such as Convert-Path, Resolve-Path must point to an existing item.
# If debugging didn't create the file, we would have to manually normalize the status file path (i.e. get it's absolute path).
<#
if ($g_debugFlag) {
if ($WhatIf) {
$is_home = $statusfile_path -match '^~'
if ($is_home) {
# It's an absolute path
Expand Down Expand Up @@ -128,7 +128,7 @@ function New-LogFactory {
# Always test for write permissions on the status file
try {
'' | Out-File $this.StatusFile_FullName -Append -Force
if (!$status -and $g_debugFlag) {
if (!$status -and $WhatIf) {
# We're running Log-Rotate the first time in debug mode.
Remove-Item $this.StatusFile_FullName
}
Expand Down Expand Up @@ -162,7 +162,7 @@ function New-LogFactory {
$LogFactory | Add-Member -Name 'DumpStatus' -MemberType ScriptMethod -Value {

try {
if (!$g_debugFlag) {
if (!$WhatIf) {
# Update my state with each logs rotation status
$this.GetAll() | Where-Object { $_.Status['rotation_datetime'] } | ForEach-Object {
$rotationDateISO = $_.Status['rotation_datetime'].ToString('s')
Expand Down
24 changes: 12 additions & 12 deletions src/Log-Rotate/classes/New-LogObject.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@ function New-LogObject {
# File exists.
Write-Verbose "Error creating output file $my_previous_fullname`: File exists"
}else {
if (!$g_debugFlag) {
if (!$WhatIf) {
Copy-Item $my_fullname $my_previous_fullname -ErrorAction Stop
}
if ($copytruncate) {
Write-Verbose "Truncating $my_fullname"
if (!$g_debugFlag) {
if (!$WhatIf) {
Clear-Content $my_fullname
}
}else {
Expand All @@ -42,12 +42,12 @@ function New-LogObject {
# File exists.
Write-Verbose "Error creating output file $my_previous_fullname`: File exists"
}else {
if (!$g_debugFlag) {
if (!$WhatIf) {
Move-Item $my_fullname $my_previous_fullname -Force
}
if ($create) {
Write-Verbose "Creating new log file $my_fullname"
if (!$g_debugFlag) {
if (!$WhatIf) {
$newitem = New-Item $my_fullname -ItemType File | Out-Null
if ($newitem) {

Expand Down Expand Up @@ -103,7 +103,7 @@ function New-LogObject {

# Rename old logs
Write-Verbose "Renaming $source_fullName to $destination_fullName (rotatecount $rotate, logstart $start, i $i)"
if ($g_debugFlag) { continue }
if ($WhatIf) { continue }
if (Test-Path $source_fullName) {
Try {
Move-Item -Path $source_fullName -Destination $destination_fullName -Force
Expand Down Expand Up @@ -136,7 +136,7 @@ function New-LogObject {
$params = @( 'rn', $fullName, '*', $baseName )
try {
Write-Verbose "Rename log inside compressed archive $fullName to $baseName"
if ($g_debugFlag) {
if ($WhatIf) {
continue
}

Expand Down Expand Up @@ -190,7 +190,7 @@ function New-LogObject {
try {
Write-Verbose "Compressing log with: $compresscmd"
Write-Verbose "Compress command line: $compresscmd $( $params -join ' ' )"
if ($g_debugFlag) {
if ($WhatIf) {
return
}

Expand Down Expand Up @@ -343,7 +343,7 @@ function New-LogObject {
}

# Delete file
if (!$g_debugFlag) {
if (!$WhatIf) {
Remove-Item $file_fullname
}else {
# For debugging to simulate deleted file
Expand Down Expand Up @@ -1047,8 +1047,8 @@ function New-LogObject {
if ($rotate -gt 0) {
$keep_prev_count = $rotate
$prev_files = Get-Files $my_previous_noncompressed_regex $my_previous_directory | Where-Object {
!$g_debugFlag -or
($g_debugFlag -and $_.FullName -notin $debug_my_prevfilespurged_fullnames )
!$WhatIf -or
($WhatIf -and $_.FullName -notin $debug_my_prevfilespurged_fullnames )
}

Remove-Old-Files $prev_files $keep_prev_count 1
Expand Down Expand Up @@ -1091,8 +1091,8 @@ function New-LogObject {
# Delete old compressed logs
if ($rotate -gt 0) {
$prev_files = Get-Files $my_previous_compressed_regex $my_previous_directory | Where-Object {
!$g_debugFlag -or
($g_debugFlag -and $_.FullName -notin $debug_my_prevfilespurged_fullnames )
!$WhatIf -or
($WhatIf -and $_.FullName -notin $debug_my_prevfilespurged_fullnames )
}
Remove-Old-Files $prev_files $keep_prev_compressed_count 1
}
Expand Down
2 changes: 1 addition & 1 deletion src/Log-Rotate/helpers/Get-Exception-Message.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function Get-Exception-Message ($ErrorRecord) {
}
}
$Message = Get-InnerExceptionMessage $ErrorRecord.Exception
if ($g_debugFlag -band 2) {
if ($WhatIf) {
$Message = $Message + "`nStacktrace:`n" + $ErrorRecord.Exception.ErrorRecord.ScriptStackTrace
}
$Message
Expand Down
4 changes: 1 addition & 3 deletions src/Log-Rotate/helpers/Start-Script.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ function Start-Script {
# Save the caller's ErrorAction
$callerEA = $ErrorActionPreference
$ErrorActionPreference = 'Stop'
if ($g_debugFlag -band 4) { Write-Verbose "[Start-Script] callerEA: $callerEA" }
if ($g_debugFlag -band 4) { Write-Verbose "[Start-Script] ErrorActionPreference: $ErrorActionPreference" }
}

process {
try {
Write-Verbose "Running script with arg $file_FullName : `n$script"
$OS = $ENV:OS
if (!$g_debugFlag) {
if (!$WhatIf) {
if ($OS -eq "Windows_NT") {
# E.g. & Powershell -Command { echo $Args[0] } -Args @('D:/console.log')

Expand Down
2 changes: 1 addition & 1 deletion src/Log-Rotate/private/rotate/Process-Local-Block.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ function Process-Local-Block {
}
}
}else {
if ($g_debugFlag) {
if ($WhatIf) {
Write-Verbose "Not running first action script, since no logs will be rotated"
Write-Verbose "Not running prerotate script, since no logs will be rotated"
Write-Verbose "Not running postrotate script, since no logs will be rotated"
Expand Down
15 changes: 8 additions & 7 deletions src/Log-Rotate/public/Log-Rotate.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ function Log-Rotate {
,
[alias("c")]
[string[]]$Config
,
[alias("d")]
[switch]$WhatIf
,
[alias("f")]
[switch]$Force
Expand All @@ -90,8 +93,9 @@ function Log-Rotate {
[switch]$Version
)

if ($debug) {
if ($WhatIf) {
Write-Warning "We are in Debug mode. No logs will be rotated."
$VerbosePreference = 'Continue'
}
if ($Force) {
Write-Warning "We are in Forced-Rotation mode."
Expand All @@ -104,8 +108,8 @@ function Log-Rotate {
# 1 - On, script does not change files. Calling Log-Rotate with -Debug will switch this to 1.
# 2 - Output Stacktrace in error messages
# 4 - On, verbose mode. Implies (1). This is NOT related to calling Log-Rotate with -Verbose, but strictly for debugging messages.
$g_debugFlag = 0
if ($g_debugFlag) {
$WhatIf = 0
if ($WhatIf) {
Write-Warning "Developer's debug flag is on."
}

Expand All @@ -122,7 +126,7 @@ function Log-Rotate {
# If we're using the -debug flag, always use -verbose mode.
$DebugPreference = 'Continue'
# Preserve our set debug flag for testing
$g_debugFlag = if ($g_debugFlag) { $g_debugFlag } else { 1 }
$WhatIf = if ($WhatIf) { $WhatIf } else { 1 }
}else {
# If we're not using the -debug flag, debug should stay silent instead of prompting.
$DebugPreference = 'SilentlyContinue'
Expand Down Expand Up @@ -159,9 +163,6 @@ function Log-Rotate {
#Write-Verbose "Current working directory: $( Convert-Path . )"
Write-Verbose "Current working directory: $( $(Get-Location).Path )"

# Debug
if ($g_debugFlag -band 4) { Write-Verbose "Verbose stream: $VerbosePreference" }
if ($g_debugFlag -band 4) { Write-Verbose "Debug stream: $DebugPreference" }

# Get the configuration as a string
if ($ConfigAsString) {
Expand Down

0 comments on commit da8b119

Please sign in to comment.