lib/TMCSettings.ps1

function New-TMConsoleSetting {
    <#
    .SYNOPSIS
    Creates a new TMConsole PowerShell setting item
 
    .DESCRIPTION
    This function will create a new setting item with the given name and value
    in TMConsole's powershellsettings file
 
    .PARAMETER Name
    The name of the new setting item to create
 
    .PARAMETER Value
    The value of the new setting item
 
    .EXAMPLE
    New-TMConsoleSetting -Name 'TestSetting' -Value 'Setting Value #1'
 
    .EXAMPLE
    @(
        @{Name = 'NewSetting1'; Value = 123}
        @{Name = 'NewSetting2'; Value = 'Setting Value 2'}
        @{Name = 'NewSetting3'; Value = @('Val1', 'Val2', 'Val3')}
    ) | New-TMConsoleSetting
 
    .OUTPUTS
    A PSCustomObject representing all of the current TMConsole PowerShell settings
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true,
            Position = 0,
            ValueFromPipelineByPropertyName = $true)]
        [String]$Name,

        [Parameter(Mandatory = $true,
            Position = 1,
            ValueFromPipelineByPropertyName = $true)]
        [Object]$Value
    )

    begin {
        ## Check for/Create the powershellsettings file
        if ($IsWindows) {
            $PowerShellSettingsFilePath = Join-Path $env:APPDATA 'tmconsole' 'powershellsettings'
        }
        if ($isMacOS) {
            $PowerShellSettingsFilePath = Join-Path '/' 'Users' $env:USER 'Library' 'Application Support' 'tmconsole' 'powershellsettings'
        }
        if (-not (Test-Path $PowerShellSettingsFilePath -PathType Leaf)) {
            New-Item -Path $PowerShellSettingsFilePath -ItemType File -Force -Value '{}'
        }

        $PowerShellSettings = Get-Content -Path $PowerShellSettingsFilePath -Raw | ConvertFrom-Json -Depth 10
        if (-Not $PowerShellSettings) {
            $PowerShellSettings = [PSCustomObject]@{}
        }
    }

    process {
        if ($PowerShellSettings.$Name) {
            $PowerShellSettings.$Name = $Value
        }
        else {
            $PowerShellSettings | Add-Member -NotePropertyName $Name -NotePropertyValue $Value -Force
        }
    }

    end {
        $PowerShellSettings | ConvertTo-Json -Depth 10 -Compress | Set-Content -Path $PowerShellSettingsFilePath -Force
        $PowerShellSettings
    }
}


function Get-TMConsoleSetting {
    <#
    .SYNOPSIS
    Gets a TMConsole PowerShell setting item
 
    .DESCRIPTION
    This function will retrieve a setting item with the given name
    from TMConsole's powershellsettings file
 
    .PARAMETER Name
    The name of the settings item to retrieve
 
    .EXAMPLE
    Get-TMConsoleSetting 'TestSetting2'
 
    .EXAMPLE
    'TestSetting2', 'Setting1' | Get-TMConsoleSetting
 
    .EXAMPLE
    $AllSettings = Get-TMConsoleSetting
 
    .OUTPUTS
    If a setting name is provided, the value of that setting item is returned. Otherwise a
    PSCustomObject representing all of the current TMConsole PowerShell settings
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false,
            Position = 0,
            ValueFromPipeline = $true)]
        [String]$Name
    )

    begin {
        # Make sure the file exists
        if ($IsWindows) {
            $PowerShellSettingsFilePath = Join-Path $env:APPDATA 'tmconsole' 'powershellsettings'
        }
        if ($isMacOS) {
            $PowerShellSettingsFilePath = Join-Path '/' 'Users' $env:USER 'Library' 'Application Support' 'tmconsole' 'powershellsettings'
        }
        if (-not (Test-Path $PowerShellSettingsFilePath -PathType Leaf)) {
            throw "There is no setings file at '$PowerShellSettingsFilePath'"
        }

        # Get the contents of the file as an object
        $PowerShellSettings = Get-Content -Path $PowerShellSettingsFilePath -Raw | ConvertFrom-Json -Depth 10
    }

    process {
        if ($Name) {
            $PowerShellSettings.$Name
        }
        else {
            $PowerShellSettings
        }
    }
}


