Refactor for testing

This commit is contained in:
dfinke
2021-07-24 13:01:32 -04:00
parent 91eefbd48b
commit 204b82144f

View File

@@ -5,43 +5,27 @@ function Read-Clipboard {
Check out the how to video - https://youtu.be/dv2GOH5sbpA Check out the how to video - https://youtu.be/dv2GOH5sbpA
.DESCRIPTION .DESCRIPTION
Read text from clipboard. It is tuned to read tab delimited data. It can read CSV or JSON Read text from clipboard. It can read CSV or JSON. Plus, you can specify the delimiter and headers.
.EXAMPLE .EXAMPLE
Read-Clipboard # delimter default is tab `t Read-Clipboard # Detects if the clipboard contains CSV, JSON, or Tab delimited data.
.EXAMPLE .EXAMPLE
Read-Clipboard -Delimiter ',' # Converts CSV Read-Clipboard -Delimiter '|' # Converts data using a pipe delimiter
.EXAMPLE .EXAMPLE
Read-Clipboard -AsJson # Converts JSON Read-Clipboard -Header 'P1', 'P2', 'P3' # Specify the header columns to be used
#> #>
param( param(
$Delimiter = "`t", $Delimiter,
$Header, $Header
[Switch]$AsJson
) )
if ($IsWindows) { if ($IsWindows) {
$osInfo = Get-CimInstance -ClassName Win32_OperatingSystem $osInfo = Get-CimInstance -ClassName Win32_OperatingSystem
if ($osInfo.ProductType -eq 1) { if ($osInfo.ProductType -eq 1) {
$clipboardData = Get-Clipboard -Raw ReadClipboardImpl (Get-Clipboard -Raw)
if ($AsJson) {
ConvertFrom-Json -InputObject $clipboardData
}
else {
$cvtParams = @{
InputObject = $clipboardData
Delimiter = $Delimiter
}
if ($Header) {
$cvtParams.Header = $Header
}
ConvertFrom-Csv @cvtParams
}
} }
else { else {
Write-Error "This command is only supported on the desktop." Write-Error "This command is only supported on the desktop."
@@ -51,3 +35,42 @@ function Read-Clipboard {
Write-Error "This function is only available on Windows desktop" Write-Error "This function is only available on Windows desktop"
} }
} }
function ReadClipboardImpl {
param(
[Parameter(Mandatory)]
[ValidateNotNullOrEmpty()]
[String] $data,
$Delimiter,
$Header
)
if (!$PSBoundParameters.ContainsKey('Delimiter') -and !$PSBoundParameters.ContainsKey('Header')) {
try {
ConvertFrom-Json $data
}
catch {
if ($data.indexof(",") -gt -1) {
ConvertFrom-Csv $data
}
else {
ConvertFrom-Csv $data -Delimiter "`t"
}
}
}
else {
$cvtParams = @{
InputObject = $data
}
if ($Delimiter) {
$cvtParams.Delimiter = $Delimiter
}
if ($Header) {
$cvtParams.Header = $Header
}
ConvertFrom-Csv @cvtParams
}
}