function Remove-TMConsoleSetting {
    <#
    .SYNOPSIS
    RDeletes a TMConsole PowerShell setting item
 
    .DESCRIPTION
    This function will delete a setting item with the given name
    in TMConsole's powershellsettings file
 
    .PARAMETER Name
    The name of the setting item to delete
 
    .EXAMPLE
    (Get-TMConsoleSetting).PSObject.Properties.Name | Remove-TMConsoleSetting
 
    .EXAMPLE
    Remove-TMConsoleSetting -Name 'Setting1'
 
    .OUTPUTS
    A PSCustomObject representing all of the current TMConsole PowerShell settings
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $false,
            Position = 0,
            ValueFromPipeline = $true)]
        [String]$Name
    )

    begin {
        # Make sure the file exists
        if ($IsWindows) {
            $PowerShellSettingsFilePath = Join-Path $env:APPDATA 'tmconsole' 'powershellsettings'
        }
        if ($isMacOS) {
            $PowerShellSettingsFilePath = Join-Path '/' 'Users' $env:USER 'Library' 'Application Support' 'tmconsole' 'powershellsettings'
        }
        if (-not (Test-Path $PowerShellSettingsFilePath -PathType Leaf)) {
            throw "There is no setings file at '$PowerShellSettingsFilePath'"
        }

        # Get the contents of the file as an object
        $PowerShellSettings = Get-Content -Path $PowerShellSettingsFilePath -Raw | ConvertFrom-Json -Depth 10
    }

    process {
        if ($PowerShellSettings.$Name) {
            $PowerShellSettings.PSObject.Properties.Remove($Name)
        }
    }

    end {
        $PowerShellSettings | ConvertTo-Json -Depth 10 -Compress | Set-Content -Path $PowerShellSettingsFilePath -Force
        $PowerShellSettings
    }
}


function Set-TMConsoleSetting {
    <#
    .SYNOPSIS
    Creates a new TMConsole PowerShell setting item
 
    .DESCRIPTION
    This function will create a new setting item with the given name and value
    in TMConsole's powershellsettings file
 
    .PARAMETER Name
    The name of the existing setting item to be updated
 
    .PARAMETER Value
    The new value for the setting item
 
    .EXAMPLE
    Set-TMConsoleSetting -Name TestSetting3 -Value 123
 
    .OUTPUTS
    A PSCustomObject representing all of the current TMConsole PowerShell settings
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true,
            Position = 0,
            ValueFromPipelineByPropertyName = $true)]
        [ArgumentCompleter({ ((Get-TMConsoleSetting).PSObject.Properties | Where-Object MemberType -EQ 'NoteProperty').Name })]
        [ValidateScript({ $_ -in ((Get-TMConsoleSetting).PSObject.Properties | Where-Object MemberType -EQ 'NoteProperty').Name })]
        [String]$Name,

        [Parameter(Mandatory = $true,
            Position = 1,
            ValueFromPipelineByPropertyName = $true)]
        [Object]$Value
    )

    begin {
        # Make sure the file exists
        if ($IsWindows) {
            $PowerShellSettingsFilePath = Join-Path $env:APPDATA 'tmconsole' 'powershellsettings'
        }
        if ($isMacOS) {
            $PowerShellSettingsFilePath = Join-Path '/' 'Users' $env:USER 'Library' 'Application Support' 'tmconsole' 'powershellsettings'
        }
        if (-not (Test-Path $PowerShellSettingsFilePath -PathType Leaf)) {
            throw "There is no setings file at '$PowerShellSettingsFilePath'"
        }

        # Get the contents of the file as an object
        $PowerShellSettings = Get-Content -Path $PowerShellSettingsFilePath -Raw | ConvertFrom-Json -Depth 10
    }

    process {
        $PowerShellSettings.$Name = $Value
    }

    end {
        $PowerShellSettings | ConvertTo-Json -Depth 10 -Compress | Set-Content -Path $PowerShellSettingsFilePath -Force
        $PowerShellSettings
    }
}